Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 50 ยท 3D Transformations and Viewing

3D Rotation: Axes, Euler Angles and Quaternions

Learn 3D rotation matrices about x, y and z, why Euler angles suffer gimbal lock, and how axis-angle and quaternions with slerp fix it, with live demos.

In 2D there is only one way to turn a shape: around a point. In 3D you can turn an object around any line through space, and the number of ways to describe that turn is surprisingly large. Rotation matrices, Euler angles, axis-angle pairs and quaternions all describe the same thing, but each has strengths and traps. A game character that spins smoothly, a camera that never flips upside down and a robot arm that follows a path all depend on choosing the right one.

This lesson builds rotation from the three basic axis rotations, shows why chaining them as angles causes the famous gimbal lock, and then introduces quaternions and spherical interpolation. It assumes you know the 4x4 matrices from 3D points and transformation matrices.

Rotation about a single axis

We use a right-handed system: x to the right, y up, z toward the viewer. A positive angle follows the right-hand rule: point your right thumb along the axis, and your fingers curl in the positive direction. Seen from the positive end of the axis looking back at the origin, that is counterclockwise.

Rotating about one axis leaves that coordinate unchanged and turns the other two like a 2D rotation:

TEXT
Rx(a) = | 1    0       0    |     Ry(a) = |  cos a  0  sin a |     Rz(a) = | cos a  -sin a  0 |
        | 0  cos a  -sin a  |             |    0    1    0   |             | sin a   cos a  0 |
        | 0  sin a   cos a  |             | -sin a  0  cos a |             |   0       0    1 |

Notice the sign of sin a in Ry is swapped compared to the other two. That is not a typo: it follows from the cyclic order x, y, z, so that "y rotates toward z" is the positive direction for Rx, "z toward x" for Ry and "x toward y" for Rz.

Every rotation matrix is orthogonal: its columns are unit vectors at right angles, its determinant is +1, and its inverse is its transpose. That makes undoing a rotation cheap: R^-1 = R^T.

Order matters

Rotations about different axes do not commute. Turn a book 90 degrees about x and then 90 degrees about y, and you get a different pose than y first, then x. With column vectors the matrix applied first sits on the right: "x, then y, then z" is Rz * Ry * Rx.

The first demo makes this visible. The red face is the cube's +z face and the dot marks one corner. Move the three sliders, then switch the order and watch the same angles produce a different pose.

Axis rotations and their order

Set X to 90 and Y to 90 in both orders and compare the red face. Same numbers, different result.

Euler angles and gimbal lock

Because any orientation can be reached by three axis rotations in a row, we can store an orientation as three numbers: Euler angles. A common convention is yaw (turn about the vertical y-axis), pitch (nod about x) and roll (tilt about z). There are twelve valid axis orders, so always document which one you use.

Euler angles are easy to understand, easy to show in an editor and compact. They have two real problems.

The first is gimbal lock. Imagine three nested rings (gimbals), each rotating about its own axis, with the object in the innermost ring. If the middle ring turns so far that the outer and inner axes line up, two of the three rings now rotate about the same line. One degree of freedom is gone, and no combination of the remaining controls can tilt the object in that missing direction. With the order Ry(yaw) * Rx(pitch) * Rz(roll), this happens at a pitch of plus or minus 90 degrees.

flowchart LR
  A["yaw<br/>(about y)"] --> B["pitch<br/>(about x)"] --> C["roll<br/>(about z)"]
  B -. "pitch = 90 degrees" .-> D["roll axis now equals yaw axis:<br/>one degree of freedom lost"]

At a pitch of 90 degrees the first and last rotations act about the same line.

The console below proves it with numbers. At a pitch of 90 degrees, different yaw and roll values produce the identical matrix as long as yaw - roll is the same.

JS Gimbal lock in numbers

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first two matrices are identical; the last two differ. Near 90 degrees the matrices are almost equal, which also causes wild jumps when you convert a matrix back to angles.

The second problem is interpolation. Blending two sets of Euler angles linearly gives paths that wobble and turn the long way around, and the result depends on the chosen axis order.

Axis-angle

Euler's rotation theorem says that every rotation in 3D, however complicated, is equivalent to a single turn by an angle theta about some axis n (a unit vector). That pair is the axis-angle form. Rodrigues' formula rotates a vector v with it:

TEXT
v' = v cos(theta) + (n x v) sin(theta) + n (n . v)(1 - cos(theta))

Axis-angle is intuitive and has no gimbal lock, but combining two axis-angle rotations is awkward. This is where quaternions shine.

Quaternions

A quaternion has four numbers: a scalar part w and a vector part (x, y, z). For a rotation by theta about the unit axis n:

TEXT
q = ( cos(theta/2),  n * sin(theta/2) )

The half angle is not a quirk; it falls out of how a quaternion rotates a vector, by "sandwiching" it: v' = q v q*, where q* is the conjugate (negate the vector part) and v is treated as a quaternion with w = 0. The vector gets multiplied twice, so each factor contributes half of the turn.

What you need to know in practice:

  • A rotation quaternion has unit length. After many multiplications, re-normalize to fight rounding drift.
  • Composition is quaternion multiplication: apply q1 and then q2 as q2 * q1. Like matrices, it is not commutative.
  • Inverse is the conjugate, as for orthogonal matrices.
  • q and -q describe the same rotation. That matters for interpolation.
  • Four numbers (instead of nine) are cheaper to store and combine, and they never lock.
  • To feed a quaternion to the GPU or to a matrix chain, convert it to a matrix:
TEXT
| 1-2(y2+z2)   2(xy-wz)    2(xz+wy)  |
|  2(xy+wz)   1-2(x2+z2)   2(yz-wx)  |
|  2(xz-wy)    2(yz+wx)   1-2(x2+y2) |

(here y2 means y squared, and so on).

Slerp: spherical linear interpolation

To animate from orientation qa to orientation qb, you want constant angular speed along the shortest turn. Unit quaternions live on the surface of a four-dimensional sphere, and the shortest path between two points on a sphere is an arc of a great circle. Slerp walks along that arc:

TEXT
slerp(qa, qb, t) = sin((1-t)W)/sin(W) * qa + sin(tW)/sin(W) * qb,    cos(W) = qa . qb

Two details make it robust. If qa . qb is negative, negate qb first (since -qb is the same rotation, this picks the short way around). If the two are almost equal, sin(W) is near zero, so fall back to a normalized linear blend.

The demo below interpolates between two orientations. The left cube uses slerp. The right cube linearly blends the two matrices entry by entry. A blend of two rotation matrices is generally not a rotation, so the cube visibly shrinks and shears in the middle.

Slerp versus blending matrices

At t = 0.5 the right-hand cube is clearly smaller: the blended matrix scales space. The slerped cube stays rigid.

Which representation should you use?

A practical rule of thumb: use Euler angles for human-facing controls (inspectors, sliders), quaternions to store and interpolate orientation, and matrices to transform many points at once, which is what the GPU wants. Most engines convert between them constantly. In the next lessons the orientation of the camera and of every object ends up as a matrix anyway.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In a right-handed system, what does a positive rotation angle about the y-axis look like when you look from the positive y end toward the origin?

    The right-hand rule: with the thumb along +y, your fingers curl counterclockwise as seen from the tip of the thumb looking back at the origin.
  2. 2. What is gimbal lock?

    When the middle rotation reaches 90 degrees (in the yaw-pitch-roll order used here), the first and last axes coincide, so changing either one has the same effect.
  3. 3. A quaternion for a rotation of 180 degrees about the y-axis has w equal to...

    w = cos(theta/2) = cos(90 degrees) = 0. The vector part is (0, 1, 0).
  4. 4. Why does slerp negate one quaternion when the dot product is negative?

    The two end points describe the same orientation either way, but interpolating toward -q takes the long way round the sphere. Flipping the sign gives the shortest turn.
  5. 5. Why is a linear blend of two rotation matrices a bad way to animate a rotation?

    The set of rotation matrices is not closed under averaging: the blend is not orthogonal, so it scales and shears space.

Summary

  • Rotation about x, y or z is a 3x3 (or 4x4) orthogonal matrix; positive angles follow the right-hand rule, and the inverse is the transpose.
  • Rotations do not commute; with column vectors the first rotation applied is the rightmost matrix.
  • Euler angles (yaw, pitch, roll) are readable but suffer gimbal lock and interpolate badly.
  • Any rotation is one turn about one axis (axis-angle); quaternions (cos(theta/2), n sin(theta/2)) encode that in four numbers, compose by multiplication and never lock.
  • Slerp gives constant-speed shortest-path interpolation; matrices are best for transforming many points.

Next, we place a camera in the scene: The Camera and View Transformation.