Jump to content
  • Advertisement
Sign in to follow this  
RobMaddison

Quaternions and matrices

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

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?

Share this post


Link to post
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!