Upcoming Events
Southwest Gaming Expo
11/20 - 11/22 @ Dallas, TX

Workshop on Network and Systems Support for Games (NetGames 2009)
11/23 - 11/25 @ Paris, France

ICIDS 2009 Interactive Storytelling
12/9 - 12/11 @ Guimarães, Portugal

Global Game Jam
1/29 - 1/31  

More events...


Quick Stats
6665 people currently visiting GDNet.
2341 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!



Link to us

Link to us

  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