Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
89 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

  Contents

 Introduction
 Let's Get Started
 Quaternions
 Converting from
 Quaternions

 Multiplying
 Quaternions

 Conversion to
 Quaternions

 Demo
 Conclusion

 Printable version

 


Conversion To Quaternions

Now we learn how to convert other representations to quaternions. Although I do not use all the conversions in the sample program, there are times when you'll need them when you want to use quaternion orientation for more advanced stuff like inverse kinematics.


Axis Angle to Quaternion

A rotation around an arbitrary axis in 3D space can be converted to a quaternion as follows


If the axis of rotation is  (ax, ay, az)- must be a unit vector
and the angle is            theta (radians)

    w   =   cos(theta/2)
    x   =   ax * sin(theta/2)
    y   =   ay * sin(theta/2)
    z   =   az * sin(theta/2)

The axis must first be normalized. If the axis is a zero vector (meaning there is no rotation), the quaternion should be set to the rotation identity quaternion.

Euler to Quaternion

Converting from Euler angles to a quaternion is slightly more tricky, as the order of operations must be correct. Since you can convert the Euler angles to three independent quaternions by setting the arbitrary axis to the coordinate axes, you can then multiply the three quaternions together to obtain the final quaternion.

So if you have three Euler angles (a, b, c), then you can form three independent quaternions

Qx = [ cos(a/2), (sin(a/2), 0, 0)]
Qy = [ cos(b/2), (0, sin(b/2), 0)]
Qz = [ cos(c/2), (0, 0, sin(c/2))]

And the final quaternion is obtained by Qx * Qy * Qz.




Next : Demo - Avoiding Gimbal Lock