3D Rotations and Transformations
3D Rotations ¶ 3D Rotations are fundamental transformations in space.
2D Rotation around Z-axis ¶ The rotation of a point ( x , y ) (x, y) ( x , y ) in the X Y XY X Y -plane (around the Z Z Z -axis) by an angle ϕ \phi ϕ (or θ \theta θ ) is given by:
[ x ′ y ′ z ′ ] = [ cos ϕ − sin ϕ 0 sin ϕ cos ϕ 0 0 0 1 ] [ x y z ] \begin{bmatrix}
x'\\
y'\\
z'
\end{bmatrix}
=
\begin{bmatrix}
\cos\phi & -\sin\phi & 0\\
\sin\phi & \cos\phi & 0\\
0 & 0 & 1
\end{bmatrix}
\begin{bmatrix}
x\\
y\\
z
\end{bmatrix} ⎣ ⎡ x ′ y ′ z ′ ⎦ ⎤ = ⎣ ⎡ cos ϕ sin ϕ 0 − sin ϕ cos ϕ 0 0 0 1 ⎦ ⎤ ⎣ ⎡ x y z ⎦ ⎤ 3D Axis-Aligned Rotation Matrices ¶ Rotation around X-axis (R x ( θ x ) R_x(\theta_x) R x ( θ x ) ) ¶ [ x ′ y ′ z ′ ] = [ 1 0 0 0 cos θ x − sin θ x 0 sin θ x cos θ x ] [ x y z ] \begin{bmatrix}
x'\\
y'\\
z'
\end{bmatrix}
=
\begin{bmatrix}
1 & 0 & 0\\
0 & \cos\theta_x & -\sin\theta_x\\
0 & \sin\theta_x & \cos\theta_x
\end{bmatrix}
\begin{bmatrix}
x\\
y\\
z
\end{bmatrix} ⎣ ⎡ x ′ y ′ z ′ ⎦ ⎤ = ⎣ ⎡ 1 0 0 0 cos θ x sin θ x 0 − sin θ x cos θ x ⎦ ⎤ ⎣ ⎡ x y z ⎦ ⎤ Rotation around Y-axis (R y ( θ y ) R_y(\theta_y) R y ( θ y ) ) ¶ [ x ′ y ′ z ′ ] = [ cos θ y 0 sin θ y 0 1 0 − sin θ y 0 cos θ y ] [ x y z ] \begin{bmatrix}
x'\\
y'\\
z'
\end{bmatrix}
=
\begin{bmatrix}
\cos\theta_y & 0 & \sin\theta_y\\
0 & 1 & 0\\
-\sin\theta_y & 0 & \cos\theta_y
\end{bmatrix}
\begin{bmatrix}
x\\
y\\
z
\end{bmatrix} ⎣ ⎡ x ′ y ′ z ′ ⎦ ⎤ = ⎣ ⎡ cos θ y 0 − sin θ y 0 1 0 sin θ y 0 cos θ y ⎦ ⎤ ⎣ ⎡ x y z ⎦ ⎤ Right-Hand Coordinate Frame ¶ The rotations typically follow the Right-hand coordinate frame convention
To determine the positive direction of rotation, curl your fingers from the first axis to the second; your thumb points in the direction of the third axis (the rotation axis).
Euler Angles and Decomposition ¶ All 3D rotations can be decomposed into 3 axis-aligned rotations
These three angles of rotation are called Euler angles .
A common set of Euler angles is Roll-Pitch-Yaw (RPY):
θ x = roll \theta_x = \text{roll} θ x = roll
θ y = pitch \theta_y = \text{pitch} θ y = pitch
θ z = yaw \theta_z = \text{yaw} θ z = yaw
The total rotation R R R is a product of the individual rotations. For the Roll-Pitch-Yaw sequence (X − Y − Z X-Y-Z X − Y − Z ):
R = R x ( θ z ) R y ( θ y ) R z ( θ z ) R = R_x(\theta_z) R_y(\theta_y) R_z(\theta_z) R = R x ( θ z ) R y ( θ y ) R z ( θ z ) Important: The Application of Rotation matrices is from right to left
Combination of Euler Angles ¶ There are 12 possible combinations of Euler angles (rotations).
This arises from two key choices when chaining rotations:
Axis Permutations: There are 3 ! = 6 3! = 6 3 ! = 6 possibilities for the order of axes (e.g., X − Y − Z , Y − Z − X , Z − Y − Z X-Y-Z, Y-Z-X, Z-Y-Z X − Y − Z , Y − Z − X , Z − Y − Z ).
Tait-Bryant Angles: You only use two axis but three rotations (e.g., X − Y − X , X − Z − X X-Y-X, X-Z-X X − Y − X , X − Z − X ).
Rotation Composition ¶ The rotation from frame C C C to frame W W W via an intermediate frame S S S is a matrix product:
w R c = w R s s R c \mathbf{^wR_c} = \mathbf{^wR_s} \mathbf{^sR_c} w R c = w R s s R c The composition of two rotation matrices, R 1 R 2 R_1 R_2 R 1 R 2 , is also orthonormal with a determinant of 1.
det ( R 1 R 2 ) = det ( R 1 ) det ( R 2 ) = 1 ⋅ 1 = 1 \det(R_1 R_2) = \det(R_1) \det(R_2) = 1 \cdot 1 = 1 det ( R 1 R 2 ) = det ( R 1 ) det ( R 2 ) = 1 ⋅ 1 = 1 The inverse of a composed rotation is: ( R 1 R 2 ) − 1 = R 2 − 1 R 1 − 1 = R 2 T R 1 T (R_1 R_2)^{-1} = R_2^{-1} R_1^{-1} = R_2^T R_1^T ( R 1 R 2 ) − 1 = R 2 − 1 R 1 − 1 = R 2 T R 1 T
For homogeneous transformations T \mathbf{T} T (which include both rotation and translation):
w T c = w T s s T c \mathbf{^wT_c} = \mathbf{^wT_s} \mathbf{^sT_c} w T c = w T s s T c 3D Rotation Representations and DOF ¶ All rotation representations must account for 3 Degrees of Freedom (DOF) .
Representation Description Initial free variables (Numbers) Constraints Final DOF Rotation Matrix R \mathbf{R} R 3 × 3 3 \times 3 3 × 3 matrix9 DOF det ( R ) = 1 \det(R)=1 det ( R ) = 1 , R T R = I R^T R = I R T R = I 3 DOF Euler Angles ( θ x , θ y , θ z ) (\theta_x, \theta_y, \theta_z) ( θ x , θ y , θ z ) 3 DOF None 3 DOF Quaternions q \mathbf{q} q ( x , y , z , w ) (x, y, z, w) ( x , y , z , w ) 4 DOF ∣ q ∣ = 1 |\mathbf{q}| = 1 ∣ q ∣ = 1 (One constraint)3 DOF Axis-Angle ( u ^ , θ u ) (\mathbf{\hat{u}}, \theta_u) ( u ^ , θ u ) 4 DOF ∣ u ^ ∣ = 1 |\mathbf{\hat{u}}| = 1 ∣ u ^ ∣ = 1 (Unit vector constraint)3 DOF
Roll-Pitch-Yaw to Rotation Matrix Conversion ¶ For the Roll-Pitch-Yaw (RPY) sequence R = R x ( θ x ) R y ( θ y ) R z ( θ z ) R = R_x(\theta_x) R_y(\theta_y) R_z(\theta_z) R = R x ( θ x ) R y ( θ y ) R z ( θ z ) , the individual rotation matrices are multiplied:
R = [ c θ z − s θ z 0 s θ z c θ z 0 0 0 1 ] [ c θ y 0 s θ y 0 1 0 − s θ y 0 c θ y ] [ 1 0 0 0 c θ x − s θ x 0 s θ x c θ x ] R =
\begin{bmatrix}
c\theta_z & -s\theta_z & 0 \\
s\theta_z & c\theta_z & 0 \\
0 & 0 & 1
\end{bmatrix}
\begin{bmatrix}
c\theta_y & 0 & s\theta_y \\
0 & 1 & 0 \\
-s\theta_y & 0 & c\theta_y
\end{bmatrix}
\begin{bmatrix}
1 & 0 & 0 \\
0 & c\theta_x & -s\theta_x \\
0 & s\theta_x & c\theta_x
\end{bmatrix} R = ⎣ ⎡ c θ z s θ z 0 − s θ z c θ z 0 0 0 1 ⎦ ⎤ ⎣ ⎡ c θ y 0 − s θ y 0 1 0 s θ y 0 c θ y ⎦ ⎤ ⎣ ⎡ 1 0 0 0 c θ x s θ x 0 − s θ x c θ x ⎦ ⎤ This product yields the final 3 × 3 3 \times 3 3 × 3 rotation matrix:
R = [ r 11 r 12 r 13 r 21 r 22 r 23 r 31 r 32 r 33 ] R =
\begin{bmatrix}
r_{11} & r_{12} & r_{13} \\
r_{21} & r_{22} & r_{23} \\
r_{31} & r_{32} & r_{33}
\end{bmatrix} R = ⎣ ⎡ r 11 r 21 r 31 r 12 r 22 r 32 r 13 r 23 r 33 ⎦ ⎤ Where c θ = cos θ c\theta = \cos\theta c θ = cos θ and s θ = sin θ s\theta = \sin\theta s θ = sin θ .
Rotation Matrix to Roll-Pitch-Yaw Conversion ¶ Given a rotation matrix R R R , the Euler angles ( θ x , θ y , θ z ) (\theta_x, \theta_y, \theta_z) ( θ x , θ y , θ z ) (Roll, Pitch, Yaw) can be extracted:
Pitch (θ y \mathbf{\theta_y} θ y ):
The pitch angle can be calculated from the r 31 r_{31} r 31 element.
− sin ( θ y ) = r 31 - \sin(\theta_y) = r_{31} − sin ( θ y ) = r 31
θ y = sin − 1 ( − r 31 ) ∈ [ − π 2 , π 2 ] \theta_y = \sin^{-1}(-r_{31}) \in [-\frac{\pi}{2}, \frac{\pi}{2}] θ y = sin − 1 ( − r 31 ) ∈ [ − 2 π , 2 π ] Roll (θ x \mathbf{\theta_x} θ x ):
The roll angle can be calculated using the arctan 2 \arctan2 arctan 2 function for quadrant-aware calculation:
θ x = arctan 2 ( r 32 , r 33 ) ∈ ( − π , π ] \theta_x = \arctan2(r_{32}, r_{33}) \in (-\pi, \pi] θ x = arctan 2 ( r 32 , r 33 ) ∈ ( − π , π ] Yaw (θ z \mathbf{\theta_z} θ z ):
The yaw angle can be similarly calculated using arctan 2 \arctan2 arctan 2 .
θ z = arctan 2 ( r 21 , r 11 ) \theta_z = \arctan2(r_{21}, r_{11}) θ z = arctan 2 ( r 21 , r 11 ) Gimbal Lock ¶ Gimbal Lock is a significant problem encountered when using Euler angles.
It occurs when the pitch angle is θ y = 9 0 ∘ = π 2 \theta_y = 90^{\circ} = \frac{\pi}{2} θ y = 9 0 ∘ = 2 π .
At this angle, cos ( θ y ) = 0 \cos(\theta_y) = 0 cos ( θ y ) = 0 .
The first and third rotation axes (Roll and Yaw axes) become aligned (swapped).
This causes a loss of one Degree of Freedom (DOF), meaning two of the three rotation angles are redundant or indistinguishable.
The Axis-angle representation defines a rotation by a unit vector k ^ = [ k x , k y , k z ] T \mathbf{\hat{k}} = [k_x, k_y, k_z]^T k ^ = [ k x , k y , k z ] T and a rotation angle θ \theta θ .
The Rodrigues’ Formula converts this representation into the rotation matrix R R R :
R = I + sin θ [ k ^ × ] + ( 1 − cos θ ) [ k ^ × ] 2 R = I + \sin\theta [\mathbf{\hat{k}}_\times] + (1-\cos\theta) [\mathbf{\hat{k}}_\times]^2 R = I + sin θ [ k ^ × ] + ( 1 − cos θ ) [ k ^ × ] 2 Cross Product Matrix ¶ The [ k ^ × ] [\mathbf{\hat{k}}_\times] [ k ^ × ] notation represents the Cross product matrix :
[ k × ] = [ 0 − k z k y k z 0 − k x − k y k x 0 ] [\mathbf{k}_\times] =
\begin{bmatrix}
0 & -k_z & k_y\\
k_z & 0 & -k_x\\
-k_y & k_x & 0
\end{bmatrix} [ k × ] = ⎣ ⎡ 0 k z − k y − k z 0 k x k y − k x 0 ⎦ ⎤ The cross product of two vectors, a × b \mathbf{a} \times \mathbf{b} a × b , can be computed as a matrix multiplication using the cross-product matrix of a \mathbf{a} a :
a × b = [ a × ] b \mathbf{a} \times \mathbf{b} = [\mathbf{a}_\times]\mathbf{b} a × b = [ a × ] b The matrix A = [ a × ] A = [\mathbf{a}_\times] A = [ a × ] is a Skew-symmetric matrix , meaning A T = − A A^T = -A A T = − A .
Applying the Axis-Angle Rotation to a Point ¶ Applying the rotation R R R to a point p \mathbf{p} p is:
R p = p + sin θ ( k ^ × p ) + ( 1 − cos θ ) k ^ × ( k ^ × p ) R\mathbf{p} = \mathbf{p} + \sin\theta(\mathbf{\hat{k}}\times\mathbf{p}) + (1-\cos\theta)\mathbf{\hat{k}}\times(\mathbf{\hat{k}}\times\mathbf{p}) R p = p + sin θ ( k ^ × p ) + ( 1 − cos θ ) k ^ × ( k ^ × p )