Chapter 55: orientation in 3D

Euler Angles and Quaternions Two ways to describe how a robot is turned

Every transform in ROS 2 carries a rotation made of four numbers: x, y, z and w. This page shows where those four numbers come from. It starts with simple turns around one axis and the rotation matrices built from them, then builds the quaternion step by step, with real numbers at every step, and ends with why computers prefer it.

Words used on this page (open this if a word is new)
frameThree arrows X, Y and Z that mark a point of view.
orientationHow one frame is turned compared to another.
Euler anglesThree angles, one turn around each axis: yaw, pitch and roll.
yaw, θ (theta)A turn around the Z axis (up).
pitch, φ (phi)A turn around the Y axis (to the left of the robot).
roll, ψ (psi)A turn around the X axis (forward).
rotation matrixA 3 by 3 grid of numbers that turns any point.
matrix productRows times columns: the way two matrices are combined.
complex numbera + bi, with a real part a and an imaginary part b.
iThe imaginary unit: i · i = minus 1.
quaternionq = a + bi + cj + dk: four numbers that describe a turn.
unitarya² + b² + c² + d² = 1.
inverseThe turn that cancels a turn.
gimbal lockAt a pitch of 90° yaw and roll turn around the same line.
01

Two ways to describe a turn

Every part of a robot has its own frame, and the orientation says how that frame is turned. There are two tools for writing an orientation down: Euler angles with rotation matrices, and quaternions. Use Next and Back, the sliders and the buttons. Point at a coloured word to light up its part of the picture.

Key idea. Euler angles (with rotation matrices) and quaternions do the same job: they describe how one frame is turned compared to another. Whenever a computer has to store and combine orientations, in robotics, computer games, computer vision, animation and special effects, quaternions are preferred.

Quick check

02

One turn at a time: yaw, pitch and roll

A turn around a single axis is the easiest turn there is. It only needs sin and cos of one angle. Use Next and Back, the sliders and the buttons. Point at a coloured word to light up its part of the picture.

Key idea. A turn around one axis is an elementary rotation, written as a 3 by 3 matrix of cos and sin. Around Z it is yaw (θ), around Y pitch (φ), around X roll (ψ). These three angles are the Euler angles.

Quick check

03

Multiplying two matrices, one number at a time

To put two turns together, their matrices are multiplied. Here is exactly how, and how much work it is. Use Next and Back, the sliders and the buttons. Point at a coloured word to light up its part of the picture.

Key idea. Each number of a matrix product is a row times a column: 3 products and 2 sums. A 3 by 3 result has 9 numbers, so combining two rotation matrices costs 27 products and 18 sums. The order of the matrices matters.

Quick check

04

Three turns in one matrix

Any orientation can be split into yaw, pitch and roll, and their three matrices multiply into one rotation matrix. Use Next and Back, the sliders and the buttons. Point at a coloured word to light up its part of the picture.

Key idea. R = Rz(θ) · Ry(φ) · Rx(ψ) holds the whole orientation in 9 numbers, r11 to r33. With rotation matrices every orientation costs 9 numbers of memory.

Quick check

05

A number that turns: a + bi

Quaternions grow out of a special kind of number. It starts with one new number, i. Use Next and Back, the sliders and the buttons. Point at a coloured word to light up its part of the picture.

Key idea. A complex number a + bi has a real part a and an imaginary part b. Multiplying by i turns a point a quarter turn around 0, and i · i = minus 1. Numbers like these can describe turning.

Quick check

06

The quaternion: four numbers for a turn

A quaternion extends a + bi with three imaginary parts, one for each axis of space. Use Next and Back, the sliders and the buttons. Point at a coloured word to light up its part of the picture.

Key idea. q = a + bi + cj + dk: a is the scalar part, b, c, d the vector part. For a turn by θ around an axis, a = cos(θ/2) and (b, c, d) = sin(θ/2) times the axis. It is always unitary: a² + b² + c² + d² = 1. ROS 2 writes it as x, y, z, w.

Quick check

07

Why computers prefer quaternions

Both tools describe the same turns, so the choice comes down to cost: memory, work, and how easy a turn is to undo. Use Next and Back, the sliders and the buttons. Point at a coloured word to light up its part of the picture.

Key idea. A quaternion needs 4 numbers instead of 9, combines two turns with 16 products and 12 sums instead of 27 and 18, and is undone by flipping three signs. On top of that it has no gimbal lock.

Quick check

08

Think in angles, store a quaternion

People think in angles, computers in quaternions. A conversion connects the two. Use Next and Back, the sliders and the buttons. Point at a coloured word to light up its part of the picture.

Key idea. Choose roll, pitch and yaw, because they are easy to picture, then convert them into a quaternion for ROS 2. The conversion works both ways.

Quick check

09

Summary and quiz

Key takeaways

  1. Euler angles and quaternions are two equivalent ways to describe how one frame is turned compared to another.
  2. Euler angles split a turn into three elementary rotations: yaw (θ) around Z, pitch (φ) around Y and roll (ψ) around X. Each is a 3 by 3 matrix of cos and sin.
  3. Multiplying the three matrices gives one rotation matrix R = Rz(θ) · Ry(φ) · Rx(ψ) with 9 numbers to store.
  4. Combining two rotation matrices takes 27 products and 18 sums, and the order matters.
  5. A complex number a + bi has a real and an imaginary part; multiplying by i is a quarter turn, and i · i = minus 1.
  6. A quaternion q = a + bi + cj + dk has a scalar part a and a vector part b, c, d. It needs only 4 numbers.
  7. It is unitary (a² + b² + c² + d² = 1), two turns are combined by multiplying two quaternions (16 products and 12 sums), and the inverse is just a change of sign of b, c and d.
  8. People think in Euler angles, computers store quaternions: choose angles, convert them, and give the quaternion to ROS 2.

Quiz