# Quaternions and matrices

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

## Recommended Posts

Quickie...

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

##### Share on other sites

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.

Edited by Samith

##### Share on other sites

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:

1. They use less memory (important if you are storing a lot of animation data, for instance).
2. They make interpolation easier (look up slerp and nlerp).
3. They are easy to renormalize, if you keep composing small rotations onto an attitude, for instance.

##### Share on other sites
Thanks both for the replies. I am using quaternions in my engine for slerping my animations and that works fantastically well. But once I've worked out the final slerped quaternion it gets converted to a matrix in order to send to the shader. Just making sure that's the right thing to do (never heard of anyone using quaternions in a shader!).

Thanks

##### Share on other sites

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.

##### Share on other sites
It is possible to send a quaternion to a shader and use it directly. This may be useful for skinning or to reduce memory requirements. For example you can see this presentation by Crytek as an example of quaternion use in shaders.

##### Share on other sites

Thanks guys.

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?

##### Share on other sites

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).

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634062
• Total Posts
3015304
×