4
\$\begingroup\$

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?

\$\endgroup\$
6
  • \$\begingroup\$ related gamedev.stackexchange.com/questions/53723/… \$\endgroup\$
    – danijar
    Sep 22, 2013 at 11:51
  • 1
    \$\begingroup\$ How is it not working? \$\endgroup\$
    – ltjax
    Sep 23, 2013 at 16:06
  • \$\begingroup\$ 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. \$\endgroup\$
    – Marco
    Sep 23, 2013 at 19:24
  • 2
    \$\begingroup\$ Have you tried flipping temp2 and temp1 ? Don't forget that quaternion multiplication is not commutative*( AB != B*A ). \$\endgroup\$
    – akaltar
    Dec 24, 2013 at 1:43
  • \$\begingroup\$ Try to normalize your quaternions after each multiplication. \$\endgroup\$
    – kolenda
    Mar 6, 2014 at 16:03

1 Answer 1

2
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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). \$\endgroup\$ Oct 3, 2014 at 22:08
  • \$\begingroup\$ I suppose this question deserves an answer from someone more familiar to GLM vectors and quats, not someone who's more used to XMVector3Rotates. \$\endgroup\$
    – user5665
    Oct 4, 2014 at 0:43
  • \$\begingroup\$ Matt: agreed; sadly, I know my quaternions in the abstract, but not in this specific implementation. \$\endgroup\$ Oct 4, 2014 at 0:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .