Euler angle to Quaternion then Quaternion to euler angle

user1466739 picture user1466739 · Jun 19, 2012 · Viewed 40.9k times · Source

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/

Answer

Ali picture Ali · Jun 19, 2012

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.