How do quaternions work?

Started by
20 comments, last by Boder 16 years, 1 month ago
Hey all, Does anyone have a link to a tutorial on Quaternions for idiots? Thanks!
Advertisement
This is the one I know of
Idiots can learn how to use quaternions, but will not be capable of learning how they work. People who aren't idiots, but simply aren't yet well-informed about quaternions, will get a lot from this paper.
Thanks, Guys - Both of those links help. However, I am still a little bit lost. To be clear, I'm not really an idiot, but I have had a very difficult time visualizing quaternions. I'm trying to read that 2nd paper, but it's quite complex and requires a lot of math. I guess it must just be a very complex subject, but is there an example of anyone that shows the absolute basics of how quaternions can be used to make a rotation?
Quote:Original post by The Steve
Thanks, Guys - Both of those links help. However, I am still a little bit lost. To be clear, I'm not really an idiot, but I have had a very difficult time visualizing quaternions. I'm trying to read that 2nd paper, but it's quite complex and requires a lot of math. I guess it must just be a very complex subject, but is there an example of anyone that shows the absolute basics of how quaternions can be used to make a rotation?
The usage of quaternions with respect to rotations is pretty well-documented.

There are a lot of articles and tutorials available online. Caveat emptor, though - a lot of these contain misleading or simply incorrect information. For that reason, I'd stick to reputable sources if possible (Eberly, Shoemake, and Jason Schenkel [sp?] come to mind - the latter contributed some good articles on the subject to GPG I or II, I believe).

You don't need to have a deep understanding of the math behind quaternions to use them effectively (although it's good to have an appreciation - or at least an awareness - of it).

As a way to represent and manipulate rotations, quaternions are fairly straightforward. As I'm sure you know, a quaternion has four elements, usually referred to as x, y, z, and w. x, y, and z taken together form the 'vector part', while w is the 'scalar part'.

We can encode an axis-angle rotation in a (unit-length) quaternion as follows:
q.v = (axis/|axis|)*sin(angle/2)q.w = cos(angle/2)
From there, pretty much everything about how we use quaternions in the context of rotation falls out from the following equation:
v' = q*v*q-1
In this equation, q is a quaternion that encodes an axis-angle rotation (as shown above), v is a vector encoded as a quaternion (the vector is assigned to the vector part of the quaternion, with the real part - w - taking on an arbitrary value, usually zero), and v' is the vector (again, in quaternion form) after rotation.

It's also worth noting that for unit-length quaternions the inverse is equivalent to the conjugate, and that the equation as presented above assumes the 'mathematically correct' definition of quaternion multiplication (a 'reversed' form is also used sometimes, but we'll leave that aside for now).

The equation can be shown to be correct in various ways; Schenkel and Shoemake each have shown how the equation can be derived in various papers. (That said, unless you're particularly curious, you can just take it for granted that the correctness of the equation can be proved.)

From this equation, we can also show fairly easily that the product of two quaternions represents an axis-angle rotation that is equivalent to the rotations represented by the two quaternions applied in sequence. In this way, quaternions function much as rotation matrices do (that is, through them, rotations can be combined through concatenation).

Although you can rotate vectors 'manually' via the above equation, in practice it's seldom done this way (for a variety of reasons). Instead, the quaternion is usually converted to matrix form, and the matrix is then used to apply the transformation.

The algorithm for converting a quaternion to a matrix is well-documented, and can be derived in a number of different ways if one is interested. When consulting references on the subject, just be aware that the form of the resulting matrix depends on vector notation convention (row or column).

Ultimately, quaternions are more or less functionally equivalent to 3x3 matrices in terms of their behavior with respect to rotations. The differences between quaternions and matrices in this context basically have to do with storage requirements and run-time efficiency (and, to a lesser extent, the elegance and/or stability of some of the algorithms involved).

My advice regarding quaternions is, in general, to use matrices instead unless you have a specific reason to use quaternions, and can articulate that reason clearly (even if only to yourself).

Hm, I hadn't intended to write this long of a post, but in any case, I hope the information contained therein will be helpful to you.

[Edited by - jyk on February 20, 2008 1:20:56 PM]
Wow - that was one seriously awesome explanation and was basically what I needed to hear. Thanks JYK, you're awesome.

The main reason behind my desire to understand quaternions (besides wanton self punishment) is that I've been trying to understand the Quake 3 engine from the ground up and Carmack makes use of a lot of quaternions there.

My programming desires right now are to create a simple engine that can load a 3DS model on the fly, do some simple lighting, and then later add some arbitrary bot/creature (even a sphere) to the environment that moves around in random motion and uses collision detection to determine what walls/objects it would bump into.

Unfortunately, the greatest problem I've run into with my understanding of other 3D engines is the math portion. I run into quaternions and say "what the heck?"
If you're still looking for more: Quaternions on GPWiki

Maybe jyk can validate it as a reliable resource [smile]
These articles at GeometricTools.com written by Dave Eberly do a decent job of explaining the mathematical concepts used inside Wild Magic. I highly recommend these articles considering Dave's depth of knowledge. Search for "quaternion" on this page and you'd come up with some really good ones on the subject.
Thanks for the links guys, I'm going to check them out soon. I wanted to point out, however, that I looked up quaternions in the angular displacement section of the book "3D Math Primer for Graphics and Game Development" and it is really pretty badass. I strongly recommend it if you're finding the concept a little hard to understand.

With that said, I have a general question that I'll try to phrase as best I can:

When you say you're rotating about an "arbitrary axis," does this mean that the vector that's pointing in any given direction is rotating like a spinning top, or does it mean that it's moving the head of the vector in a rotation that extends from the base to another given point in 3D space, sort of like someone holding the navigation aids for airplane pilots and pointing from one direction to another?
Quote:Original post by The Steve
When you say you're rotating about an "arbitrary axis," does this mean that the vector that's pointing in any given direction is rotating like a spinning top, or does it mean that it's moving the head of the vector in a rotation that extends from the base to another given point in 3D space, sort of like someone holding the navigation aids for airplane pilots and pointing from one direction to another?
Assuming your pages are the same as mine, check out page 110 of '3D Math Primer'. There's a nice diagram there showing a vector (lebeled v) rotating about an axis (labeled n).

Looking at the diagram, you can imagine the head of v rotating in a circle about n. This demonstrates pretty clearly what we mean by 'rotation about an arbitrary axis'.

This topic is closed to new replies.

Advertisement