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?
Code test:
#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);
}
Result:
Init: x= 90, y= 180, z= -270
Final: x= -90, y= -3.41509e-006, z= -90
thank you in advance o/
Have a look at this page. It has everything you need (even some code samples!) for dealing with 3D transformations.
Quaternion to Euler Angles
Euler Angles to Quaternion
All rotation conversions
If you end up needing quaternion's to Euler angles, but you need an arbitrary rotation order, I came across a site with conversion code. Sometimes the trick is just finding the right rotation order. (Btw, the orders that have the same letter twice, like XYX, are proper Euler angles, but the ones like XYZ are Tait-Bryan angles).
Here's the link: http://bediyap.com/programming/convert-quaternion-to-euler-rotations/
And here's the code:
Euler -> Quaternion
Extracted from Three.js.
Here's a piece of code which works for me:
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.