Skip to main content

Rotation Matrices

Geoffrey Hunter
mbedded.ninja Author

Overview

Rotation matrices are matrices which are used to describe the rotation of a rigid body from one orientation to another.

In R3\mathbb{R3} they form a 3x3 matrix.

R=[ux^vx^wx^uy^vy^wy^uz^vz^wz^]\mathbf{R} = \begin{bmatrix} \hat{u_x} & \hat{v_x} & \hat{w_x} \\ \hat{u_y} & \hat{v_y} & \hat{w_y} \\ \hat{u_z} & \hat{v_z} & \hat{w_z} \end{bmatrix}

such that if a\vec{\b{a}} was a point in 3D space, then we can rotate a\vec{\b{a}} to b\vec{\b{b}} around the origin by applying R\b{R} in the following manner:

Ra=b\b{R}\vec{\b{a}} = \vec{\b{b}}

where:
a\b{a} and b\b{b} are 1x3 vectors

Rotation matrices are always orthogonal matrices which have a determinant of 1.

Combining Rotations

Two successive rotations represented by R1\b{R_1} and R2\b{R_2} can be represented by a single rotation matrix R3\b{R_3} where:

R3=R2R1\b{R_3} = \b{R_2} \b{R_1}

Pay careful attention to the order of the matrix multiplication, successive rotation matrices are multiplied on the left.

How To Find The Rotation Matrix Between Two Coordinate Systems

Suppose I have the frame with the following unit vectors defining the first coordinate system X1Y1Z1X1Y1Z1:

X1=[xxxyxz]Y1=[yxyyyz]Z1=[zxzyzz]X1=\begin{bmatrix}x_x\\x_y\\x_z\end{bmatrix} \quad Y1=\begin{bmatrix}y_x\\y_y\\y_z\end{bmatrix} \quad Z1=\begin{bmatrix}z_x\\z_y\\z_z\end{bmatrix}

And a second coordinate system X2Y2Z2X2Y2Z2 defined by the unit vectors:

X2=[xxxyxz]Y2=[yxyyyz]Z2=[zxzyzz]X2=\begin{bmatrix}x_x\\x_y\\x_z\end{bmatrix} \quad Y2=\begin{bmatrix}y_x\\y_y\\y_z\end{bmatrix} \quad Z2=\begin{bmatrix}z_x\\z_y\\z_z\end{bmatrix}

The rotation matrix RR which rotates objects from the first coordinate system X1Y1Z1X1Y1Z1 into the second coordinate system X2Y2Z2X2Y2Z2 is:

R=[X1X2X1Y2X1Z2Y1X2Y1Y2Y1Z2Z1X2Z1Y2Z1Z2]R = \begin{bmatrix} X1 \cdot X2 & X1 \cdot Y2 & X1 \cdot Z2\\ Y1 \cdot X2 & Y1 \cdot Y2 & Y1 \cdot Z2\\ Z1 \cdot X2 & Z1 \cdot Y2 & Z1 \cdot Z2\\ \end{bmatrix}

where:
\cdot is the matrix dot product
and everything else as above

Creating A Rotation Matrix From Euler Angles (RPY)

A rotation expressed as Euler angles (which includes RPY or roll-pitch-yaw notation) can be easily converted into a rotation matrix. To represent a extrinsic rotation with Euler angles α\alpha, β\beta, γ\gamma are about axes xx, y y, zz can be formed with the equation:

R=Rz(γ)Ry(β)Rx(α)\b{R} = \b{R}_z(\gamma) \b{R}_y(\beta) \b{R}_x(\alpha)

where:

Rx(θ)=[1000cos(θ)sin(θ)0sin(θ)cos(θ)]Ry(θ)=[cos(θ)0sin(θ)010sin(θ)0cos(θ)]Rz(θ)=[cos(θ)sin(θ)0sin(θ)cos(θ)0001]\b{R}_x(\theta) = \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos(\theta) & -\sin(\theta) \\ 0 & \sin(\theta) & \cos(\theta) \\ \end{bmatrix} \\ \b{R}_y(\theta) = \begin{bmatrix} \cos(\theta) & 0 & \sin(\theta) \\ 0 & 1 & 0 \\ -\sin(\theta) & 0 & \cos(\theta) \\ \end{bmatrix} \\ \b{R}_z(\theta) = \begin{bmatrix} \cos(\theta) & -\sin(\theta) & 0 \\ \sin(\theta) & \cos(\theta) & 0 \\ 0 & 0 & 1 \end{bmatrix}

Converting A Rotation Matrix To Euler Angles

Whilst converting a rotation expressed as Euler angles is relatively trivial (see above), it is not no simple to go the other way and convert a rotation matrix to Euler angles.

Javascript

THREE.js has a Euler class with the function .setFromRotationMatrix() which can convert a rotation matrix to Euler angles. The supported Euler angle orders are XYZ, YZX, ZXY, XZY, YXZ, ZYX, and it only supports intrinsic rotations.

External Resources

https://www.andre-gaschler.com/rotationconverter/ is a great one-page rotation calculator.