What are quaternions? When/how to use them?

AFAIK, all 3d hardware works with 4x4 matrix stacks. So, natively, that is the fastest hardware representation. However, quats (and Eulers) can be converted into matrices in a fairly efficient manner, from what I understand, so it isn’t expensive to use those formats.

You can sort of arrange it like this, going from fastest-in-hardware to easiest-to-understand:

4x4 matrices -> Quaternions -> Euler angles

Let me elaborate more: The key to understanding quaternions is to realize that Euler rotations (which are 3 separate rotations around mutually perpendicular axis*) are equivalent to a single rotation around an arbitrary axis – axis-angle rotations. You’re dealing with the full orientation of an object in this sense, and this is what a quaternion represents. Just like a 3x3 rotation matrix, that orientation is relative.

*If those axis are not perpendicular, then you have problems. This is where gimbal lock comes from.

If you don’t actually want to understand quaternions, that’s fine, but you should have a pretty firm grasp on that concept. You can combine two quaternions in any shape or form and they will generate a valid rotation – Not so with Euler rotations.

Really, quats are the same thing as axis-angle representations, but you don’t get quite as many nifty shortcuts with those. The same goes for matrix vector rotations. In fact, outside of the squads, you can get by with just vector rotations. Some people say that quaternions are just a passing fad, fooey, I’m happy with linear algebra and vector math alone.

As proof, I’ll show how my example above could have been done with just vector math:

                mat = object.getNetTransform().getMat()
                oldRight = mat.getRow3(0).normalize()

                newUp = normal
                newForward = newUp.cross (oldRight)
                newRight = newForward.cross (newUp)

                mat.setRow(0, newRight)
                mat.setRow(1, newForward)
                mat.setRow(2, newUp)
                object.setNetTransform(mat)

BTW, not tested, I might have my rows wrong and some commands out of whack. As you can see, it’s pretty much the same thing, conceptually.

That wikipedia article is weak, IMHO. It starts out promising, but then it leaps right into the pure math of quaternions. But, I’m not a huge math guy.

It’s sort of similar to explaining how 4x4 matrices work. Again, you’re looking at something that you’ll never find in an algebra class, as it’s a specific funky mathematical shorthand for combining rotations and translations. And it has the same kind of non-intuitive mathematical weirdness behind it as quaternions. (Shearing 4D space, versus quat’s complex numbers.)

Sorry, my post is rambling now. I shouldn’t try to explain things when I am tired.