Oliver Marsh
195 posts / 1 project
Olster1.github.io
Using Quaternions for camera controller
Edited by Oliver Marsh on Reason: Initial post
Hi,

I'm making a fps style camera in a 3d game and was learning about Quaternions. I just want the camera to have pitch and heading, but no bank. And not track euler angles over multiple frames. Whats the best way to do this? The code I have looks like this.

  1 2 3 4 5 6 7 8 9 10 11 //Get the change in mouse position V2 diff = normalize(v2_minus(mouseP, state->lastMouseP)); float sensitivity = 3; float angleX = sensitivity*diff.y*dt; float angleY = -sensitivity*diff.x*dt; Quaternion rot = eulerAnglesToQuaternion(angleY, angleX, 0); //rotate the camera orientation by the change in rotation camera.orientation = quaternion_mult(camera.orientation, rot); 

I was wondering if this looks correct and is it equivalent to the below code, which is tracking Euler angles over frames.

  1 2 3 4 5 6 7 8 9 10 11  static V2 eulerXY = {}; float sensitivity = 3; float angleX = sensitivity*diff.y*dt; float angleY = -sensitivity*diff.x*dt; eulerXY.x += angleX; eulerXY.y += angleY; camera.orientation = eulerAnglesToQuaternion(eulerXY.y, eulerXY.x, 0); 

Also in the first code snippet, is:
 1 camera.orientation = quaternion_mult(camera.orientation, rot); 

the same as:
 1 camera.orientation = quaternion_mult(rot, camera.orientation); 

Thanks for your help, any info about the best way to use Quaternions would be great.
Simon Anciaux
1356 posts
Using Quaternions for camera controller
I can't help you much but according to wikipedia, quaternion multiplication isn't commutative.
Martin Fouilleul
39 posts / 3 projects
PhD student at Ircam, doing research on programming languages for temporal interaction. Former sound engineer and computer music designer.
Using Quaternions for camera controller
Hello Oliver,

Imagine your heading is zero and your pitch is 45 degrees. Now you move the mouse to the left to produce a 90 degrees rotation.

So maybe I'm wrong but I think your two snippets are not equivalents. You probably want the second one, because a change in heading always produce a rotation around a vertical axis, whereas your first snippet a change in heading will produce a rotation around an axis which depends on your previous pitch/axis angles.

Martin Fouilleul
39 posts / 3 projects
PhD student at Ircam, doing research on programming languages for temporal interaction. Former sound engineer and computer music designer.
Using Quaternions for camera controller
Also, as Simon said, quaternion multiplication is not commutative, since rotations of the unit sphere are not.

For instance, if you reverse my previous example and first rotate 90 degrees to the left, then 45 degrees up, you will end up with a different orientation (ie, heading = 90, pitch = 45, bank = 0).
Oliver Marsh
195 posts / 1 project
Olster1.github.io
Using Quaternions for camera controller
Edited by Oliver Marsh on
Thanks Martin and Simon,
Yes I think I understand a bit better. The banking is what prompted the question, the camera seemed to introduce bank after looking around, and I couldn't understand why. Could you say the tracking euler angles method has knowledge of the original 'correct' orientation, i.e. looking down the z axis (since we only ever rotate once from this orientation), whereas the first snippet only knows of it's previous frame orientation?

Thanks, Oliver
Martin Fouilleul
39 posts / 3 projects
PhD student at Ircam, doing research on programming languages for temporal interaction. Former sound engineer and computer music designer.
Using Quaternions for camera controller
Yes you could put it that way.
In the euler angles method you always rotate from an original (x,y,z) basis.
In the quaternion multiplication method, you rotate a basis attached to the camera itself, so the next rotation will happen relative to that new basis, and so on.