Linear Algebra · Theory · M7
M7-T3 — Three-Dimensional Rotation Matrices
In 3D a rotation is three numbers, not one, and turning about x then z is not the same as z then x.
Every orientation of a robot, drone or camera can be reached by a sequence of turns about coordinate axes. The matrices are simple extensions of the 2D rotation, but their products reveal something new: 3D rotations do not commute, so the order in which they are applied is part of the definition of an orientation.
Learning objectives
After this lesson, you should be able to:
- Write Rx, Ry and Rz and explain the pattern of each.
- Compose rotations about axes and show by a numeric example that the order matters.
- Distinguish rotating about fixed (world) axes from rotating about the moving (body) axes.
- Read the columns of a 3D rotation matrix as the body axes expressed in the world.
Introduction and motivation
A quadrotor tilts forward by 10° (pitch) and yaws 30°. A camera on a gimbal rolls and pans. Describing where such a body points means writing a 3 × 3 matrix, and a wrong order of multiplication gives a drone that tilts the wrong way or a map where points are rotated in a weird twist. In ArduPilot, PX4 or ROS the same roll, pitch, yaw triple appears everywhere; this lesson makes its meaning exact.
The intuition
Turning about the z-axis leaves z fixed and rotates x and y exactly like the 2D case. The same is true for the other two axes, each fixing its own coordinate. Any orientation is a product of such turns. Because you rotate the whole body each time, the second turn happens about axes that the first turn has already moved, or about the fixed world axes, depending on how you describe it, and the results differ.
Interactive figure. An oblique wireframe box with body axes x prime, y prime and z prime rotated by roll, pitch and yaw. The readout shows the three by three rotation matrix, its determinant, and the effect of reversing the order of the turns. This figure needs JavaScript to be interactive.
Geometric and physical interpretation
Geometric view. Rz(ψ) turns the xy-plane about the vertical axis, Ry(θ) turns the xz-plane about the y-axis, Rx(φ) turns the yz-plane about the x-axis, each counter-clockwise looking from the positive end of its axis towards the origin (right-hand rule: thumb along the axis, fingers curl in the positive direction). Physical view. For a body with x forward, y left, z up: yaw ψ about z turns left, pitch θ about y points the nose down for positive θ (because of the right-hand rule), and roll φ about x tilts the right wing down.
The mathematics
- φ, θ, ψ
- roll, pitch and yaw angles about the x, y and z axes.
- Rx, Ry, Rz
- the elementary rotation matrices.
- R
- a general 3D rotation; its columns are the body axes written in the world frame.
Elementary rotations
Rx(φ) = [1 0 0; 0 cos φ −sin φ; 0 sin φ cos φ] Ry(θ) = [cos θ 0 sin θ; 0 1 0; −sin θ 0 cos θ] Rz(ψ) = [cos ψ −sin ψ 0; sin ψ cos ψ 0; 0 0 1]
What it means. Each contains the 2D rotation in the plane perpendicular to its axis and an identity entry for the axis itself. The signs of Ry look different because the cyclic order of the axes is z → x → y.
Composition
R = Rz(ψ) Ry(θ) Rx(φ) and in general R₁R₂ ≠ R₂R₁
What it means. Read right to left when applied to a vector: first roll about the world x, then pitch about the world y, then yaw about the world z. Equivalently, reading left to right: yaw, then pitch about the new y, then roll about the new x (intrinsic order). The two descriptions give the same matrix.
Each axis rotation fixes its own axis. A general rotation is a product of three of them, and the order matters.
Step-by-step derivation: Rz, the column reading and non-commutativity
- Rz. A rotation about the z-axis leaves
e₃ = (0, 0, 1)alone, so column 3 is(0, 0, 1). It rotatese₁ande₂in the xy-plane by ψ, exactly as in 2D, giving columns(cos ψ, sin ψ, 0)and(−sin ψ, cos ψ, 0). - Rx and Ry. The same argument with the roles of the axes permuted (x fixed; y→z; and y fixed; z→x) gives the other two matrices. The cyclic pattern x → y → z → x explains why Ry has the sine signs swapped: z is turned into x, not x into z.
- Orthogonality. Each matrix has orthonormal columns, so
RᵀR = I, and a product of orthogonal matrices is orthogonal:(R₁R₂)ᵀ(R₁R₂) = R₂ᵀR₁ᵀR₁R₂ = I. Determinants multiply, so the product has determinant +1: it is a rotation. - The columns.
R e₁is the first column: where the original x-axis ends up. If R describes the pose of a body, the three columns are the body x, y and z axes written in the world frame (the previous lessons:R_WB = [b₁ b₂ b₃]). - Non-commutativity. Compare
Rz(90°)Rx(90°)e₁andRx(90°)Rz(90°)e₁. First:Rx(90°) e₁ = e₁, thenRz(90°) e₁ = e₂, giving(0, 1, 0). Second:Rz(90°) e₁ = e₂, thenRx(90°)e₂ = e₃, giving(0, 0, 1). Different results, soRz Rx ≠ Rx Rz.
Fixed versus moving axes. Applying Rz(ψ) after the current orientation R as Rz R rotates about the fixed world z-axis (premultiplication). Applying it as R Rz rotates about the body's own current z-axis (postmultiplication). Remember: pre-multiply for world axes, post-multiply for body axes.
Small angles. For small angles cos ≈ 1 and sin ≈ angle, so R ≈ I + [ω]ₓ, with [ω]ₓ the skew-symmetric matrix of the angle vector. To first order the order does not matter, which is why small attitude errors can be added; Module 10 makes this precise.
A worked example: composing roll, pitch and yaw
- Order matters. With
Rz(90°) = [[0, −1, 0], [1, 0, 0], [0, 0, 1]]andRx(90°) = [[1, 0, 0], [0, 0, −1], [0, 1, 0]]:Rz Rx e₁ = (0, 1, 0)butRx Rz e₁ = (0, 0, 1). - Matrix products differ.
Rz(90°)Rx(90°) = [[0, 0, 1], [1, 0, 0], [0, 1, 0]], whereasRx(90°)Rz(90°) = [[0, −1, 0], [0, 0, −1], [1, 0, 0]]. - A pure yaw. Rotate the point
(1, 0, 0)by 90° yaw:(0, 1, 0). The z-coordinate of any point never changes underRz. - Properties of a product. For
R = Rz(30°) Ry(20°) Rx(10°)the determinant is 1 andRᵀR = Ito rounding; the first column is(0.8138, 0.4698, −0.3420), the direction the body's x-axis (nose) points in the world. - Reading the nose. A nose pointing along
(0.8138, 0.4698, −0.3420)has a downward component of −0.342 = −sin 20°: pitch 20° tilts the nose down, as the right-hand rule predicts.
Robotics application
UAVs. Attitude is reported as roll, pitch and yaw and applied as R = Rz Ry Rx to convert body-frame thrust and sensor vectors to the world; see attitude representation and rotation matrices.
Mobile robots on slopes. A ground robot's IMU gives roll and pitch; rotating a 3D LiDAR scan by them levels the point cloud before ground segmentation; see rotations and transforms in 3D.
Manipulators. The orientation of each link is a product of joint rotations about their axes, in the order of the kinematic chain (Module 8).
IMU and gimbals. Integrating gyro rates composes small rotations in body order; a gimbal's roll, pitch and pan angles are literally the factors of such a product.
Engineering implications and limitations
Practical rules
- State the convention in writing. Which axes, which order (ZYX is common), intrinsic or extrinsic, radians or degrees, passive or active. Two libraries with different conventions give silent errors.
- Pre-multiply for fixed axes, post-multiply for body axes. Most incremental-update bugs are this single swap.
- Check with the columns. The first column of R is where the robot nose points; plot it and compare with the physical robot.
- Re-orthonormalise after many products. Floating-point drift makes RᵀR ≠ I; project back with the SVD (next lesson) or use quaternions for long integrations.
- Do not interpolate matrix entries. A linear blend of two rotation matrices is not a rotation. Interpolate quaternions or the axis-angle instead.
Limits
Three-angle descriptions have singular configurations (gimbal lock, lesson 8) and angle jumps at ±180°. The matrix itself has no such problem; it just has nine numbers for three degrees of freedom, which is why constraints matter (next lesson).
Common misconceptions
“3D rotations commute like 2D ones.”
Only rotations about the same axis do. Rz(90°)Rx(90°) ≠ Rx(90°)Rz(90°).
“Roll, pitch, yaw are three independent angles you can apply in any order.”
They are only meaningful with a fixed convention for the order and the axes.
“The matrix for pitch looks wrong because its sine signs differ.”
It is correct: the cyclic order z → x makes Ry have the sin θ entry in the top right.
“Averaging two rotation matrices gives a rotation halfway between them.”
The average is generally not orthogonal. Use quaternion interpolation (slerp).
Engineering takeaways
- Rx, Ry and Rz each fix their own axis and rotate the plane perpendicular to it.
- A general orientation is a product such as Rz(ψ)Ry(θ)Rx(φ); matrix multiplication order is part of the definition.
- 3D rotations do not commute: Rz(90°)Rx(90°) ≠ Rx(90°)Rz(90°).
- The columns of R are the body axes in the world; pre-multiply for fixed-axis turns and post-multiply for body-axis turns.
- State the convention (order, intrinsic/extrinsic, units) in every interface.
References and further reading
Primary papers and authoritative documentation.
- Buss, Introduction to Inverse Kinematics with Jacobian Transpose, Pseudoinverse and Damped Least Squares: introduction to 3D rotations: rotation matrices about the axes; the site did not load when we last checked
- Modern Robotics (Lynch and Park), free preprint: chapter 3.2, rotation matrices
- Barfoot, State Estimation for Robotics (author-hosted first edition, free to read): chapter 6, rotations and poses
- UCSD ECE276A: Sensing and Estimation in Robotics: 3D rotations and their parameterisations
Knowledge check
Three conceptual questions. Write an answer, then reveal the explanation. Your answers stay in your browser.
Question 1
Write Ry(90°) and use it to rotate the vector (1, 0, 0). Where does the x-axis go?
Explanation
Ry(90°) = [[0, 0, 1], [0, 1, 0], [−1, 0, 0]]. Applied to (1, 0, 0) it gives (0, 0, −1): the x-axis points straight down. This is the “nose down by 90°” case that causes gimbal lock.
Question 2
A drone yaws 90° and then rolls 90°, both about its own axes (intrinsic). Write the orientation matrix and say where its body x-axis points in the world.
Explanation
Intrinsic turns post-multiply: R = Rz(90°) Rx(90°) = [[0, 0, 1], [1, 0, 0], [0, 1, 0]]. The first column is (0, 1, 0): the nose points along world +y (roll about the nose does not change where it points).
Question 3
Why is (Rz(30°) + Rz(60°))/2 not a valid rotation?
Explanation
Its columns have length cos 15° = 0.966 rather than 1, so RᵀR ≠ I and the determinant is below 1. The average shrinks the vectors; the correct halfway rotation is Rz(45°), obtained from the angle (or by slerp on quaternions).
What’s next
The set of all 3D rotation matrices has a precise structure: orthogonal with determinant +1, closed under multiplication and inverse. That structure, called SO(3), is what makes rotations safe to compose and what lets us repair a drifting matrix.
Related content
Nothing below is required to finish this lesson.
- BeforeM7-T2: Two-Dimensional Rotations
Write and use a 2D rotation matrix, and derive its properties.
- BeforeM2-T5: Composition of Transformations
Combine two transformations into one matrix product and predict the effect of changing the order.
Get notified when the next lesson and new labs are released.
One email per new lesson or lab.