Rotation Matrices

How to 3D

Chapter 4: Transformation Machinery

Rotation Matrices

Earlier you saw that rotation around the z-axis could be expressed with these dot products:

$$\begin{aligned} p'_x &= \begin{bmatrix}\cos a & -\sin a & 0\end{bmatrix} \cdot \mathbf{p} \\ p'_y &= \begin{bmatrix}\sin a & \cos a & 0\end{bmatrix} \cdot \mathbf{p} \\ p'_z &= \begin{bmatrix}0 & 0 & 1\end{bmatrix} \cdot \mathbf{p} \end{aligned}$$

As with scaling, we add 0 to the first three vectors to cancel the homogeneous coordinate of \(\mathbf{p}\). We also add a fourth dot product to produce the homogeneous coordinate of \(\mathbf{p'}\) and assemble all four vectors into this matrix:

$$\begin{aligned} \begin{bmatrix} \cos a & -\sin a & 0 & 0 \\ \sin a & \cos a & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \end{aligned}$$

Rotating around the z-axis now becomes a matrix-vector multiplication, just like translation and scaling:

$$\begin{aligned} \begin{bmatrix}p'_x \\ p'_y \\ p'_z \\ 1\end{bmatrix} &= \begin{bmatrix} \cos a & -\sin a & 0 & 0 \\ \sin a & \cos a & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \times \begin{bmatrix}p_x \\ p_y \\ p_z \\ 1\end{bmatrix} \\ \end{aligned}$$

When we're stuck in 2D, rotating around the z-axis is the only kind of rotation possible. However, when we move into the third dimension, we have many more axes around which we may rotate. Suppose we want to rotate around the x-axis. The matrix is very similar to the one for rotating around the z-axis:

$$\begin{aligned} \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos a & -\sin a & 0 \\ 0 & \sin a & \cos a & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \end{aligned}$$

The matrix for rotating around the y-axis is also similar:

$$\begin{aligned} \begin{bmatrix} \cos a & 0 & -\sin a & 0 \\ 0 & 1 & 0 & 0 \\ \sin a & 0 & \cos a & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \end{aligned}$$

Notice how the sines and cosines follow the same rectangular arrangement in all three matrices. That's because they are constructed using the same polar coordinate logic we applied for rotating around z. The only difference is that we are looking at the problem from a different axis.

Observe how each matrix contains a row of three 0s and one 1. Let's consider what impact that row has on the rotation:

When the row of 0s and 1 is dotted with \(\mathbf{p}\), it just echoes out the incoming component unchanged. This means that rotating around a cardinal axis is effectively a 2D rotation, changing only two of the three components.

We can also rotate around non-cardinal axes. This is a much more complex operation, and we'll explore it in another chapter. For now, let's step back and figure out why matrices have become the tool of choice for representing transformations.

← Scale MatrixWhy Matrices →