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

 


Demo - Avoiding Gimbal Lock

Finally, we've reached what you all been waiting for: "How can quaternions avoid gimbal lock.?"

The basic idea is

  1. Use a quaternion to represent the rotation.
  2. Generate a temporary quaternion for the change from the current orientation to the new orientation.
  3. PostMultiply the temp quaternion with the original quaternion. This results in a new orientation that combines both rotations.
  4. Convert the quaternion to a matrix and use matrix multiplication as normal.

Firstly, I want to make a disclaimer regarding the sample code. The code is ugly and very poorly organized. But do remember, this is just a cut down version of my program when I was testing quaternions, and I'm not getting paid for this.

There are two executable samples that I have included. The first program, CameraEuler.exe, is an example for camera implementation using Euler angles.

The main concern should be the Main_Loop function in main.cpp.

The main thing you should take note (in the while loop) is

  1. There are 3 angles I keep track of for rotation in the X, Y, and Z axis.
  2. With every key press, I adjust the corresponding rotation variable.
  3. In the while loop, I translate and then just convert the 3 Euler angles to rotation matrices and multiply them into the final transformation matrix.

Use the up/down keys to rotate around the X axis, left/right to rotate around the Y axis and Insert/PageUp to rotate around the Z axis.

This program suffers from gimbal lock. If you want to see it in action, rotate the camera so that the yaw is 90 deg. Then try rotating in the X and Z direction. See what happens.

Now for the quaternion solution. The program is CameraQuat.exe and it is a slight modification of the previous program.

The main point you should take note (in the while loop) is

  1. The orientation of the camera is a quaternion.
  2. There are 3 angles corresponding to the keypress. Note the angles are meant to be an on/off switch (not accumulative). I reset them inside the while loop. Of course this is not the best way to do it but as I said, it is a quick job.
  3. I convert the 3 angles to a temporary quaternion.
  4. I multiply the temporary quaternion to the camera quaternion to obtain the combined orientation. Note the order of multiplication.
  5. The camera rotation is then converted to the Axis Angle representation for transforming the final matrix.

When a key is pressed, I generate a temporary quaternion corresponding to the key for a small rotation in that particular axis. I then multiply the temporary quaternion into the camera quaternion. This concatenation of rotations in 4D space will avoid gimbal lock. Try it and see for yourself.

The camera quaternion has to be changed into either a matrix form or equivalence form so that you can concatenate it into a final transformation matrix. You have to do this for every quaternion you use, as 4D space and 3D space just don't mix. In the case of OpenGL, I just changed the quaternion to an Axis Angle representation and let the API do the rest.

Although I did not use the global Euler angles for rotations in the second program, I have left them there as a guide for you to see the similar Euler rotations in the first program. Note the Euler angles will be incorrect if you rotate more than 1 axis (because it counts the keypress rather than getting the Euler angles from the camera quaternion). It is just a reference for you to see that when you rotate the yaw to 90 deg when the program starts, the gimbal lock problem is no more.

Note: I don't recommend you use my math library as it is. Understand the quaternion and write your own. For your information, I am going to throw all of that away and rewrite it too. It is just too messy and ugly for my taste.




Next : Conclusion