2017-03-06 5 views
-1

J'ai un modèle 3D et j'ai besoin de faire pivoter ses sommets autour de l'axe Y (L'axe va droit dans mon cas). Par exemple disons que j'ai le vert (3,2,3) (x, y, z) et quand je tourne autour de l'axe Y, seuls les x et z vont changer. comment pourrais-je l'implémenter en Java en utilisant des degrés? Merci d'avance!Java Rotation des sommets 3D autour de l'axe Y

(FYI) C'est pour faire tourner les points sur ma hitbox. Chaque "boîte" est juste un triangle mais enveloppé dans un cube, donc je peux juste vérifier si un point est dans le cube. Ceci est fait par triangle par modèle. Cela fonctionne parfaitement parce que je suis incapable de marcher à travers les mailles avec des trous dans tout ça. Cependant, si une rotation est appliquée, des choses étranges commencent à se produire.

Edit: voici mon code en utilisant la méthode 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

Convertir vos degrés en radians. –

Répondre

0

Multipliez vos points par la matrice suivante:

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

-à-dire

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

(x, y, z) sont vos coordonnées originales, (newx, newy, newz) sont vos coordonnées tournées, et c = cos(angle) et s = sin(angle). Notez que les fonctions de trig de Java prennent leurs paramètres en tant que radians, vous devez donc convert the angle in degrees appropriately.

Si vous n'avez pas utilisé des matrices avant, cela équivaut aux trois expressions suivantes:

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

Hmm =/juste pour vous renseigner sur ce que je fais, je fais tourner les points sur mes objets hitbox. Après la mise en œuvre de votre équation, tous les objets qui font pivoter mon personnage passent à travers eux. J'ai mis à jour le projet pour montrer mon code –