manhon
manhon

Reputation: 683

An example on why quaternion can prevent gimbal lock

I read this. https://matthew-brett.github.io/transforms3d/gimbal_lock.html

and it gave an example of gimbal lock

>>> import numpy as np
>>> np.set_printoptions(precision=3, suppress=True)  # neat printing
>>> from transforms3d.euler import euler2mat, mat2euler
>>> x_angle = -0.2
>>> y_angle = -np.pi / 2
>>> z_angle = -0.2
>>> R = euler2mat(x_angle, y_angle, z_angle, 'sxyz')
>>> R
array([[ 0.   ,  0.389, -0.921],
       [-0.   ,  0.921,  0.389],
       [ 1.   , -0.   ,  0.   ]])

Then I tried this: http://kieranwynn.github.io/pyquaternion/

q1 = Quaternion(axis=[1, 0, 0], angle=-0.2)
q2 = Quaternion(axis=[0, 1, 0], angle=-numpy.pi/2)
q3 = Quaternion(axis=[0, 0, 1], angle=-0.2)
q4 = q3 * q2 * q1;
q4.rotation_matrix
array([[ 0.        ,  0.38941834, -0.92106099],
       [ 0.        ,  0.92106099,  0.38941834],
       [ 1.        ,  0.        ,  0.        ]])

it gave same gimbal lock.

So, why Quaternion can prevent gimbal lock?

Upvotes: 1

Views: 2132

Answers (1)

kylengelmann
kylengelmann

Reputation: 196

Gimbal lock can occur when you do three separate rotations around separate axes, which every euler angle rotation does. For every set of rotations about several axes, there is always an equivalent single rotation about one single axis. Quaternions stop gimbal lock by allowing you to take this single equivalent rotation rather than a set of three rotations that, if done in the wrong order, could create gimbal lock.

I hope this helps, if you need me to clarify anything or have any further questions feel free to ask!

Upvotes: 2

Related Questions