Java围绕Y轴旋转3D顶点

时间:2017-03-06 00:04:29

标签: java math rotation vertices rotational-matrices

我有一个3D模型,我需要围绕Y轴旋转它的顶点(轴在我的情况下直线向上)。例如,假设我有顶点 (3,2,3)(x,y,z)当我围绕Y轴旋转时,只有x和z会改变。我如何使用度数在java中实现这个?提前谢谢!

(FYI)这是用于旋转我的hitbox上的点。每个“盒子”只是一个三角形,但包裹在一个立方体中,所以我可以检查一个点是否在立方体中。每个模型按三角形完成。这非常有效,因为我可以通过网格中的洞和一切来完成。但是,如果应用任何旋转,奇怪的事情就会开始发生。

编辑:这是我使用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;
}

1 个答案:

答案 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