# Gimbal Lock, Euler Angles, and Quaternions

I have been reading a 3d Math book and have been trying to learn some 3d concepts and how they operate. It talks a lot about the pros and cons of using different models of angular displacement such as Euler angles and Quaternions.

This topic isn’t very visually exciting, so I created my rendition of “Mr. Gimbal Lock” to the left. You now have a visual reference for the upcoming sinister snake-like creature it is! :) Keep reading to find out.

While Euler angles are more intuitive using degrees, they present a few different issues. Firstly, there are instances when the angles are different, but the object looks identical. For example 360 degrees is the same as 0 degrees. This issue is called *aliasing*. By seeing the final orientation, it is difficult to tell what exactly happened with the rotation.

Another interesting scenario is how rotations affect each other. For my rotations on the y axis, any change along the x or z axis will affect the other axises automatically. This is concept known as *gimbal lock*. Maybe an example will help!

Looking at the example, it looks as though they been transformed the exact same amount. Taking a peek at the code shows that they have different rotations. Code is using C#.

using UnityEngine; using System.Collections; public class GimbalLock : MonoBehaviour { private Quaternion newDirection; void Start () { newDirection = new Quaternion(); newDirection = Quaternion.Euler(-90, 45, 0); } void Update () { transform.rotation = Quaternion.Lerp(transform.rotation, newDirection, Time.deltaTime); } }

This is one of two scripts that are used for rotating the object. The other script is identical except for a different rotation.

This one uses: **newDirection = Quaternion.Euler(-90, 45, 0);**

the other uses: ** newDirection = Quaternion.Euler(-90, 0, 45);**

Why do they do the exact same thing? Euler angles and gimbal lock are the root of this phenomenon. He does look pretty sinister in the picture at the beginning!

Because Euler angles are imprecise when it comes to rotations, Unity opts to store rotation information for the object using Quaternions instead. Quaternions are pretty complicated to understand, so a good definition is outside of the scope of this post.

One good thing about Quaternions is that they have a better way of handling unique angles. Technically, they do have a slight aliasing problem at values such as (0,0,0, 1) and (0,0,0, -1).

The example blocks look identical because Unity converts Euler space to Quaternion space at assignment :

newDirection = Quaternion.Euler(-90, 45, 0);

*newDirection* is a Quaternion value, so Unity converts any Euler angles to make sure there are no problems before anything gets rotated. The angles are different in Eulers, but Quaternions “fix” the value so there is only one unique rotation amount for that orientation. That is why they both read the same rotation amount.

## Final Comments

What is the best book/blog/forums to learn about 3d math topics? The 3D Math Primer book is pretty good, but some of the crazy matrix math and quaternion math is a little over my head at the moment.

I don’t like hunting around for project files on other sites, so I will try to make sure I have a big button for anything that has acquiring source code.

How is my rendition of Mr. Gimbal Lock?

Hello I wonder if u can help me, I need to know how to get the angle that an object. my goal is to make a clock just hours out of minutes, so that the directional light change as the sun

If you want to create something that has a clock and the sun, I would probably try to create a some type of new Time Class that they can both reference. The new class can have functions that get the current time of the day as well as the rotation amounts. Actually finding the rotation amount should be as simple as calling the

sunObject.transform.eulerAngles. That will get the current Vector3 rotation in world space. The sun and clock objects can be updated simply by referencing the new Time Class. That way they will always be in sync, and update if you do any other things like time slowing down, speeding up, etc.Hello,

I am making a Unity game that uses Android Gyroscope. My plugin works and now I just to apply a rotation to my Camera. I use Quaternion.Euler to try to avoid gimbal lock bu it doesn’t work. Can you help me?

This post, while much appreciated, could use a little cleaning up. The unity file doesnt play embedded (I create a separate page to run it) but Im scratching my head with what exactly you are trying to show with the two cubes as its not documented exactly what the effect is/where its occurring here and the code surely doesn’t match what’s in the demo. Try to make them fit together, it would help the reader. Thanks for posting though!! :)