# Quaternion Rotation Explained: What the Four Numbers Store, Why Three Angles Fail, and What Slerp Adds

How one turn around one axis becomes four numbers, what gimbal lock takes away from Euler angles, and why slerp walks the arc while a quaternion lerp cuts through it.

> quaternions · rotation · interpolation · About 7 min · Nov 2

## Key points

1. A quaternion stores one turn around one axis in four numbers: x, y and z point along the axis scaled by sin(θ/2), and w = cos(θ/2) keeps the total length at 1, so the four numbers carry exactly three degrees of freedom.
2. Three Euler angles can name any orientation, but at a particular middle angle two axes align (gimbal lock) and one direction of motion becomes unreachable; interpolating the three numbers can also send a rotation 180° the long way to travel 20°.
3. Four constrained numbers beat three free ones because orientations form a curved space with no seam, while any three-dial scheme breaks somewhere — the price is that q and −q mean the same rotation.
4. Because of that sign ambiguity, a full 360° turn gives −q rather than q, so quaternions must be compared by angle, not component by component.
5. Quaternion.Lerp blends the four numbers and renormalizes, which is exact at the ends and halfway but eases in between (about 37° instead of 45° a quarter into a 180° turn); slerp walks the arc so t is a fraction of the angle, and flips the sign to take the shorter path unless the no-invert variant is used.
6. Euler angles are a readable display format, not a working one: use them for UI, keep quaternions for anything that accumulates or interpolates rotation.

---

Ask an AI tool for a character that turns to face the direction it walks, or a camera that swings smoothly behind the player, and the code usually looks like this:

```csharp
transform.rotation = Quaternion.Slerp(transform.rotation, target, 0.1f);
```

Then you click the object in the editor and the inspector offers three numbers in degrees — rotation X, Y, Z — and typing 90 into Y visibly turns it. The engine clearly understands three numbers. So why does the code hand it four?

The three numbers are what the engine *shows* you. The four are what it *keeps*. Why it keeps them tells you when to write `slerp` instead of `lerp`.

## Three dials, and the pose they can't reach

Start with the version that feels obvious. **Euler angles** describe a 3D orientation as three ordered turns: pitch about X, yaw about Y, roll about Z. One angle was enough to spin a 2D sprite, so three angles for three ways to tilt sounds right.

The trap is that the turns happen one after another, and each one carries the previous ones with it. Turn an object 90° about Y and its X axis is no longer the world's X axis, so "rotate 30° about X" now means something different than it did a moment ago. Order therefore matters: (30°, 60°, 0°) is not the pose (60°, 30°, 0°) describes.

The worse consequence: at one particular middle angle, two of the three axes line up and two dials start doing the same job. The name for this is **gimbal lock** [2].

The game version: a first-person camera pitches up until it looks straight up. Its viewing axis is now vertical — the same line the yaw dial turns around. So "turn left" and "roll your head" have become the same movement. You command a yaw change and the picture doesn't turn, it spins around the middle of the screen. The camera can still pitch and still roll, but one of the three directions of motion is now unreachable through any combination of the three numbers.

That is not an engine bug and not a mistake in the generated code. It is what describing orientation with three ordered numbers costs, and no clever choice of axis order removes it — every order has its own bad angle somewhere [2].

![Three nested gimbal rings around a small aircraft: when two rings end up with parallel axes, one direction of motion is no longer available.](https://upload.wikimedia.org/wikipedia/commons/4/49/Gimbal_Lock_Plane.gif)

## The second thing three angles get wrong

Even far from gimbal lock, the three numbers lie about rotation in a quieter way: they don't blend honestly.

A turret should swing from 170° to −10°, a 20° turn the short way round. The numbers say 170 and −10, so a lerp between them travels 180° across the dial and lands on the far side of the circle. The pose at the end is right; the motion is a half turn to get 20° away.

So the three-number version fails twice over: a pose the dials can't reach, and a path between poses that spins the wrong way. Both come from the same source — the numbers *describe* a pose, and you can't safely do arithmetic on a description.

## What the four numbers actually hold

Here is the fact that unlocks the whole thing: every rotation in 3D, however it was assembled, is a single turn by some angle around one fixed axis [1]. You don't need three dials; you need to name a line in space and say how far to turn around it.

A **quaternion** packs exactly that into four numbers. With `(x, y, z)` the unit vector along the axis and `θ` the angle in radians:

$$q = \left(\cos\frac{\theta}{2},\ \sin\frac{\theta}{2}x,\ \sin\frac{\theta}{2}y,\ \sin\frac{\theta}{2}z\right) = (w, x, y, z)$$

Read it in two halves. `x, y, z` point along the axis of the turn, and their *length* is `sin(θ/2)` — short for a small turn, longest at half a turn. The fourth number `w = cos(θ/2)` is the balancing term that keeps the whole thing on the unit sphere, so that

$$w^2 + x^2 + y^2 + z^2 = 1$$

Spin an object about Y in code and the four numbers appear directly:

```gdscript
var q := Quaternion(Vector3.UP, deg_to_rad(90))
print(q)  # (0, 0.707107, 0, 0.707107) — printed as x, y, z, w
```

The axis is (0, 1, 0), so `x` and `z` respond to nothing and all the turning shows up in `y` and `w`. A short table of turns about Y makes the pattern obvious:

| Turn about Y | Quaternion `(w, x, y, z)` |
| --- | --- |
| 0° (no rotation) | (1, 0, 0, 0) |
| 10° | (0.996, 0, 0.087, 0) |
| 90° | (0.707, 0, 0.707, 0) |
| 180° | (0, 0, 1, 0) |
| 350° | (−0.996, 0, 0.087, 0) |

Notice the last row: turning 350° gives almost the negative of "no rotation", and 360° gives exactly `(−1, 0, 0, 0)`. That is the halved angle at work. The surface of a 4D unit sphere covers the space of 3D rotations twice over, so **two quaternions that are negatives of each other mean the same orientation** — `q` and `−q` rotate an object identically [1]. Walking halfway around the sphere turns you all the way around your axis; walking all the way lands on `−q`; only two full walks bring you back to `q`.

Two consequences fall straight out of that: comparing quaternions component by component is meaningless, and interpolating between two of them means choosing between two arcs. (Component order is also not universal — formulas put `w` first, Godot prints `x, y, z, w`, Unity's constructor takes `w` last — so print one before trusting an order you copied.)

## Why four numbers beat three

Four numbers, one equation between them, so three degrees of freedom — the same information the three angles carried. Nothing extra is stored. The gain is *shape*.

The set of all orientations is curved. Three independent dials make a flat grid over that curve, and every flat map of a curved surface has to break somewhere: gimbal lock is that break. Four numbers with a length constraint sit on the curve itself, so there is no seam to fall through. That's the trade: you give up human-readable degrees and accept the sign ambiguity where `q` and `−q` both work, and in return no pose is unreachable and no direction of motion disappears.

## What slerp does that lerp can't

The previous article established that `lerp(a, b, t)` returns the point a fraction `t` of the way from `a` to `b`. For rotations, `a` and `b` are points on that 4D sphere, so blending the four numbers one at a time draws a straight line *through the inside of the sphere* — and points inside aren't orientations at all.

Engines patch this by renormalizing the result back onto the surface. That is precisely what Unity's `Quaternion.Lerp` does: "interpolates between `a` and `b` by `t` and normalizes the result afterwards" [3]. It lands on the right pose at the ends and halfway, but the angle covered per unit of `t` is uneven — Unity's own docs warn that "the angular velocity will not be constant if the rotations are far apart" [3].

Run the numbers on a 180° turn a quarter of the way through. `slerp` gives exactly 45°, because it walks the arc and `t` is a fraction of the *angle*. Component-wise lerp plus normalization gives about 36.9°. Both are at 90° halfway, so the shape is: slightly slow at the start, slightly fast at the finish — an ease-in nobody asked for. Over a 90° turn the same error is under 1°, invisible; over a half turn it's 8°, and "swing round in 0.4 seconds" visibly lurches at the end.

**Slerp** is spherical linear interpolation: same three arguments, but `t` measures progress along the arc, so equal steps in `t` are equal steps in degrees [4][5]. It also settles the sign question — if the two quaternions sit more than a quarter turn apart on the sphere, which means the rotations differ by more than 180°, it flips one and takes the other way round. Godot exposes that decision: `slerp` checks for the shorter path, `slerpni` ("no invert") skips the check and can spin the long way [5]. Generated code that reaches for `slerpni` is how a turret ends up turning 340° to move 20°.

## What this changes in the code you're directing

- **Keep rotations as quaternions; read Euler angles only for humans.** Unity's docs say values read back from `eulerAngles` may differ from what you set, and recommend against incrementing them to animate [6]. That's the real answer to "quaternion to euler": it's a display format, not a working one.
- **A timed turn** is `slerp(from, to, clamp(elapsed / duration, 0, 1))` — the same time-driven `t` as the earlier lerp pattern.
- **A chase toward a moving target** is `slerp(current, target, weight)` every frame, and it inherits the same frame-rate problem as [`position = lerp(position, target, 0.1)`](https://eulore.ai/articles/lerp-e50ac7a2), so a weight built as `1 - exp(-k * delta)` stays consistent across machines.
- **Check angles, not components.** With `q` and `−q` being the same pose, `==` on four floats will eventually be false for a rotation that hasn't changed. Compare the angle between two rotations against a tolerance instead.
- **Never average or scale quaternions by hand.** "Half of this rotation" isn't half of each number; that's what `slerp` to the other end is for.

The one-sentence version: three angles are a readable label for a pose, a quaternion is a set of coordinates you can actually do arithmetic on, and `slerp` is the arithmetic that respects the fact that rotations live on a curve.

## Mind map

```mermaid
flowchart LR
  Q["Quaternion: four numbers for one rotation"] --> A["xyz = axis direction, length sin of half angle"]
  Q --> B["w = cos of half angle, keeps length 1"]
  Q --> C["Why not three Euler angles"]
  C --> C1["gimbal lock at a 90 degree middle turn"]
  C --> C2["numbers wrap: 170 to -10 spins the long way"]
  Q --> D["Double cover: q and -q are the same rotation"]
  D --> D1["compare by angle, not components"]
  D --> D2["one 360 degree turn gives -q"]
  Q --> E["slerp vs lerp"]
  E --> E1["component lerp plus normalize: cuts inside the sphere, eases"]
  E --> E2["slerp: walks the arc, constant angular speed, short path"]
  Q --> F["Practice: Euler for display, quaternion for math"]
```

## Glossary

- Quaternion：Four numbers (w, x, y, z) describing one turn around one axis, the standard way to store a 3D rotation in code.
- Euler angles：A rotation written as three ordered turns about X, Y and Z — readable for humans, but with a singular pose.
- Gimbal lock：The alignment where two of the three Euler axes become parallel, so one direction of rotation can no longer be commanded.
- Axis-angle：The description underneath any 3D rotation — which line to turn around, and how far — that a quaternion encodes.
- Unit constraint：The equation w² + x² + y² + z² = 1, which makes four numbers carry only three degrees of freedom.
- Double cover：The fact that q and −q name the same orientation, so one full 360° turn yields the negated quaternion.
- Slerp：Interpolation that walks the arc between two rotations, so the weight measures a fraction of the angle instead of a fraction of the numbers.

## Sources

1. [Wikipedia: Quaternions and spatial rotation — axis-angle encoding, the halved angle, and the q / −q double cover](https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation)
2. [Wikipedia: Gimbal lock — two gimbal axes becoming parallel and the loss of one degree of freedom](https://en.wikipedia.org/wiki/Gimbal_lock)
3. [Unity Scripting API: Quaternion.Lerp — interpolates and normalizes afterwards, angular velocity not constant when rotations are far apart](https://docs.unity3d.com/ScriptReference/Quaternion.Lerp.html)
4. [Unity Scripting API: Quaternion.Slerp — spherical linear interpolation between unit quaternions](https://docs.unity3d.com/ScriptReference/Quaternion.Slerp.html)
5. [Godot 4 Quaternion class reference — component order, axis-angle constructor, slerp vs slerpni, normalization requirement](https://docs.godotengine.org/en/stable/classes/class_quaternion.html)
6. [Unity Scripting API: Transform.eulerAngles — read-back values may differ from what was set, and shouldn't be incremented to animate](https://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html)

---

Original article: https://eulore.ai/articles/quaternion-c93fc988

> **Eulore** · Start small. Go deep.
>
> Eulore is an AI learning tool built around connected articles, each a 5–10 minute read. Ask questions as you go and help shape what comes next. That’s how this article was created.
>
> Start your own series → https://eulore.ai
