3D Rotations-Part 2
3D Rotations-Part 2
Rotations in Three-Dimensions:
Euler Angles and Rotation Matrices
Part 2 – Summary and Sample Code
D. Rose - February, 2015
Abstract
This paper describes a commonly used set of Tait-Bryan Euler angles, shows how to convert
from Euler angles to a rotation matrix and back, how to rotate objects in both the forward and
reverse direction, and how to concatenate multiple rotations into a single rotation matrix.
The paper is divided into two parts. Part 1 provides a detailed explanation of the relevant
assumptions, conventions and math. Part 2 provides a summary of the key equations, along with
sample code in Java. (See the links at the top of the page.)
Figure 1 shows the coordinate system and rotation conventions that will be used in this paper.
danceswithcode.net/engineeringnotes/rotations_in_3d/rotations_in_3d_part2.html 1/6
10/3/2019 3D Rotations-Part 2
Rotation Matrices
A rotation matrix is composed of nine numbers arranged in a 3x3 matrix like this:
(eq 1)
Code sample 1 shows a minimal data structure for representing a 3x3 rotation matrix. This object
will be used throughout the subsequent code examples.
(eq 2)
where:
(u, v, w) are the three Euler angles (roll, pitch, yaw), corresponding to rotations around the
x, y and z axes
c() and s() are shorthand for cosine and sine
The corresponding Java method is shown in Code Sample 2. The inputs are the three Euler
angles (in radians) in the order yaw, pitch and roll. The method first computes all the necessary
sine and cosine values, creates an empty rotation matrix object, then populates the matrix as
defined in equation 2. It returns the fully-populated rotation matrix object.
However, for the special case where the pitch angle (v) = +/-90°, the system enters a state called
“gimbal lock.” Equation 3b is still valid, but equations 3a and 3c are undefined. This condition
must be tested for and handled as follows:
In practice, it is usual to set one of the angles (u) or (w) to zero and solve for the other.
Code Sample 3 implements this solution, including the special case handling. The input is a
populated rotation matrix, and the return value is a three element array containing the yaw, pitch
and roll angles (in that order) in radians. Note that if the code detects the gimbal lock condition,
it sets the yaw angle (w) to zero, and solves for the roll angle (u).
This code will return values of roll and yaw between −π and +π radians, and pitch angles
between −π/2 and +π/2 radians.
danceswithcode.net/engineeringnotes/rotations_in_3d/rotations_in_3d_part2.html 3/6
10/3/2019 3D Rotations-Part 2
else if( A.r31 == -1 ){
angle[0] = 0.0; //yaw = 0
angle[2] = Math.atan2( A.r12, A.r13 ); //Roll
System.out.println("Gimbal lock: pitch = 90");
}
//General solution
else{
angle[0] = Math.atan2( A.r21, A.r11 );
angle[2] = Math.atan2( A.r32, A.r33 );
System.out.println("No gimbal lock");
}
return angle; //Euler angles in order yaw, pitch, roll
}
(eq 5)
where:
(x0, y0, z0) are the coordinates of the point before rotation
x1, y1, z1) are the coordinates of the point after rotation
rnn are the elements of the rotation matrix R as shown in equation 1
The Java implementation of Equations 6a through 6c is shown in Code Sample 4. The input
argument p_in is a three-element array of type double that contains the x, y and z values of the
point to be rotated. On return, the contents of p_in will not have changed. The returned value is a
new three-element array of type double that contains the x, y and z values of the rotated point.
Inverse Rotations
For rotation matrices, the transpose of a matrix equals its inverse. Therefore if Rf is a forward
rotation matrix, the reverse rotation Rr can be obtained by transposing the rows and columns of
Rf.
danceswithcode.net/engineeringnotes/rotations_in_3d/rotations_in_3d_part2.html 4/6
10/3/2019 3D Rotations-Part 2
The implementation is shown in Code sample 5. The input is a RotationMatrix object. The code
transposes the rows and columns, and returns a new 3x3 RotationMatrix. The input matrix is not
affected.
C = AB
(eq 8)
where:
Code Sample 6 performs the 3x3 matrix multiplication described in equation 8. The function
returns a new 3x3 rotation matrix.
Comments and error reports may be sent to the following address. We may post comments of
general interest. Be sure to identify the page you are commenting on.
Home
danceswithcode.net/engineeringnotes/rotations_in_3d/rotations_in_3d_part2.html 6/6