-1

I have a 3D model and I need to rotate its vertices around the Y axis (The axis going straight up in my case). For example lets say i have the vert (3,2,3)(x,y,z) and when i rotate around the Y axis only the x and z's will change. how could I implement this in java using degrees? Thanks in advance!

(FYI) this is for rotating the points on my hitbox. Each "box" is just a triangle but wrapped in a cube so i can just check if a point is in the cube. This is done per triangle per model. This works perfectly because im able to walk through meshes with holes in them and everything. However, if any rotation is applied weird things start to happen.

Edit: here is my code using Andys method

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;
}

1 Answers1

1

Multiply your points by the following matrix:

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

i.e.

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

where (x, y, z) are your original coordinates, (newx, newy, newz) are your rotated coordinates, and c = cos(angle) and s = sin(angle). Note that Java's trig functions take their parameters as radians, so you need to convert the angle in degrees appropriately.

If you've not used matrices before, this is equivalent to the following three expressions:

newx = c * x - s * z
newy = y
newz = s * x + c * z
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Hmm =/ just to fill you in on what im doing, I am rotating the points on my objects hitbox. After implementing your equation, any objects that are rotated my character walks right through them. I updated the project to show my code – Brian Baker Mar 06 '17 at 03:24