Saturday, 18 April 2015

Gimbal Lock


What is gimbal lock and why do we still have to worry about it?




If you have ever worked with rotations and Euler angles you are probably at least somewhat familiar with the phrase "gimbal lock". But like many things concerning rotations, angles and spaces it can be tricky to visualize and get a good grasp of.
Sometimes it feels like every time I need to think about gimbal lock I have forgotten everything about it and have to go back the beginning and ask myself: OK, but what is it really that is happening?

Hopefully, this article will take care of that problem.



GIMBAL LOCK

A gimbal is, according to Wikipedia, a “ring that is suspended so [that] it can rotate about an axis” and gimbals are “typically nested one within another to accomodate rotation about multiple axes”. As such, a nested three-gimbal system can be used to physically model any 3D rotation based on Euler angles.

Gimbal lock occurs when the middle gimbal rotates in such a way that causes the outer and inner gimbals to align in a plane. In this case, rotating both these gimbals in effect only rotates the rigid body on one axis. The resulting system can only rotate on two axes and it has lost one degree of freedom.



Euler angles and Gimbal lock


Euler angles - you probably remember that this is something like a set of three angles of rotation about the axes X, Y and Z? Not exactly.

Suppose you want to rotate an object, and you have a set of finite angles (X: 45 °, Y: 45 °, Z: 45 °). One of the pitfalls of the Euler angles - the need to choose some of the same order of turns. If the first turn 45 ° around the axis X,
then around Y
and at the end Z, then obtain the result in the left half of the picture below. If the order is ZXY, then the result will be different, as on the right half.








In fact, not only the above described Euler angles, it's the angles Tate - Bryan. Euler angles are many confusing variations, some of which need to be rotated around the global axes, in other axes are rotated after each step in the third axis is always fixed on the object and move with it. Everything else is added to a different order of turns. If you can - do not use the Euler angles.

The choice of the order of rotations depends on the place the appearance of gimbal lock. What is it? Take for example this procedure turns: ZXY. If the rotation about the X axis will be equal to 90 ° or -90 °, then the rotation about the Z and Y 'is' one another and there will be only a stub of the larger of rotations. For example (X: 90 °, Y: 90 °, Z: 90 °) in a turn (X: 90 °, Y: 0 °, Z: 0 °). Attention to the illustrations.






Can also be substituted (X: 90 °, X: 130 °, Z: 140 °), or (X: 90 °, Y: 30 °, Z: 40 °), but the result will still be obtained (X: 90 °, Y: 0 °, Z: 10 °). Bit is not intuitive, do not you think? This is all due to gimbal lock. When the rotation around the X axis is equal to 90 ° or -90 °, may not use the local rotation axis Y becomes parallel to the axis Z, but with the opposite direction, so the rotation around it comes into conflict with the previous rotation around Z.





When axis coincide, one degrees of freedom is lost. This is the hinge lock. Do you want to make a turn, but you do not have axes to implement it.

Joint lock appears in the middle of the hierarchy turns. If you use the procedure XYZ or ZYX, then turn to the right or to the left will jam animation. Since such a turn is much more common than, for example, turn towards the zenith or nadir, in many programs use a sequence ZXY. This hierarchy of turns used in Unity3d, though everything inside rotation still stored in quaternions. What are quaternions? This is better to tell apart. Quaternions and rotation matrix is one way to avoid gimbal lock. There are also clever algorithms that seamlessly bypass the lock side, but it affects the quality of the animation. It is best to use the Euler angles only for simple cases: propellers, wheels, pendulums. Sometimes it is possible to change the hierarchy of turns, but then still have to remember about the lock.



Avoid Gimbal Lock for Rotation/Direction Maya Manipulators


In the past few weeks, my colleague Naiqi Weng had to work on an issue where someone had a Maya manipulator that contains both a rotateManip and a directionManip, which controlled one single “rotate” attribute. The manipulator was working fine excepted under some specific rotations where the directional manipulator "pops" the object's rotation. She noticed that when this was happening there was two axes on top of each other when looking at the rotateManip and the rotateMode was in "Gimbal" mode, which would mean that this was happening due to gimbal lock.


                        


Having more than one manipulators control one single attribute is not a recommended way to work with manipulators.

Gimbal lock is the loss of one degree of freedom in a three-dimensional space that occurs when the axes of two of the three gimbals are driven into a parallel configuration, "locking" the system into rotation in a degenerate two-dimensional space. The only way to avoid gimbal lock is to use quaternion instead of euler to represent rotations.

In this specific situation, unless both rotate manip and direction manip use quaternion, the gimbal lock behavior can NOT be avoided. Quaternion can uniquely identify a rotation, but when it is converted into euler rotation, it loses one degree of freedom information. Let’s say you have rotated your object with your rotate manipulator, and you used the quaternion rotation. Now you want your direction manip to follow the rotate manipulator rotates, you convert the quaternion to euler rotation and get an angle of what the current direction manip should be, however you will never tell whether this direction manip should rotation from original direction to this angle from clock wise, or counter clock wise, because either way, you get the same end result, which is the current direction manip location.





Quaternion Vs Euler 

Quaternion is a 4D representation which represents 3D rotation, that’s why it is sufficient to avoid any ambiguities, while euler is 3D representation and the gimbal lock ambiguity cannot be removed in this representation. Quaternion is a data type suitable for defining object orientation and rotations. Quaternions are easier to work with than matrices and using quaternions helps to avoid gimbal lock problem like in case of Euler angles usage. 

Tasks like smooth interpolation between three-dimensional rotations and building rotation by vector are fairly simpler to solve with quaternions than with Euler angles or matrices. Industrial grade inertial trackers and many other orientation sensors can return rotational data in quaternion form, also to avoid gimbal lock problem, and make such values easier to filter by interpolation.

Unfortunately the Maya manipulator API are not exposed for quaternions yet. For now, the only workaround would be to track the rotation direction of the objects manually.


Refrences: 

  • https://unionassets.com/blog/euler-angles-and-gimbal-lock-282
  • http://around-the-corner.typepad.com/adn/2012/08/avoid-gimbal-lock-for-rotationdirection-maya-manipulators.html




No comments:

Post a Comment