Sign in to follow this  
IdiotFace

Quaternion stuff

Recommended Posts

IdiotFace    122
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 this post


Link to post
Share on other sites
SiCrane    11839
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 this post


Link to post
Share on other sites
IdiotFace    122
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 this post


Link to post
Share on other sites
jyk    2094
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 this post


Link to post
Share on other sites
IdiotFace    122
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this