java 3D旋转四元数 [英] java 3D rotation with quaternions

查看:40
本文介绍了java 3D旋转四元数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种使用四元数在 3D 中旋转点的方法,但它似乎无法正常工作:

I have this method for rotating points in 3D using quaternions, but it seems not to work properly:

    public static ArrayList<Float> rotation3D(ArrayList<Float> points, double angle, int xi, int yi, int zi)
{
    ArrayList<Float> newPoints = new ArrayList<>();

    for (int i=0;i<points.size();i+=3)
    {
        float x_old = points.get(i);
        float y_old = points.get(i+1);
        float z_old = points.get(i+2);

        double w = Math.cos(angle/2.0);
        double x = xi*Math.sin(angle/2.0);
        double y = yi*Math.sin(angle/2.0);
        double z = zi*Math.sin(angle/2.0);

        float x_new = (float) ((1 - 2*y*y -2*z*z)*x_old + (2*x*y + 2*w*z)*y_old + (2*x*z-2*w*y)*z_old);
        float y_new = (float) ((2*x*y - 2*w*z)*x_old + (1 - 2*x*x - 2*z*z)*y_old + (2*y*z + 2*w*x)*z_old);
        float z_new = (float) ((2*x*z + 2*w*y)*x_old + (2*y*z - 2*w*x)*y_old + (1 - 2*x*x - 2*y*y)*z_old);

        newPoints.add(x_new);
        newPoints.add(y_new);
        newPoints.add(z_new);
    }

    return newPoints;
}

如果我打这个电话 rotation3D(list, Math.toRadians(90), 0, 1, 0); 其中 points(0,0,10),输出是(-10.0, 0.0, 2.220446E-15),但应该是(-10,0,0),对?有人可以看看我的代码并告诉我是否有什么问题吗?

If i make this call rotation3D(list, Math.toRadians(90), 0, 1, 0); where points is (0,0,10), the output is (-10.0, 0.0, 2.220446E-15), but it should be (-10,0,0), right? Could someone take a look at my code and tell me if is there somethig wrong?

这里有 4 个屏幕代表我的对象的初始位置,3 次旋转 -90 度(对象没有正确绘制,这是 GL 问题,我稍后会处理):

Here are 4 screens that represent the initial position of my object, and 3 rotations with -90 degrees (the object is not properly painted, that's a GL issue, that i will work on later):

推荐答案

我没有研究过代码,但你从中得到的是正确的:假设一个左手坐标系,当你旋转点 (0,0,10) 围绕 y 轴(即 (0,1,0)) 90 度,最终得到 (-10,0,0).

I haven't studied the code but what you get from it is correct: Assuming a left-handed coordinate system, when you rotate the point (0,0,10) 90 degrees around the y-axis (i.e. (0,1,0)) you end up with (-10,0,0).

如果您的坐标系是右手坐标系,我认为您必须反转角度的符号.

If your coordinate system is right-handed I think you have to reverse the sign of the angle.

这篇关于java 3D旋转四元数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆