Rotation matrices are matrices which are used to describe the rotation of a rigid body from one orientation to another .
In R 3 \mathbb{R3} R 3 they form a 3x3 matrix.
R = [ u x ^ v x ^ w x ^ u y ^ v y ^ w y ^ u z ^ v z ^ w z ^ ] \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} R = u x ^ u y ^ u z ^ v x ^ v y ^ v z ^ w x ^ w y ^ w z ^
such that if a ⃗ \vec{\b{a}} a was a point in 3D space, then we can rotate a ⃗ \vec{\b{a}} a to b ⃗ \vec{\b{b}} b around the origin by applying R \b{R} R in the following manner:
R a ⃗ = b ⃗ \b{R}\vec{\b{a}} = \vec{\b{b}} R a = b
where:
a \b{a} a and b \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 R 1 \b{R_1} R 1 and R 2 \b{R_2} R 2 can be represented by a single rotation matrix R 3 \b{R_3} R 3 where:
R 3 = R 2 R 1 \b{R_3} = \b{R_2} \b{R_1} R 3 = R 2 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 X 1 Y 1 Z 1 X1Y1Z1 X 1 Y 1 Z 1 :
X 1 = [ x x x y x z ] Y 1 = [ y x y y y z ] Z 1 = [ z x z y z z ] 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} X 1 = x x x y x z Y 1 = y x y y y z Z 1 = z x z y z z
And a second coordinate system X 2 Y 2 Z 2 X2Y2Z2 X 2 Y 2 Z 2 defined by the unit vectors:
X 2 = [ x x x y x z ] Y 2 = [ y x y y y z ] Z 2 = [ z x z y z z ] 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} X 2 = x x x y x z Y 2 = y x y y y z Z 2 = z x z y z z
The rotation matrix R R R which rotates objects from the first coordinate system X 1 Y 1 Z 1 X1Y1Z1 X 1 Y 1 Z 1 into the second coordinate system X 2 Y 2 Z 2 X2Y2Z2 X 2 Y 2 Z 2 is:
R = [ X 1 ⋅ X 2 X 1 ⋅ Y 2 X 1 ⋅ Z 2 Y 1 ⋅ X 2 Y 1 ⋅ Y 2 Y 1 ⋅ Z 2 Z 1 ⋅ X 2 Z 1 ⋅ Y 2 Z 1 ⋅ Z 2 ] 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} R = X 1 ⋅ X 2 Y 1 ⋅ X 2 Z 1 ⋅ X 2 X 1 ⋅ Y 2 Y 1 ⋅ Y 2 Z 1 ⋅ Y 2 X 1 ⋅ Z 2 Y 1 ⋅ Z 2 Z 1 ⋅ Z 2
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 x x x , y y y , z z z can be formed with the equation:
R = R z ( γ ) R y ( β ) R x ( α ) \b{R} = \b{R}_z(\gamma) \b{R}_y(\beta) \b{R}_x(\alpha) R = R z ( γ ) R y ( β ) R x ( α )
where:
R x ( θ ) = [ 1 0 0 0 cos ( θ ) − sin ( θ ) 0 sin ( θ ) cos ( θ ) ] R y ( θ ) = [ cos ( θ ) 0 sin ( θ ) 0 1 0 − sin ( θ ) 0 cos ( θ ) ] R z ( θ ) = [ cos ( θ ) − sin ( θ ) 0 sin ( θ ) cos ( θ ) 0 0 0 1 ] \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} R x ( θ ) = 1 0 0 0 cos ( θ ) sin ( θ ) 0 − sin ( θ ) cos ( θ ) R y ( θ ) = cos ( θ ) 0 − sin ( θ ) 0 1 0 sin ( θ ) 0 cos ( θ ) R z ( θ ) = cos ( θ ) sin ( θ ) 0 − sin ( θ ) cos ( θ ) 0 0 0 1
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.
Further Reading
https://www.andre-gaschler.com/rotationconverter/ is a great one-page rotation calculator.