Archived

This topic is now archived and is closed to further replies.

shakazed

Quaternions!

Recommended Posts

So if you''ve read up on them, you''ve probobly heard that they''re 4d vectors developed way back when for purposes that don''t matter to us and they''re impossible to visualize. Well that''s one way to think of them. Another way is much simpler and it''s uses for game programming are endless.

A quaternion for people like you and me is a vector and a rotation around that vector. Take for example your arm. You can rotate it on the x y and z axises. But if you keep your arm still in a fixed rotation and then you twist it, you''re rotating it around the current rotation. Now you say that you can achieve it through euler angles and you can, it''s just must more difficult and problems such as gimble lock can happen if you don''t apply rotations in the right order. So if you use a quaternions, you don''t need to worry about in which order you apply the rotations if you want to mimic and object such as your arm. Also, if you want to interpolate between two of your arm positions, quaternions create much more smoother results using spherical linear interpolation (slerping them).

If you can''t see the uses for them, then I''ll give you a few examples. Lets say you want to do a character animation system. What I said about the arm earlier applies to the whole body. When you want to interpolate between two poses, the animation is much smoother and rotations around vectors are helpful like before. Second, what if you''re doing a high tech space racing game. You obviously want to store the direction it''s facing but when you go around a turn, then you want to bank the view so that''s where the rotation around the vector comes in to help.

Hope that makes sense.

---
My Site
Come join us on IRC in #directxdev @ irc.afternet.org

Share this post


Link to post
Share on other sites
I got a book called "quaternions and rotation sequences" last week because I can't understand any of the online resources either. Here is how you do a rotation about an axis.

The first thing you need to know for using quaternions for roatations is how to multiply two quaternions together. A quaternion is like a complex number ( a + bi ) where a is the real part and b is the imaginary part, except quaternions have 3 "imaginary" parts ( a + bi + cj +dk ). You multiply two quaterions together just the same as you multiply two 3rd degree polynomials together, i.e. you distribute each term in one multiplicand into the other multiplicand and add each result toghether ( you should have 4 results because there are 4 components in the quaternion that have to be distributed into the other quaternion ). So

( a + bi + cj +dk ) * ( x + yi + zj +wk ) =
a * ( x + yi + zj +wk ) +
bi * ( x + yi + zj +wk ) +
cj * ( x + yi + zj +wk ) +
dk * ( x + yi + zj +wk )

Doing this you end up with terms in the product like (b*z)ij and (c*w)jk. You can remember the rules for multiplying i's, j's, and k's together by writing down the letter sequence "kijk". For any product ij, ji, ik, ki, jk, kj you read the mulitplicand from the letter sequence "kijk". The next letter in the sequence is the result of the multplication and if you had to read from left to right then the result is positive. If you had to read from right to left the result is negative. So reading ij from "kijk" you go: i then j then k. Since you read from left to right the result is +k. kj would read k then j then i, so the result is -i. Note here that multiplication is not commutative, that is, ij != ji.

The second thing you need to know is how to take the conjugate of a quaternion. This is easy. The conjugate of ( x + yi + zj +wk ) is just ( x - yi - zj -wk ). So basically to take the conjugate you just change the signs on the i,j, and k factors.

The third thing you need to know is how to construct the quaternion you need for a rotation about an axis. This is easy too. If you axis is ( x,y,z ) and you want to rotate about an angle theta, the quaternion you need is

cos( theta/2 ) + sin( theta/2 )( xi + yj + zk )

The final step is to take a vector you want to rotate (a, b ,c ) and make it into a quaternion like this v = ai + bj + ck then take your rotation quaternion from the previos step, call that one q and carry out the multiplication

qvq *

where q * is the conjugate of q . The result is the vector ( a,b,c ) rotated about an axis (x,y,z) an angle theta. The result will be of the form ri + sj + tk, so the result is still a quaternion, but the r, s, and t components are the x,y, and z of the new vector.


In chapter 7 of that book I mentioned they show you how to convert matrices into quaternions and quaternions into matrices, and so on and so forther for pitch,yaw,roll; direction cosines, and other stuff too. So you might want to look at that.


[edited by - grady on August 14, 2002 2:22:45 PM]

Share this post


Link to post
Share on other sites