Quaternion stuff

Started by
4 comments, last by IdiotFace 18 years, 4 months ago
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?
Advertisement
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.
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)
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.
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?
Ooooo, just fixed it: A typo in my quaternion multiplication thing, and on top of that an incorrect multiplication order somewhere. Thanks guys!

This topic is closed to new replies.

Advertisement