# Quaternion stuff

This topic is 4793 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys -- I've got a simple quaternion question that I don't see referenced anywhere else. Say I have a camera that is represented by its orientation (quaternion) and a position. I want it to rotate with the mouse: along its X axis with changes in the Y mouse position and along its Y axis as the mouse X position changes. I'm doing something like (mult-quat orientation (axis-angle->quat 1.0 0.0 0.0 mouse-y-delta)) (mult-quat orientation (axis-angle->quat 0.0 1.0 0.0 mouse-x-delta)) It works, but not as I expect: The camera gets rotated around the GLOBAL x/y axes, while I obviously would like it to rotate around its LOCAL x/y axes. What do I need to do? Extract an up/right quaternion from my camera's orientation and use that?

##### Share on other sites
First try multiplying the quaternions in the opposite order. Quaternion multiplication isn't commutative so sometimes you need to swap the order to get the effect you want.

##### Share on other sites
Hmmm, doesn't seem to be doing anything different with the order swapped...

(mult-quat (axis-angle->quat 1.0 0.0 0.0 mouse-y-delta) orientation)
(mult-quat (axis-angle->quat 0.0 1.0 0.0 mouse-x-delta) orientation)

##### Share on other sites
You might post some more code to show the context. If you're constructing your quat from scratch each frame using Euler angles, switching the order may still give the wrong results, just different wrong results. From your original post it sounds like you always want local-axis rotations, which would require maintaining your quat from frame to frame. Another thing you might try is not only switching the order as SiCrane suggested, but also switch the order of the two lines of code.

##### Share on other sites
jyk: I think I'm already doing what you're suggesting. The code looks something like this:

(define orientation (make-quat 0.0 0.0 0.0 1.0)) ;; x/y/z/w

(letrec ((loop
(lambda ()
(set! orientation (mult-quat orientation (mouse-delta->quat mouse-x-delta mouse-y-delta)))
(view-from-quat orientation)
;; other stuff goes here
(loop)))

(define (mouse-delta->quat x y)
(normalize-quat
(mult-quat (axis-angle->quat 0.0 1.0 0.0 (deg->rad x))
(axis-angle->quat 1.0 0.0 0.0 (deg->rad y)))))

In other words, the camera's orientation quaterion is initialized once to (0,0,0,1), and then multiplied by a quaternion constructed by mouse-delta->quat when the mouse moves.

So I guess it sounds like I've got some stupid typo in my math routines, and not a conceptual error or something?

##### Share on other sites
Ooooo, just fixed it: A typo in my quaternion multiplication thing, and on top of that an incorrect multiplication order somewhere. Thanks guys!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634090
• Total Posts
3015432
×