Quaternions step by step

Started by
17 comments, last by Dmytry 17 years, 6 months ago
hi, I've spent the last 9 hours in the world of Quaternions. There are so many Threads and after reading about 100 of them, i have to say that i still don't know how to use them(yes, I feel ashamed). So i don't want to write 3 pages about what I already know about Quaternions and what not. I don't want also to post a code. Let's say i have everything I need, a Quaternion class with all needed member functions, very usefull ODE-functions that handle Quaternions and so on... What I have: 1. a rigid body in the world 2. position and rotation matrix of the body 3. center of mass My Goal: 1. roll/pitch/yaw the body around his point of reference(here the mass center) note: i've already implemented all roll/yaw/pitch-functions and key-controls What I need: 1. someone to tell me step-by-step what i have to do. no code, just words or pseudo-code. i need just to understand it. for all that aren't big fans of Quaternions: i have already made this body rotating with matrix, so please talk only about Quats. Thanks a lot in advance!
Advertisement
Quote:i have already made this body rotating with matrix, so please talk only about Quats.
May I ask, if you already have it working with matrices, why do you want to introduce quaternions?
because i want
That's one good reason:
"A matrix product requires many more operations than a quaternion product so we can save a lot of time and preserve more numerical accuracy with quaternions than with matrices."

And another one: I'm implementing a body that moves and rotates around and has a camera inside(so the object is transparent). to manipulate both of them (body & cam) i think using quaternions is better idea that with matrices.

cheers
Quote:Original post by nebemore
That's one good reason:
"A matrix product requires many more operations than a quaternion product so we can save a lot of time and preserve more numerical accuracy with quaternions than with matrices."
Well, I'm not trying to avoid your question of how to use quaternions in this context, but I guess I am playing devil's advocate a bit. Regarding the above observation, the fact that quaternion concatenation is a bit more efficient really only matters (IMO) in a context where you're performing many, many concatenations per frame. The classic example of where quaternions can really pay off is in animation using hierarchical models or keyframe interpolation; in this context the more efficient concatenation and interpolation and minimal storage can be a win. There are other situations though where (again, IMO) matrices are a perfectly suitable solution.
Quote:And another one: I'm implementing a body that moves and rotates around and has a camera inside(so the object is transparent). to manipulate both of them (body & cam) i think using quaternions is better idea that with matrices.
This doesn't make much sense to me. Why would using a quaternion be better for a transparent object, a camera, manipulating two objects at once, or any combination of the above?

The reason I question the switch to quats here is that (or so I observe) there is a lot of misinformation and misunderstanding about quaternions, matrices, other rotation representations, and their respective characteristics. Quite often people seem to think quaternions are a better (or the only) solution to a problem, when in fact rotation matrices would work just fine.

Anyway, to get back to your original question, can you be more specific? It'd be easier to provide information if we knew exactly what aspect of using quaternions in this context is giving you trouble.
What about for a trailing camera that is "springy". Wouldn't interpolations between the targets location and the camera be best handled using a quaternion? From what I understood interpolation is the quaternion's strong point.

nebemore: Have you looked at gamedevs articles on Quaternions? There is plenty of information on them. Take a look.

There isn't, unfortunately, that great of a step by step walkthrough for understanding the math behind them but there are articles that walk you through implimenting them in code.
thanks for the answers!

i'll try to be more specific:

first let forget the camera and concentrate only on the object in the space.

i want to yaw,pitch and roll the body in the space using control-keys.

let's say the body lies at point (x,y,z) with z positive as Up.

1. At the begin the body has a init orentation. Like I said, i'm using ODE for the physics. There is the function dBodyGetRotation that gives me the start orientation of the body. Another function dRtoQ transforms the initial Rotation Matrix in Quaternion. Good, now i have the initial Quaternion. let name it Qstart.

2. Let take the formula Vnew= q ∗ Vold ∗ q∗, where q is the desired rotation quaternion and q* = q conjugate. The first Question here is: what is Vold? Is Vold=Qstart? (you can correct me anytime)

3. The next is to determine what is q. Now i'm taking the function dQFromAxisAndAngle(dQuaternion q, dReal ax, dReal ay, dReal az,
dReal angle)
.
This function computes q as a rotation of angle radians along the axis (ax,ay,az). so if i want to yaw the body it will look like:
dQFromAxisAndAngle(yawQuat, 0, 0, 1, rotAngle),where rotAngle is set by pushing a key and yawQuat the quatenion to store the result. Is the setting of the ax,ay,az correct?

4. So if everything till now is correct(I doubt). what is when i have 3 quaternions - yawQuat, rollQuat and pitchQuat? Should I first multiply all three of them and store in another Quaternion. Let say
Qtemp = rollQuat*pitchQuat*yawQuat. Is the order correct?
Is now the formula correct Vnew= Qtemp ∗ Vold ∗ Qtemp∗ ?

5. Transforming of the rotation matrix into quaternion is necessary only at the begin. After that by every key-input i have to take the actuall rotation Quaternion (Vold(frame t)=Vnew(frame t-1)). Am I right?

6. Is Vnew in every frame the new rotation of the body?

so, there are almost all my questions about quaternions.
Thanks for reading, advices and of course for your corections.

cheers
does nobody know how to use 'correct' quaternions? I'm already desperate
Have you read this? Sorry I can't answer your question directly, I haven't gotten around to learning those buggers myself.
IMO the easiest way to get a grip on quaternions is just to think of them as a drop-in substitute for rotation matrices. Every operation you would perform with rotation matrices - construction from an axis-angle pair or Euler angle triple, concatenation, normalization, normalized linear interpolation, spherical linear interpolation, transformation of vectors - can also be done with quaternions. If you have working code that uses rotation matrices, the same code (perhaps with some slight changes in syntax) will work with quaternions as well.

One potential gotcha is that the concatenation order for your matrices and quaternions must match. If your matrices are configured for column vectors, you'll want to use the standard mathematical definition for quaternion multiplication, and the standard formula for rotating a vector by a quat. If your matrices are configured for row vectors, you'll want to use the 'reverse' form for quat multiplication (wherein the sign of the cross-product term is negated), and also the reverse of the vector rotation formula (just transpose the terms).

Rotation matrices and quaternions each have particular strengths and weaknesses with respect to the aforementioned common operations. The two forms can be freely converted back and forth, so if one or the other representation is more suitable for a particular purpose (such as quaternions and interpolation), you can always convert to that form when needed.

This topic is closed to new replies.

Advertisement