# Quaternions step by step

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

## Recommended Posts

##### Share on other sites
Quote:
May I ask, if you already have it working with matrices, why do you want to introduce quaternions?

because i want

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by nebemoreThat'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.

##### Share on other sites
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.

##### Share on other sites

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.

cheers

##### Share on other sites
does nobody know how to use 'correct' quaternions? I'm already desperate

##### Share on other sites
Have you read this? Sorry I can't answer your question directly, I haven't gotten around to learning those buggers myself.

##### Share on other sites
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.

1. 1
2. 2
Rutin
21
3. 3
4. 4
A4L
15
5. 5
khawk
14

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

• Total Topics
633737
• Total Posts
3013612
×