欧拉角到四元数然后四元数到欧拉角

2022-01-16 00:00:00 math rotation c++ quaternions euler-angles

我正在使用 lib glm (http://glm.g-truc.net/) 用于测试四元数,但我有问题;当我将欧拉角转换为四元数然后立即将四元数转换为欧拉角时,我的结果与我最初的欧拉角完全不同.这是正常的吗?会不会是轮换不是交际的?

I'm using lib glm (http://glm.g-truc.net/) for test quaternion but I've a problem; when I convert euler angle to quaternion then immediatly quaternion to euler angles, my result are totally different from my initial euler angles. Is this normal? Could it be because the rotations are not communative?

代码测试:

#include <glmquaternion.hpp>
#include <math.h>

#define PI M_PI
#define RADTODEG(x) ( (x) * 180.0 / PI )
#define DEGTORAD(x) ( (x) * PI / 180.0 )

int         main( void )
{
    float RotX = 90.f;
    float RotY = 180.f;
    float RotZ = -270.f;

    if ( RotX || RotY || RotZ )
    {
        std::cout << "Init: x= " << RotX << ", y= " << RotY << ", z= " << RotZ << "
";
        glm::quat key_quat(glm::detail::tvec3<float>(DEGTORAD( RotX ),
                                                     DEGTORAD( RotY ),
                                                     DEGTORAD( RotZ )));
        glm::detail::tvec3<float> v = glm::eulerAngles(key_quat);

        /*  // the result is even worse with this code here
        RotX = RADTODEG(v.x);
        RotY = RADTODEG(v.y);
        RotZ = RADTODEG(v.z);
        */

        RotX = v.x;
        RotY = v.y;
        RotZ = v.z;

        std::cout << "Final: x= " << RotX << ", y= " << RotY << ", z= " << RotZ << "
";
    }
    return (0);
}

结果:

Init: x= 90, y= 180, z= -270
Final: x= -90, y= -3.41509e-006, z= -90

提前谢谢你o/

推荐答案

是的,很正常.有两种方式可以用欧拉角表示相同的旋转.

Yes, it is normal. There are 2 ways to represent the same rotation with Euler angles.

我个人不喜欢欧拉角,它们会破坏您应用的稳定性.我会避免使用它们.另外,它们也不是很方便.

I personally don't like Euler angles, they mess up the stability of your app. I would avoid them. Plus, they are not very handy either.

相关文章