PDA

View Full Version : [glm] Use quaterions to move the camera direction according to mouse movement



mamo139
09-23-2013, 02:22 PM
I am trying to use quaterions to move the camera direction vector in the following way.

This code is perfectly working


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

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.

Why I cannot multiply quaterions successfully? Am I using GLM in a wrong way?

nickels
09-25-2013, 11:36 AM
Try:



glm::quat temp3 = glm::normalize(temp2 * temp1);


(ab)^-1=(ab)*/||ab||^2=b*a*/||ab||^2
which is not equal to
b^-1 a^-1 = b*a* /(||b||^2||a||^2) unless when ||ab||=||a||=||b||=1

nickels
09-25-2013, 11:57 AM
Try:



glm::quat temp3 = glm::normalize(temp2 * temp1);


(ab)^-1=(ab)*/||ab||^2=b*a*/||ab||^2
which is not equal to
b^-1 a^-1 = b*a* /(||b||^2||a||^2) unless when ||ab||=||a||=||b||=1

Actually, I think this is wrong since ||ab||^2=(ab)(ab)*=abb*a*=-aba*b*=--aa*bb*=||a||^2||b||^2=1x1=1

nickels
09-25-2013, 12:05 PM
I would make sure that Camera1.SetCameraDirection takes a const reference to a quaternion and that Camera1.GetCameraDirection() returns a quaternion by value (weird things could happen if it returns a reference...).
No more ideas....

Groovounet
10-02-2013, 02:59 PM
I would make sure that Camera1.SetCameraDirection takes a const reference to a quaternion and that Camera1.GetCameraDirection() returns a quaternion by value (weird things could happen if it returns a reference...).
No more ideas....

The issue was that mamo had wrong expectation for the quaternion constructors. It doesn't take an axis and an angle but there is a function for that called angleAxis.