Quaternions and matrices
I've read a lot of documentation where people say "use quaternions for orientation and not Euler angles". I can see why, because of gimbal lock and other advantages but if you're using something like direct x which has built in support for matrices, doesn't that mean that before drawing anything you have to convert your quaternion to a matrix? I'm just making sure I'm not missing a trick
Technically, since you can rotate a vector with a quaternion (p_rotated = q*p*q^-1, as explained on this wikipedia page, there is also a more efficient formula for rotation later in the page) you don't HAVE to convert the quaternion into a matrix. You can transform all your points using quaternions for rotation and vectors for translation/scaling.
But in practice this people don't generally do that. It's more expensive to rotate a point by a quaternion than it is to rotate it with a matrix multiplication. So, short answer is: yes, you do have to convert the quaternion representation of the rotation back into a matrix representation.
Of course you could just use the matrix representation directly, which doesn't suffer from gimbal lock either. However, quaternions have 3 significant advantages that I can think of:
- They use less memory (important if you are storing a lot of animation data, for instance).
- They make interpolation easier (look up slerp and nlerp).
- They are easy to renormalize, if you keep composing small rotations onto an attitude, for instance.
Thanks
Another important reason is of course that you usually combine the translation, rotation, and any other transformations into a single matrix which you use to transform your points. So indeed, it's usual to use quaternions while animating, but once you have determined the transformation for a mesh, you create a matrix, and use that matrix to transform its vertices.
Thanks guys.
I think I had quaternions wrong in my head...
I have a man standing up on the screen, facing away from me (x right, y up, z into the screen) and I want to orient him. I assumed that creating a vector, say (1, 1, 1) but normalized, with a rotation of zero would orient the man so he's facing 45 degrees up to the sky, I didn't realise that the object rotates around the quaternion vector literally. So if I set this quaternion:
0.707, 0.707, 0.707, 0 (for x, y, z, w) (for convenience, I have not shown the normalized quaternion which I use), I expected this to face my man towards 1, 1, 1 - but he doesn't move. I can understand this because it's a zero rotation around the vector. Now if I use this:
0.707, 0.707, 0.707, 45 - he appears in a position I wasn't quite expecting, but now understand. If you look from the vector point (1, 1, 1) back down at the man so you're viewing him from his front top right, the 45 degree rotation is the whole image rotating around the centre. I didn't think that's how quaternions worked but ok that's fine.
So now I've come to try and wrap my head around orienting my character using the quaternion (this is kind of a follow on from my recent Euler angle post), but how would I orient him so he is still standing up, but facing up towards 1, 1, 1 for instance?
Think of a normalised quaternion as an axis-angle rotation, q = (x, y, z, w) is a rotation ang found by w = cos(ang/2) (so ang = 2 * acos(w)) around the axis defined by the vector (x, y, z) which is scaled by sin(ang/2).