
I twist and twist, I want to confuse: Euler and Gimbal lock corners

Place any finger of your left hand forward. Let us not be shy, no one will laugh at you. This is for an important experiment. Put up? Now imagine that you are your finger (and delirium). Turn at right angles to the right, then up, and finally left. Where did you end up? Right, in the same place, but already on the back.
With some stretch, this is how rotation works using Euler angles. A bit unpredictable and uncomfortable, isn't it? Euler angles have several drawbacks, but there is one particularly bad property because of which you do not want to contact them. His name is Gimbal lock.
In the Russian language, gimbal lock is called differently: hinged lock, axle lock, folding frames. Unfortunately, on searches in the search engine with such keywords a lot of garbage is generated, and the Wikipedia article leaves much to be desired, so I myself will tell you about this phenomenon and suggest how to deal with it.
Attention! Going under the cat you are at risk of breaking your head.
First, let me remind you what Euler angles are. You probably remember that this is something like a set of three angles of rotation around the axes X, Y and Z? Not certainly in that way. Suppose you want to rotate an object, and you have a set of end angles (X: 45 °, Y: 45 °, Z: 45 °). One of the tricks of the Euler corners is the need to choose one order of turns. If you first turn 45 ° around the X axis, then around Y and finally around Z, you get the result as in the left half of the picture below. If the order is ZXY, then the result will be different, as in the right half.

In fact…
In fact, not just Euler angles are described above, but Tate - Brian angles . Euler angles have many confusing variations, in some of which you need to rotate around the global axes, in the other axes rotate after each step, in the third axis are always fixed on the object itself and move with it. To everything else, a different order of turns is added. If possible, do not use Euler angles.
The place of appearance of the hinged lock depends on the choice of the order of turns. What is it? Take for example this order of turns: ZXY. If the rotation around the X axis is equal to 90 ° or -90 °, then the rotations around Z and Y will "eat" each other and there will only be a stub from the larger of the rotations. For example (X: 90 °, Y: 90 °, Z: 90 °) will turn into simple (X: 90 °, Y: 0 °, Z: 0 °). Attention to the illustration.

You can also substitute (X: 90 °, Y: 130 °, Z: 140 °) or (X: 90 °, Y: 30 °, Z: 40 °), but the result will still be (X: 90 ° , Y: 0 °, Z: 10 °). Not a bit intuitive, don't you think? This is all because of the hinge lock. When the rotation around the X axis becomes 90 ° or -90 °, the not yet used local axis of rotation Y becomes parallel to the Z axis, but in the opposite direction, so the rotation around it conflicts with the previous rotation around Z.
* Whole! *
It exploded heads of particularly impressionable readers. I will explain the same with the arrows.

When the axes coincide, one degree of freedom is lost. This is the hinge lock. You want to make a turn, but you don’t have enough axes to make it.
A hinge lock appears in the middle of the rotation hierarchy. If you use the XYZ or ZYX order, then turning right or left will jam the animation. Since such a turn is much more common than, for example, a turn towards the zenith or nadir, many programs use the ZXY sequence. Such a hierarchy of rotations is used in Unity3d, though inside all rotations are still stored in quaternions. What are quaternions? It is better to talk about this separately. Quaternions and rotation matrices are one way to avoid a hinge lock. There are also tricky algorithms that smoothly bypass the castle side, but this affects the quality of the animation. It is best to use Euler angles only for simple cases: propellers, wheels, pendulums. Sometimes you can change the hierarchy of turns,
On the links below you can watch an interactive demonstration of the hinged lock using the example of a gyroscope. With him, to understand the hierarchy of turns and the situation with the castle is much easier.
WebGL | Windows | Linux | Mac | GitHub sources
Screenshots for everyone else
habrastorage.org/storage2/800/f9b/549/800f9b54918bfb0c563abafeed43f741.png
habrastorage.org/storage2/43a/0d2/39c/43a0d239c61201b09edc739f7a0f9e92.png
habrastorage.org/storage2/f97/fae/c76/f97faec763796d67e0bfd94df9e18644.png
habrastorage.org/storage2 /0db/c9f/6b4/0dbc9f6b4da28516ef1ee1e3382f5bcb.png
habrastorage.org/storage2/43a/0d2/39c/43a0d239c61201b09edc739f7a0f9e92.png
habrastorage.org/storage2/f97/fae/c76/f97faec763796d67e0bfd94df9e18644.png
habrastorage.org/storage2 /0db/c9f/6b4/0dbc9f6b4da28516ef1ee1e3382f5bcb.png
Rotate the scene with the mouse, Esc - exit, the remaining buttons are indicated on the screen.
For Linux users:
Make the GimbalLock file executable with “chmod + x GimbalLock” and run it.