四元数滚动俯仰偏航

时间:2013-10-23 01:24:36

标签: quaternions

我在哪里可以找到一个函数(最好用php编写),它将四元数旋转转换为3D旋转(Roll Pitch Yaw)并且还解决了万向节锁定问题(在万向节锁角处没有返回NaN)。感谢

1 个答案:

答案 0 :(得分:0)

这不是PHP,而是C ++。它应该很容易转移到PHP。可以看到无效/万向节情况,其中rotateX,rotateY或rotateZ未被指定,因为该值为NaN。

        //Our Quaternion is defined by qx,qy,qz,qw
        double qx;
        double qy;
        double qz;
        double qw;          

        double rotateXa0 = 2.0*(qy*qz + qw*qx);
        double rotateXa1 = qw*qw - qx*qx - qy*qy + qz*qz;
        double rotateX = 0.0;
        if (rotateXa0 != 0.0 && rotateXa1 != 0.0) 
            rotateX = atan2(rotateXa0, rotateXa1);

        double rotateYa0 = -2.0*(qx*qz - qw*qy);
        double rotateY = 0.0;
        if( rotateYa0 >= 1.0 )
            rotateY = M_PI/2.0;
        else if( rotateYa0 <= -1.0 )
            rotateY = -M_PI/2.0;
        else rotateY = asin(rotateYa0);

        double rotateZa0 = 2.0*(qx*qy + qw*qz);
        double rotateZa1 = qw*qw + qx*qx - qy*qy - qz*qz;
        double rotateZ = 0.0;
        if (rotateZa0 != 0.0 && rotateZa1 != 0.0)
            rotateZ = atan2(rotateZa0, rotateZa1);