//Create change quaternions for each axis Quaternion xOffset = Quaternion(); xOffset.FromAxisAngle(Rotation.x * m_TurnSpeed, 1.0, 0.0, 0.0); Quaternion yOffset = Quaternion(); yOffset.FromAxisAngle(Rotation.y * m_TurnSpeed, 0.0, 1.0, 0.0); Quaternion zOffset = Quaternion(); zOffset.FromAxisAngle(Rotation.z * m_TurnSpeed, 0.0, 0.0, 1.0); //Multiply the change quats by the current quat m_Orientation = yOffset * zOffset * xOffset * m_Orientation; //Get matrix m_Orientation.Normalize(); m_Orientation.RotationMatrix(m_RotationMatrix); Translation = m_Orientation.MultVect(Translation); m_Position.x += (Translation.x * m_MoveSpeed); m_Position.y += (Translation.y * m_MoveSpeed); m_Position.z += (Translation.z * m_MoveSpeed);
I apply m_RotationMatrix using glMultMatrix(), and m_Position using glTranslatef().
This works fine for objects rotating and translating about, but when I apply this to my camera, obviously things don't work. Now, I say obviously, because as far as I know, if I want to change the direction I'm "looking at" in OpenGL, I need to apply the reverse of the changes I'd apply to an object. However, I'm not quite sure how to do this, and all my attempts have failed.
Leaving everything exactly the same, I find rotations don't add up and translations are backwards or inverted. Inverting the input doesn't help.
I believe that using
m_Orientation = m_Orientation * yOffset * zOffset * xOffset;
Instead of the previous form is appropriate for the camera, and this is enough to fix all the rotation issues, but I simply can't manage to get translations to work after that. I can get close by adding the X and Y changes to the position normally and then subtracting the Z change from the position, but this still leads to wonky behavior when moving and rotating around the Z axis at the same time, and the object ends off spiraling away from the point it is supposed to be pointing towards.
So, any tips on how to adapt the above behavior for proper use as a camera?
Also, when applying a camera to OpenGL, to I need to use glPushMatrix() and glPopMatrix()? I ask because that helped some tests run better than others, but it doesn't seem to make sense to do so, since those are basically for pushing a new local space for transformations onto the stack (I'm horribly mangling my terminology, I'm sure).
Edited by Saldan, 20 May 2012 - 11:29 PM.