• Create Account

# Quaternions and matrices

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1RobMaddison  Members   -  Reputation: 572

Like
0Likes
Like

Posted 03 December 2013 - 07:21 PM

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

### #2Samith  Members   -  Reputation: 1470

Like
1Likes
Like

Posted 03 December 2013 - 07:44 PM

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, 03 December 2013 - 07:51 PM.

### #3Álvaro  Crossbones+   -  Reputation: 9872

Like
1Likes
Like

Posted 03 December 2013 - 10:20 PM

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.

### #4RobMaddison  Members   -  Reputation: 572

Like
0Likes
Like

Posted 04 December 2013 - 02:36 AM

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

### #5quasar3d  Members   -  Reputation: 622

Like
2Likes
Like

Posted 04 December 2013 - 02:44 AM

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.

### #6apatriarca  Crossbones+   -  Reputation: 1393

Like
1Likes
Like

Posted 04 December 2013 - 03:14 AM

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.

### #7RobMaddison  Members   -  Reputation: 572

Like
0Likes
Like

Posted 07 December 2013 - 09:48 AM

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?

### #8Paradigm Shifter  Crossbones+   -  Reputation: 4746

Like
0Likes
Like

Posted 07 December 2013 - 10:29 AM

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

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS