# Why do these quaterion multiplications produce different results?

I am trying to use quaterions to modify the camera direction vector.

This code works perfectly:

glm::quat temp1 = glm::normalize( glm::quat((GLfloat)( -Input1.MouseMove.x  * mouse_sens * time_step), glm::vec3(0.0, 1.0, 0.0)) );
glm::quat temp2 = glm::normalize( glm::quat((GLfloat)( -Input1.MouseMove.y  * mouse_sens * time_step), dir_norm) );



This code does not:

glm::quat temp1 = glm::normalize( glm::quat((GLfloat)( -Input1.MouseMove.x  * mouse_sens * time_step), glm::vec3(0.0, 1.0, 0.0)) );
glm::quat temp2 = glm::normalize( glm::quat((GLfloat)( -Input1.MouseMove.y  * mouse_sens * time_step), dir_norm) );

glm::quat temp3 = temp2 * temp1;


Why can I not multiply quaterions successfully? Am I using GLM wrong?

• Sep 22, 2013 at 11:51
• How is it not working? Sep 23, 2013 at 16:06
• The two pieces of code, from my understanding of glm, should produce the same result. However, they are not. The first piece of code produce expected result. In the second piece of code when i move the mouse I get extremely small movements in an apparently random direction. Sep 23, 2013 at 19:24
• Have you tried flipping temp2 and temp1 ? Don't forget that quaternion multiplication is not commutative*( AB != B*A ). Dec 24, 2013 at 1:43
• Try to normalize your quaternions after each multiplication. Mar 6, 2014 at 16:03

t2 * (t1 * direction * inverse(t1)) * inverse(t2)

t3 * direction * inverse(t3)

Given that t3 = t2 * t1

It's (t2 * t1) * direction * inverse(t2 * t1)

As far as my knowledge of Quaternion multiplication goes, I don't think t2 * (t1 * direction * inverse(t1)) * inverse(t2) and (t2 * t1) * direction * inverse(t2 * t1) should yield the same result.

• To be more specific, this isn't about the multiplication of quaternions, but the action of quaternions on a vector. It's very possible that glm treats q*v as either (vq) or even (q^-1vq) - that is, that it applies its action on the other side. In these cases, it's easy to see why q1*(q2*v) = v(q2q1) would be different from (q1*q2)*v = v(q1q2). Oct 3, 2014 at 22:08
• I suppose this question deserves an answer from someone more familiar to GLM vectors and quats, not someone who's more used to XMVector3Rotates.
– user5665
Oct 4, 2014 at 0:43
• Matt: agreed; sadly, I know my quaternions in the abstract, but not in this specific implementation. Oct 4, 2014 at 0:47