Skip to the lesson
FixTheRobot Explore Robotics / Linear Algebra / M7 Rotations and Rigid-Body Transformations / M7-T3

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.

  • Lesson 3 of 9
  • Module 7: Rotations and Rigid-Body Transformations
  • Rotations and rigid bodies
  • About 8 minutes to read

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.

Rotations about the three axesThree panels show the elementary rotation matrices Rx, Ry and Rz. In each the row and column belonging to the rotation axis are those of the identity, and a two by two rotation block sits in the other two coordinates.Rx(φ) rollRy(θ) pitchRz(ψ) yaw 1   0   00   cφ   −sφ0   sφ   cφ cθ   0   sθ0   1   0−sθ   0   cθ cψ   −sψ   0sψ   cψ   00   0   1 c = cos, s = sin. The axis of rotation is the row and column left as in the identity. Note the sign of the sin θ entries in Ry: it is flipped relative to the others (cyclic order z → x).
The three elementary rotations: each leaves one axis fixed and rotates the other two with a 2 × 2 rotation block.

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.

Set roll, pitch and yaw with the sliders. The box and its coloured axes show the rotated body; the faint arrows are the world axes. Then tick the box to apply the same three turns in the opposite order and compare the matrices: they differ, because 3D rotations do not commute.

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

  1. Rz. A rotation about the z-axis leaves e₃ = (0, 0, 1) alone, so column 3 is (0, 0, 1). It rotates e₁ and e₂ in the xy-plane by ψ, exactly as in 2D, giving columns (cos ψ, sin ψ, 0) and (−sin ψ, cos ψ, 0).
  2. 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.
  3. 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.
  4. 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₃]).
  5. Non-commutativity. Compare Rz(90°)Rx(90°)e₁ and Rx(90°)Rz(90°)e₁. First: Rx(90°) e₁ = e₁, then Rz(90°) e₁ = e₂, giving (0, 1, 0). Second: Rz(90°) e₁ = e₂, then Rx(90°)e₂ = e₃, giving (0, 0, 1). Different results, so Rz 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]] and Rx(90°) = [[1, 0, 0], [0, 0, −1], [0, 1, 0]]: Rz Rx e₁ = (0, 1, 0) but Rx Rz e₁ = (0, 0, 1).
  • Matrix products differ. Rz(90°)Rx(90°) = [[0, 0, 1], [1, 0, 0], [0, 1, 0]], whereas Rx(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 under Rz.
  • Properties of a product. For R = Rz(30°) Ry(20°) Rx(10°) the determinant is 1 and RᵀR = I to 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

  1. Rx, Ry and Rz each fix their own axis and rotate the plane perpendicular to it.
  2. A general orientation is a product such as Rz(ψ)Ry(θ)Rx(φ); matrix multiplication order is part of the definition.
  3. 3D rotations do not commute: Rz(90°)Rx(90°) ≠ Rx(90°)Rz(90°).
  4. The columns of R are the body axes in the world; pre-multiply for fixed-axis turns and post-multiply for body-axis turns.
  5. State the convention (order, intrinsic/extrinsic, units) in every interface.

References and further reading

Primary papers and authoritative documentation.

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.

Next lesson

M7-T4 — SO(3)

State what makes a matrix a rotation (orthogonal, determinant +1) and why such matrices form a group.

Nothing below is required to finish this lesson.