Get AI summaries of any video or article — Sign up free
Quaternions and 3d rotation, explained interactively thumbnail

Quaternions and 3d rotation, explained interactively

3Blue1Brown·
5 min read

Based on 3Blue1Brown's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

Quaternions provide a robust representation of 3D orientation used in graphics, robotics, VR, and mobile device motion tracking.

Briefing

Quaternions matter because they provide a reliable, programmer-friendly way to represent 3D orientation—one that sidesteps the classic failure modes of Euler-angle rotations and avoids the numerical headaches that come with interpolating rotation matrices. In practice, that robustness shows up everywhere from computer graphics to robotics and virtual reality, and even in consumer hardware: software inside many phones uses quaternions to track how the device is oriented in space.

A common alternative starts with 3×3 rotation matrices and Euler angles, which describe a rotation by turning around three easy-to-understand axes. That approach works until it doesn’t. When two rotation axes line up, the system loses a degree of freedom in a phenomenon called gimbal lock. Euler angles also make interpolation between two orientations tricky, with ambiguities and edge cases that can produce inconsistent motion—especially noticeable in animation and control systems.

Quaternions avoid gimbal lock and offer a cleaner path for interpolation between orientations. They also reduce problems that arise when trying to blend rotation matrices, such as drift away from valid rotation properties and normalization issues. The practical payoff is smoother, more stable orientation handling across software systems that must update rotations repeatedly and accurately.

The core computational trick is that quaternion multiplication can encode 3D rotations in a way that mirrors how complex numbers encode 2D rotations. In 2D, a point like (4, 1) can be rotated by an angle θ by multiplying it by a unit complex number at angle θ—using only the rule i² = −1. The multiplication “just works,” producing the rotated coordinates.

In 3D, the quaternion construction follows the same spirit but with a higher-dimensional twist. To rotate by an angle around a specific axis, the axis is represented as a unit vector with components (i, j, k) whose squared components sum to 1. A quaternion is then built from the rotation angle: the real part uses cos(θ/2), while the imaginary part uses sin(θ/2) times the axis components. The appearance of θ/2 may seem arbitrary at first, but it’s essential for the algebra to match the geometry.

To apply the rotation to a 3D point, the method uses a “sandwich” product: multiply the point by the quaternion on the left and by the inverse of the quaternion on the right. With the quaternion multiplication rules for i, j, and k, expanding those products yields the rotated coordinates. The inverse on the right is what ensures the transformation behaves like a proper rotation rather than a general quaternion multiplication.

The next step is to visualize and unpack what those two quaternion multiplications are doing geometrically—why the half-angle appears, and why the inverse sits on the right. An explorable tutorial hosted at eater.net slash quaternions is positioned as the hands-on way to see the rotation computation in action.

Cornell Notes

Quaternions give a dependable way to represent 3D orientation for graphics, robotics, and devices like smartphones. Unlike Euler angles, they avoid gimbal lock when rotation axes align, and unlike rotation matrices, they reduce interpolation ambiguity and normalization/precision issues. The rotation mechanism parallels complex numbers: a unit quaternion built from an axis and an angle rotates a point via a “sandwich” product q * p * q^{-1}. The construction uses half the rotation angle (cos(θ/2), sin(θ/2)·axis), which is crucial for the algebra to match the intended geometric rotation. This makes quaternion-based orientation updates both stable and smooth.

Why do Euler angles and rotation matrices often cause trouble in 3D orientation tasks?

Euler angles describe rotation by turning around three axes, but they can suffer gimbal lock: when two axes line up, the system loses a degree of freedom. They also make interpolation between two orientations ambiguous and prone to edge cases. Rotation matrices can interpolate too, but blending them can introduce numerical drift—rotations may stop behaving like valid rotations unless additional care (like normalization) is taken.

How do quaternions avoid gimbal lock and interpolation ambiguity?

Quaternions represent orientation in a way that doesn’t rely on sequential axis rotations, so the “axes line up” failure mode of Euler angles doesn’t occur. For interpolation, quaternion methods provide a seamless transition between orientations without the ambiguities that show up when interpolating Euler angles, and without the precision/normalization problems that can arise when interpolating rotation matrices.

What is the 2D analogy that helps explain quaternion rotations?

Complex numbers encode 2D rotations by multiplying a point (as a complex number) by a unit complex number at angle θ. Using only i² = −1, the product yields the rotated coordinates. Quaternions extend the same idea to 3D: a quaternion built from an axis and angle acts like the “rotation factor,” and quaternion multiplication performs the transformation.

How is a quaternion constructed from an axis and an angle?

First represent the rotation axis as a unit vector with components (i, j, k) normalized so i² + j² + k² = 1. Then build the quaternion using the half-angle: the real part is cos(θ/2), and the imaginary part is sin(θ/2) times the axis components. The θ/2 choice is essential for the quaternion algebra to correspond to the intended 3D rotation.

What does the quaternion “sandwich” product do to a point?

To rotate a 3D point p, compute q * p * q^{-1}. The left multiplication applies the quaternion’s rotation structure, and the right multiplication by the inverse corrects the transformation so the net effect is a proper rotation. Expanding the products using the i, j, k multiplication rules yields the rotated coordinates.

Where does quaternion-based orientation show up in real devices?

A cited example is code used across hundreds of millions of devices to track a phone’s model orientation in space. The implication is that smartphone software relies on quaternions to update how the device is oriented.

Review Questions

  1. How does gimbal lock arise from Euler-angle rotations, and why is it avoided by quaternion representations?
  2. Write the quaternion rotation procedure for a point p using q and q^{-1}, and explain where the half-angle θ/2 enters.
  3. Compare the main interpolation problems of Euler angles and rotation matrices with the interpolation advantages attributed to quaternions.

Key Points

  1. 1

    Quaternions provide a robust representation of 3D orientation used in graphics, robotics, VR, and mobile device motion tracking.

  2. 2

    Euler-angle rotations can fail via gimbal lock when two rotation axes align, causing a lost degree of freedom.

  3. 3

    Interpolating Euler angles can produce ambiguities and edge cases, while interpolating rotation matrices can suffer from numerical precision and normalization drift.

  4. 4

    A quaternion rotation is built from a unit axis vector and an angle using cos(θ/2) and sin(θ/2)·axis components.

  5. 5

    Applying a quaternion rotation to a point uses the sandwich product q * p * q^{-1}, with the inverse placed on the right to ensure a proper rotation.

  6. 6

    The quaternion method parallels complex-number rotation in 2D, where multiplying by a unit complex number rotates a point using i² = −1.

Highlights

Quaternions sidestep gimbal lock and make orientation interpolation smoother and less ambiguous than Euler angles.
Smartphones rely on quaternion-based code to track device orientation across hundreds of millions of units.
The 3D rotation formula uses a half-angle inside the quaternion and applies the rotation via q * p * q^{-1}.
Quaternion rotations mirror complex-number rotations: multiplication by a unit “rotation factor” produces rotated coordinates.

Topics

  • Quaternions
  • 3D Rotation
  • Euler Angles
  • Gimbal Lock
  • Quaternion Multiplication

Mentioned