JavaFX 8 转换为俯仰、偏航和滚动旋转角度 [英] JavaFX 8 Transform to pitch, yaw and roll rotation angles

查看:36
本文介绍了JavaFX 8 转换为俯仰、偏航和滚动旋转角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实施 this 中的答案线程 我有这段代码将 deltayaw、deltaroll 和 deltapitch 角度转换为一个角度并围绕它旋转一个节点.作为参数的角度是角度的瞬时变化,因为给出整个角度将忽略方向的变化.

Implementing the answer from this thread I have this code that translates the deltayaw, deltaroll and deltapitch angles into one angle and rotates a node around it. The angles that are taken as the parameters are momentary changes of angles since giving the whole angles would ignore the changes in orientation.

    public static void matrixRotate(Group n, double deltaroll, double deltapitch, double deltayaw){    
    double A11 = Math.cos(deltaroll)*Math.cos(deltayaw);
    double A12 = Math.cos(deltapitch)*Math.sin(deltaroll)+Math.cos(deltaroll)*Math.sin(deltapitch)*Math.sin(deltayaw);
    double A13 = Math.sin(deltaroll)*Math.sin(deltapitch)-Math.cos(deltaroll)*Math.cos(deltapitch)*Math.sin(deltayaw);
    double A21 =-Math.cos(deltayaw)*Math.sin(deltaroll);
    double A22 = Math.cos(deltaroll)*Math.cos(deltapitch)-Math.sin(deltaroll)*Math.sin(deltapitch)*Math.sin(deltayaw);
    double A23 = Math.cos(deltaroll)*Math.sin(deltapitch)+Math.cos(deltapitch)*Math.sin(deltaroll)*Math.sin(deltayaw);
    double A31 = Math.sin(deltayaw);
    double A32 =-Math.cos(deltayaw)*Math.sin(deltapitch);
    double A33 = Math.cos(deltapitch)*Math.cos(deltayaw);

    double d = Math.acos((A11+A22+A33-1d)/2d);
    if(d!=0d){
        double den=2d*Math.sin(d);
        Point3D p= new Point3D((A32-A23)/den,(A13-A31)/den,(A21-A12)/den);
        Rotate r = new Rotate();
        r.setAxis(p);
        r.setAngle(Math.toDegrees(d));
        n.getTransforms().add(r);
        Transform all = n.getLocalToSceneTransform();
        n.getTransforms().clear();
        n.getTransforms().add(all);               
    }
}

(我使用旋转是因为我需要始终围绕原点而不是中心旋转对象)

(I'm using rotate because I need to always rotate the object around the origin, not the center)

现在这产生了一个问题,因为我不再能够获得实际的俯仰、滚转和偏航角.

Now this creates a problem as I'm no longer able to get the actual pitch, roll and yaw angles.

我曾经像这样跟踪它们(没有考虑到不断变化的方向):

I used to keep track of them like this (which doesn't take into account the changing orientation):

roll +=deltaroll;
pitch += deltapitch;
yaw += deltayaw;

后来我想出了这个,它更准确一点,但是如果没有直接修改角度(在 n.getTransforms().add(all) 之后插入),则不会跟踪发生的变化在主要代码段中):

And later I've come up with this, which is a bit more accurate, but doesn't track the changes that occur if the angles are not directly modified(inserted after the n.getTransforms().add(all) in the main snippet):

roll+= Math.toDegrees(d)*((A32-A23)/den);
pitch += Math.toDegrees(d)*((A13-A31)/den);
yaw += Math.toDegrees(d)*((A21-A12)/den);  

我一直在寻找解决方案并找到了 这个答案 应该给出最终变换的角度,但我无法让它适用于所有角度.

I've been searching around for solutions and found this answer which is supposed to give the angle from the final transform but I haven't been able to get it working for all angles.

double xx = n.getLocalToSceneTransform().getMxx();
double xy = n.getLocalToSceneTransform().getMxy();
double roll = Math.atan2(-xy, xx);

我再次尝试获得的是相对于场景坐标系统的完整角度(由不同方向的 delta 角度进行的变换合成).我真的很不擅长这个,所以所有的帮助都会很棒.

Again what I'm trying to get are the full angles (composited out of the transforms made from the delta angles in different orientations) relative to the scene's coodrdinate system. I'm really bad at this so all help would be great.

推荐答案

如果你想在多次旋转后得到任意阶段的俯仰、偏航和滚转角,可以从3D模型的变换矩阵中得到.

If you want to get the pitch, yaw and roll angles at any stage after several rotations, you can get them from the transformation matrix of the 3D model.

如果你看看几次旋转后的变换矩阵:

If you have a look at the transformation matrix after several rotations:

Transform T = model3D.getLocalToSceneTransform();
System.out.println(T);

你会看到这样的:

Transform [
        0.9034731871219395, -0.4260296991535005, -0.04727468234587054, 1.4044414829046357
        0.3743586809560477, 0.837958815679334, -0.39709016761704913, 0.5234811188037405
        0.2087864414768669, 0.3410626315861443, 0.9165612381019399, -1.1277640590168572
    ]

如果你想要角度,你只需要比较这个矩阵和这个答案:

If you want the angles, you just need to compare this matrix with this one from this answer:

正如您已经说过的,要获得滚动角度,您可以使用 T.getMxx()T.getMyx():

As you have already stated, to get the roll angle you can use T.getMxx() and T.getMyx():

double roll = Math.atan2(-T.getMyx(),T.getMxx());

现在,对于音高,您可以以相同的方式使用 T.getMzy()T.getMzz():

Now, for the pitch, you can use T.getMzy() and T.getMzz() in the same way:

double pitch = Math.atan2(-T.getMzy(),T.getMzz());

最后,对于偏航,使用 T.getMzx()T.getMzy()T.getMzz():

Finally, for the yaw, use T.getMzx(), T.getMzy() and T.getMzz():

double yaw = Math.atan2(T.getMzx(),Math.sqrt(T.getMzy()*T.getMzy()+T.getMzz()*T.getMzz()));

这将为上述矩阵提供您正在寻找的角度(以弧度为单位):

This will give for the above matrix the angles you are looking for (in radians):

roll: -0.39281984604895126
pitch: -0.356235553820928
yaw: 0.21033388848106072

这篇关于JavaFX 8 转换为俯仰、偏航和滚动旋转角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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