问题描述
我正在使用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 <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/
推荐答案
是的,这很正常. 有2种方法用欧拉角表示相同的旋转
Yes, it is normal. There are 2 ways to represent the same rotation with Euler angles.
我个人不喜欢Euler角,它们会破坏应用程序的稳定性.我会避免使用它们.另外,它们也不是很方便.
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.
这篇关于欧拉角到四元数,然后四元数到欧拉角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!