3d rotation matrices
In this blog post, I will explore rotation matrices, a powerful tool for handling rotations in engineering and physics. I will show how these matrices can simplify the process of rotating vectors and coordinate systems, eliminating the need for step-by-step methods like Euler angles. I will present the transformation matrices for rotations about the x, y, and z axes and how to apply them to convert vectors between different frames of reference. I will discuss the property of orthonormality, which makes inverting rotation matrices straightforward. I think that this tool is very useful for anyone working with spatial transformations.
Rotation matrices provide a very efficient way to perform rotations in 3D space. Instead of using step-by-step methods such as Euler angles, rotation matrices allow me to express a rotation as a single matrix multiplication.
Let’s start by considering a rotation about the z-axis. Imagine a frame 1 with axes \mathbf I, \mathbf J, and \mathbf K, which is rotated to a new frame 2 with axes \mathbf i, \mathbf j, and \mathbf k, through an angle \alpha about the \mathbf K (or \mathbf k) axis. A vector \mathbf V can be expressed in both frames. In frame 1, the vector is
\mathbf V = V_X \mathbf I + V_Y \mathbf J + V_Z \mathbf K
and in frame 2, the same vector becomes
\mathbf V = V_x \mathbf i + V_y \mathbf j + V_z \mathbf k
Projecting the vector \mathbf V onto the axes of frame 2, I obtain:
\begin{aligned} V_x & = \mathbf V \cdot \mathbf i = V_X \cos(\alpha) + V_Y \sin(\alpha)\\ V_y & = \mathbf V \cdot \mathbf j = -V_X \sin(\alpha) + V_Y \cos(\alpha)\\ V_z & = \mathbf V \cdot \mathbf k = V_Z \end{aligned}
These can be conveniently expressed in matrix form:
\begin{bmatrix} V_x \\ V_y \\ V_z \end{bmatrix} = \begin{bmatrix} \cos(\alpha) & \sin(\alpha) & 0 \\ -\sin(\alpha) & \cos(\alpha) & 0 \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} V_X \\ V_Y \\ V_Z \end{bmatrix}
Or in a compact form:
\mathbf V\big|_2 = \mathbf T_z \mathbf V\big|_1
where \mathbf T_z is the rotation transformation matrix about the z-axis.
In a similar fashion, it is possible to derive the matrices for the rotation about the x-axis and y-axis.
These rotation transformation matrices are orthonormal. This means that the transpose of the matrix is also its inverse: T^T = T^{-1}. This property is very useful because the inverse of a rotation matrix is easily found, which I can use when I need to go back to the original frame.
Here’s a simple example in Python for the z-axis rotation:
import numpy as np
def rotation_matrix_z(alpha):
return np.array([
[np.cos(alpha), np.sin(alpha), 0],
[-np.sin(alpha), np.cos(alpha), 0],
[0, 0, 1]
])
angle = np.pi/2
vector_frame_1 = np.array([1, 0, 0])
Tz = rotation_matrix_z(angle)
vector_frame_2 = np.dot(Tz, vector_frame_1)
print("Vector in Frame 1: ", vector_frame_1)
print("Vector in Frame 2: ", vector_frame_2)
which outputs:
Vector in Frame 1: [1 0 0]
Vector in Frame 2: [ 6.123234e-17 -1.000000e+00 0.000000e+00]
since rotating the x-axis of 90^\circ around the z-axis give the y-axis in the opposite direction. I can easily adapt the above code to any axis using the proper transformation matrix and apply this to complex calculations.
For more insights into this topic, you can find the details here.