Skip to content

Rotation matrices, Euler angles, and gimbal lock

The fundamental rotations along the three axes are expressed by the following matrices:

Rotation for an angle 𝛾 about the 𝑥-axis:

𝑹𝑥(𝛾)=[1000cos(𝛾)−sin(𝛾)0sin(𝛾)cos(𝛾)]

Rotation for an angle 𝛽 about the 𝑦-axis:

𝑹𝑦(𝛽)=[cos(𝛽)0sin(𝛽)010−sin(𝛽)0cos(𝛽)]

Rotation for an angle 𝛼 about the 𝑧-axis:

𝑹𝑧(𝛼)=[cos(𝛼)−sin(𝛼)0sin(𝛼)cos(𝛼)0001]

Practically, inverting a rotation means applying another rotation with the inverse of the angle. So we derive that 𝑹−1(𝛼)=𝑹(−𝛼).

When staying in a fixed coordinate system, we can use transformation matrices to apply rotations to a vector.

The distance of the vector from the center remains constant: ‖𝑝‖2=𝑝𝑇𝑝=(𝑝′)𝑇𝑅𝑇𝑅𝑝′=(𝑝′)𝑇𝑝′

When applying rotations, we must distinguish between fixed frames and current frames. In fixed frames we apply the rotation staying in the original frame; in current frames we rotate by referring to the new frame after each rotation.

A rotation can be expressed as a sequence of partial rotations, each one defined with respect to the previous one. All rotations are defined with respect to the current frame, but there’s an important distinction to keep in mind when applying them.

  • When applying a rotation from the current frame, the sequence of rotations is obtained by post-multiplying the matrices associated with each rotation.
  • Instead, when applying the rotation from the fixed frame, the matrix of the later operation must be pre-multiplied to the matrices of the earlier rotations.

Let’s say we have two rotations: 𝑹10=𝑅𝑦(𝜑) around the 𝑦0 axis followed by 𝑹̅21 for angle 𝜃, which happens around 𝑧0 (not the 𝑧1 of the current frame).

We know how to do rotations in the current axis, so we can translate the fixed axis rotation to:

𝑹̅21=(𝑹10)−1𝑹𝑧(𝜃)𝑹10

which leads to:

𝑹20=𝑹10𝑹̅21=𝑹10(𝑹10)−1𝑹𝑧(𝜃)𝑹10=𝑹𝑧(𝜃)𝑹𝑦(𝜑)

A 3D rotation matrix has 9 elements, but a rotation can be completely specified by three parameters (an element of SO(𝑚) can be specified by 𝑚𝑚−12 parameters).

A popular convention is the use of 𝑥, 𝑦, 𝑧 rotations about fixed axes (known in aeronautics as roll-pitch-yaw or RPY).

Since the rotation is about fixed axes we have to pre-multiply:

𝑹𝑒(𝜑,𝜃,𝜓)=𝑹𝑧(𝜑)𝑹𝑦(𝜃)𝑹𝑥(𝜓)

Let’s introduce the notation 𝑐𝛼=cos(𝛼) and 𝑠𝛼=sin(𝛼).

We see that:

𝑹𝑒(𝜑,𝜃,𝜓)=[𝑐𝜑𝑐𝜃𝑐𝜑𝑠𝜃𝑠𝜓−𝑠𝜑𝑐𝜓𝑐𝜑𝑠𝜃𝑐𝜓+𝑠𝜑𝑠𝜓𝑠𝜑𝑐𝜃𝑠𝜑𝑠𝜃𝑠𝜓+𝑐𝜑𝑐𝜓𝑠𝜑𝑠𝜃𝑐𝜓−𝑐𝜑𝑠𝜓−𝑠𝜃𝑐𝜃𝑠𝜓𝑐𝜃𝑐𝜓]

Given a matrix 𝑹=[𝑟1,1𝑟1,2𝑟1,3𝑟2,1𝑟2,2𝑟2,3𝑟3,1𝑟3,2𝑟3,3], the inverse operation (finding the three angles) has two solutions:

𝜃∈(−𝜋2,𝜋2)⇒{𝜑=atan2(𝑟2,1,𝑟1,1)𝜃=atan2(−𝑟3,1,𝑟3,22+𝑟3,32)𝜓=atan2(𝑟3,2,𝑟3,3)𝜃∈(𝜋2,3𝜋2)⇒{𝜑=atan2(−𝑟2,1,−𝑟1,1)𝜃=atan2(−𝑟3,1,−𝑟3,22+𝑟3,32)𝜓=atan2(−𝑟3,2,−𝑟3,3)

In Euler-based parametrisations, the three angles act as three independent degrees of freedom.

At a pitch angle of ±𝜋2, the roll and yaw rotation axes become aligned, resulting in the loss of a degree of freedom. This means that a rotation around one axis can be replicated by a rotation around the other.

𝑹𝑒(𝜑,𝜋2,𝜓)=[0𝑠(𝜓−𝜑)𝑐(𝜓−𝜑)0𝑐(𝜓−𝜑)−𝑠(𝜓−𝜑)−100]

The real problem surfaces when we try to calculate the inverse, to find the angles of the rotation. We can verify that in a gimbal lock situation, there are infinitely many combinations of 𝜑 and 𝜓 that produce the same rotation matrix.