user11914177
user11914177

Reputation: 955

GLM angleAxis wrong?

From tests of quaternions in OpenGL I noticed that rotations on multiple axis are not working how they should. So I wrote a simple program to debug that. This is my program:

glm::quat rotation = glm::angleAxis(glm::radians(45.0f), glm::vec3(1.0f, 1.0f, 0.0f));
glm::vec3 eulerRotation = glm::degrees(glm::eulerAngles(rotation));
printf("X = %f\tY = %f\tZ = %f\n", eulerRotation.x, eulerRotation.y, eulerRotation.z);

From my understanding of rotations this should output:

X = 45.0  Y = 45.0  Z = 0.0

But the program outputs this:

X = 51.589348   Y = 45.000004   Z = 18.939444

I'm using GLM version 0.9.9.5 and C++ 14 So, is my understanding of rotations wrong or is GLM screwing something up?

Upvotes: 4

Views: 7030

Answers (2)

The Axis-Angle Representation and the Euler Angles are two different ways of encoding a rotation. For rotations on canonical axes (X, Y, Z), the representations are very similar and can lead to the false deduction that the conversion is trivial. For example, the Axis-Angle (45,(1,0,0)) is simply the Euler Angles (45,0,0). However, for more general axes, the conversion is not always so obvious.

Adding to the confusion is that the name Euler Vector is used when we create a single 3D vector out of the Axis-Angle representation using the length of the vector to encode the angle of rotation. For example, (45,(1,0,0)) can be encoded as 45*(1,0,0). However, an Euler Vector is not the same as a vector that contains Euler Angles for the same reason that the Axis-Angle representation is different from Euler Angles.

Finally, as stated in another answer by Amadeus, the glm framework requires that the input axis vector is normalized. Normalizing this vector will give the expected result.

Upvotes: 3

Amadeus
Amadeus

Reputation: 10655

From GLM_GTC_quaternion:

glm::angleAxis:

Build a quaternion from an angle and a normalized axis.

Parameters

  • angle Angle expressed in radians if GLM_FORCE_RADIANS is define or degrees otherwise.

  • axis Axis of the quaternion, must be normalized. (emphasis are mine)

You do not have a normalized axis in your snippet

Upvotes: 2

Related Questions