c++ - 欧拉角到四元数然后四元数到欧拉角

标签 c++ math rotation quaternions euler-angles

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

代码测试:

#include <glm\quaternion.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 << "\n";
        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 << "\n";
    }
    return (0);
}

结果:

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

提前谢谢你o/

最佳答案

是的,这很正常。 There are 2 ways用欧拉角表示相同的旋转。

我个人不喜欢欧拉角,they mess up the stability of your app.我会避开他们。另外,他们是not very handy要么。

关于c++ - 欧拉角到四元数然后四元数到欧拉角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11103683/

相关文章:

javascript - 找出集合中的哪种数字组合总计达到给定的总计?

math - 从 latex 数学公式到大 jpeg 文件

c++ - 为什么我们需要分开编译和链接?

c++ - Xcode (4.3.2) 与从端口安装的 openCV (2.3.1) 出现问题

c++ - 如何将5个值的数组1D打印为列?

math - 点在圆中的位置

c++ - 跳过 If 语句中的语句

ios - 保存和恢复 UIImageView 的旋转和大小

opengl - 四元数数学旋转?

javascript - jquery绕x y轴旋转矩形