Jump to content
  • Advertisement
Sign in to follow this  
iperez

OpenGL Gimbal Lock

This topic is 4863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The issue of Gimbal Lock has had me confused since I learned of its existence. For about a week now I have been attempting to learn OpenGL from the NeHe tutorials. I was doing good up until I wanted to implement camera rotation. I read that in order to rotate the camera, you simpy rotate the world in the opposite direction you want to rotate the camera. Sounded simple enough at first. I wrote the following lines of code after my call to glLoadIdentity(), and to my surprise it did not work as expected.
glLoadIdentity();

glRotatef(anglex, 1, 0, 0);
glRotatef(angley, 0, 1, 0);
glRotatef(anglez, 0, 0, 1);

// draw objects here
It seems like the axes are rotating independent of each other. For example, when I increase the anglex and the anglez values, the X-rotation appears fine, but the z-axis appears to still have the same orientation. I read the article at the following URL, which describes matrix operations to perform rotation and movement in a 3d environment. http://www.makegames.com/3drotation/ However, after reading this I fail to understand how this would be any different than the above code. According to the glRotatef documentation, it creates a rotation matrix based on the parameters and mulitiplies the current matrix by the created rotation matrix. Now I don't know if my issue is related to Gimbal Lock (probably not), but I still don't understand what causes Gimbal Lock so therefore I don't know how to properly avoid it. Can someone please explain this to me before I lose my mind? :)

Share this post


Link to post
Share on other sites
Advertisement
If you are having gimbal lock your camera will go nuts and jump around when you cross over the axis's I can't remeber which ones. But if you want to avoid gimbal lock and if you want to be sure you will not have the issue with gimbal lock look into quaterion based rotations for camera movements.

Share this post


Link to post
Share on other sites
Okay, so I'm not experiencing Gimbal Lock then. I'll look into quaternions, but why does my rotation not work properly if it is not Gimbal Lock?

Share this post


Link to post
Share on other sites
Quote:
But if you want to avoid gimbal lock and if you want to be sure you will not have the issue with gimbal lock look into quaterion based rotations for camera movements.
This is a bit of misinformation that pops up repeatedly. To cut to the chase, quaternions do not 'cure' gimbal lock, and you do not need them to make a gimbal-lock-free camera. My advice is to stick with matrices for the time being - they are perfectly adequate for most purposes.

As for gimbal lock, my experience is that it occurs when the middle of the three Euler angle rotations is set to +/-90 degrees. This brings the first and last axes into alignment, and a degree of freedom is lost.

In a standard FPS-style camera, where there is no roll, gimbal lock is not an issue. However, if you're trying to make a 6dof space-sim-style camera, Euler angles are probably not the best choice; rather, you'll want to use incremental axis-angle rotations.

As for your camera matrix, whatever series of transformations describes the orientation and position of your camera, you want to do the opposite transformations in the opposite order to set up your view matrix. As a standard example, say the camera 'object' matrix is:

glLoadIdentity();
glTranslatef(camera.pos.x, camera.pos.y, camera.pos.z);
glRotatef(yaw, 0, 1, 0);
glRotatef(pitch, 1, 0, 0);

The view matrix would then be:

glLoadIdentity();
glRotatef(-pitch, 1, 0, 0);
glRotatef(-yaw, 0, 1, 0);
glTranslatef(-camera.pos.x, -camera.pos.y, -camera.pos.z);

'Hope that clears things up a little.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!