# 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) );

Camera1.SetCameraDirection(temp2 * (temp1 * Camera1.GetCameraDirection() * glm::inverse(temp1)) * glm::inverse(temp2));


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;
Camera1.SetCameraDirection(temp3 * Camera1.GetCameraDirection() * glm::inverse(temp3));


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

• – danijar Sep 22 '13 at 11:51
• How is it not working? – ltjax Sep 23 '13 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. – Marco Sep 23 '13 at 19:24
• Have you tried flipping temp2 and temp1 ? Don't forget that quaternion multiplication is not commutative*( AB != B*A ). – akaltar Dec 24 '13 at 1:43
• Try to normalize your quaternions after each multiplication. – kolenda Mar 6 '14 at 16:03

## 1 Answer

Your top code chunk is:

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

Your bottom chunk is:

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). – Steven Stadnicki Oct 3 '14 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 '14 at 0:43
• Matt: agreed; sadly, I know my quaternions in the abstract, but not in this specific implementation. – Steven Stadnicki Oct 4 '14 at 0:47