2017-03-06 144 views
-1

我有一个3D模型,我需要围绕Y轴旋转它的顶点(轴在我的情况下直线上升)。例如让我说我有垂直 (3,2,3)(x,y,z),当我围绕Y轴旋转时,只有x和z会改变。我怎么能在Java中使用度来实现这个?提前致谢! (FYI)这是用于旋转我的hitbox上的点。每个“盒子”只是一个三角形,但包裹在一个立方体,所以我可以检查一个点是否在立方体中。这是根据每个型号的三角形完成的。这是完美的,因为我能够穿过它们中的洞和网格物体。但是,如果应用旋转奇怪的事情开始发生。Java围绕Y轴旋转3D顶点

编辑:这里是一个使用Andys方法

public static boolean checkPointCollision(Vector3f pos){ 
    boolean hit=false; 
    float px=Math.round(pos.x); 
    float py=Math.round(pos.y); 
    float pz=Math.round(pos.z); 
    px=pos.x; 
    py=pos.y; 
    pz=pos.z; 

    long startTime=System.currentTimeMillis(); 

    float xmin,ymin,zmin,xmax,ymax,zmax,scale,rot; 

    //Cube Collisions 
    for (Entity entity : entities) { 
     int colID=entity.getCollisionIndex(); 
     boolean entHasHitbox = entity.hasHitbox(); 
     if(colID!=-1 && hit==false && entHasHitbox){ 

      //Gets the entitys variables 
      scale = entity.getScale(); 
      rot = entity.getRotY(); 
      //Converts to radians 
      rot = (float) Math.toRadians(rot); 

      xmin = 0; 
      ymin = 0; 
      zmin = 0; 
      xmax = 0; 
      ymax = 0; 
      zmax = 0; 

      switch(entity.getCollisionType()){ 
      case 1: 
       if(entHasHitbox){ 

        //Gets the entities hitbox 
        List<Vector3f> hitboxMins = entity.getHitboxMin(); 
        List<Vector3f> hitboxMaxs = entity.getHitboxMax(); 

        for (int i = 0; i < hitboxMins.size(); i++) { 

         //Gets the entities hitbox points 
         Vector3f min = hitboxMins.get(i); 
         Vector3f max = hitboxMaxs.get(i); 

         //Sets all local position vars to the hitboxes mins and maxes 

         xmin = min.x; 
         ymin = min.y; 
         zmin = min.z; 
         xmax = max.x; 
         ymax = max.y; 
         zmax = max.z; 

         //Applies the models scale 

         xmin *=scale; 
         ymin *=scale; 
         zmin *=scale; 
         xmax *=scale; 
         ymax *=scale; 
         zmax *=scale; 

         //Rotates points 

         float nxmin = (float) (Math.cos(rot) * xmin - Math.sin(rot) * zmin); 
         float nzmin = (float) (Math.sin(rot) * xmin + Math.cos(rot) * zmin); 
         float nxmax = (float) (Math.cos(rot) * xmax - Math.sin(rot) * zmax); 
         float nzmax = (float) (Math.sin(rot) * xmax + Math.cos(rot) * zmax); 

         //Sets old points to new ones 

         xmin = nxmin; 
         zmin = nzmin; 
         xmax = nxmax; 
         zmax = nzmax; 

         //Increase local points to the entitys world position 

         xmin += entity.getPosition().x; 
         xmax += entity.getPosition().x; 
         ymin += entity.getPosition().y; 
         ymax += entity.getPosition().y; 
         zmin += entity.getPosition().z; 
         zmax += entity.getPosition().z; 

         //Debug 
         if(entities.get(17)==entity){//entities.get(17).increaseRotation(0, 10, 0); 
          System.out.println(xmin+","+ymin+","+zmin); 
         } 

         //Check if point is in the hitbox 

         if(px>=xmin && px<=xmax 
           && py>=ymin && py<=ymax 
           && pz>=zmin && pz<=zmax) 
         { 
          hit=true; 
          //Ends to loop 
          i=hitboxMins.size(); 
         } 

        } 
       } 
      break; 
      } 

     } 
    } 

    long endTime = System.currentTimeMillis()-startTime; 
    if(endTime>10){ 
     System.out.println("Delay in Point Collision"); 
    } 

    return hit; 
} 
+0

将您度弧度。 –

回答

0

乘贵点由以下矩阵我的代码:

[ c 0 -s ] 
[ 0 1 0 ] 
[ s 0 c ] 

[newx] [ c 0 -s ] [x] 
[newy] = [ 0 1 0 ] [y] 
[newz] [ s 0 c ] [z] 

其中(x, y, z)是你的原始坐标, (newx, newy, newz)是你的旋转坐标,和c = cos(angle)s = sin(angle)。请注意,Java的trig函数将其参数设置为弧度,因此您需要convert the angle in degrees appropriately。前

如果你不使用的矩阵,这等同于以下三个表达式:

newx = c * x - s * z 
newy = y 
newz = s * x + c * z 
+0

嗯= /只是为了让你知道我在做什么,我正在旋转我的对象hitbox上的点。实现你的等式后,任何旋转角色的对象都会穿过它们。我更新了项目以显示我的代码 –