【问题标题】:Java Rotate 3D Vertices around Y axisJava围绕Y轴旋转3D顶点
【发布时间】:2017-03-06 00:04:28
【问题描述】:

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

(仅供参考)这是用于旋转我的碰撞箱上的点。每个“盒子”只是一个三角形,但包裹在一个立方体中,所以我可以检查一个点是否在立方体中。这是针对每个模型的每个三角形完成的。这非常有效,因为我能够穿过带有孔的网格和所有东西。但是,如果应用了任何旋转,就会发生奇怪的事情。

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

【问题讨论】:

  • 将度数转换为弧度。

标签: java math rotation vertices rotational-matrices


【解决方案1】:

将您的分数乘以以下矩阵:

[ 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

【讨论】:

  • Hmm =/ 只是为了让你了解我在做什么,我正在旋转我的对象命中框上的点。实现你的方程后,任何旋转我的角色的物体都会穿过它们。我更新了项目以显示我的代码
猜你喜欢
  • 2011-11-18
  • 1970-01-01
  • 2014-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多