2017-03-06 192 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上的點。實現你的等式後,任何旋轉角色的對象都會穿過它們。我更新了項目以顯示我的代碼 –