TExcelTAD
Home › Math for Games › Euler Angles and Gimbal Lock

Euler Angles and Gimbal Lock

The most human way to describe a 3D orientation is three angles: yaw (turn left/right around the up axis), pitch (look up/down around the right axis) and roll (tilt the head around the forward axis). These are Euler angles, and every engine's inspector shows rotations this way. They are great for editing and terrible for computing. This lesson shows why.

Order matters

Three angles mean three rotations, applied one after another. Because 3D rotations don't commute, the same three angles give different orientations in different orders. Every engine picks an order:

Engine Euler fields Applied order (first to last)
Unity eulerAngles (x = pitch, y = yaw, z = roll) Z, then X, then Y
Unreal FRotator(Pitch, Yaw, Roll) Roll (X), then Pitch (Y), then Yaw (Z)
Godot rotation (x, y, z) default EULER_ORDER_YXZ: Z, then X, then Y
Three.js Euler(x, y, z, order) default order 'XYZ' (as a matrix product), configurable

Roll first, pitch second, yaw last is the common choice for characters and cameras: you tilt your head in your own frame, then look up, then turn your body.

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <cmath>
#include <numbers>

struct Vec3 { double x, y, z; };
struct Mat3 { double m[3][3]; };
Mat3 operator*(const Mat3& A, const Mat3& B) {
    Mat3 R{};
    for (int r = 0; r < 3; r++) for (int c = 0; c < 3; c++) for (int k = 0; k < 3; k++) R.m[r][c] += A.m[r][k] * B.m[k][c];
    return R;
}
Vec3 operator*(const Mat3& M, Vec3 v) {
    return {M.m[0][0] * v.x + M.m[0][1] * v.y + M.m[0][2] * v.z,
            M.m[1][0] * v.x + M.m[1][1] * v.y + M.m[1][2] * v.z,
            M.m[2][0] * v.x + M.m[2][1] * v.y + M.m[2][2] * v.z};
}
Mat3 rotX(double a) { double c = std::cos(a), s = std::sin(a); return {{{1, 0, 0}, {0, c, -s}, {0, s, c}}}; }
Mat3 rotY(double a) { double c = std::cos(a), s = std::sin(a); return {{{c, 0, s}, {0, 1, 0}, {-s, 0, c}}}; }
double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }
std::ostream& operator<<(std::ostream& os, Vec3 v) { return os << "(" << z0(v.x) << ", " << z0(v.y) << ", " << z0(v.z) << ")"; }

int main() {
    std::cout << std::fixed << std::setprecision(3);
    const double d90 = std::numbers::pi / 2;
    Vec3 forward{0, 0, -1};
    // yaw 90 (around Y) and pitch 90 (around X), in both orders
    std::cout << "pitch, then yaw: " << (rotY(d90) * rotX(d90)) * forward << "\n";
    std::cout << "yaw, then pitch: " << (rotX(d90) * rotY(d90)) * forward << "\n";
}
pitch, then yaw: (0.000, 1.000, 0.000)
yaw, then pitch: (-1.000, 0.000, 0.000)

Pitching up first and then turning leaves you looking straight up (turning around the vertical axis doesn't change "up"). Turning first makes you look along -X; pitching around the world X axis then doesn't raise your gaze at all, it only rolls your view sideways. The same two numbers, two different results.

Gimbal lock

Here is the real problem. With the order "roll, then pitch, then yaw", set the pitch to exactly 90° (look straight up). Now the roll axis (your forward) has been tipped onto the yaw axis (world up), so roll and yaw rotate around the same axis. You have lost one degree of freedom: there is no way to express a small rotation in the remaining direction by nudging any of the three angles.

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <cmath>
#include <numbers>

struct Mat3 { double m[3][3]; };
Mat3 operator*(const Mat3& A, const Mat3& B) {
    Mat3 R{};
    for (int r = 0; r < 3; r++) for (int c = 0; c < 3; c++) for (int k = 0; k < 3; k++) R.m[r][c] += A.m[r][k] * B.m[k][c];
    return R;
}
Mat3 rotX(double a) { double c = std::cos(a), s = std::sin(a); return {{{1, 0, 0}, {0, c, -s}, {0, s, c}}}; }
Mat3 rotY(double a) { double c = std::cos(a), s = std::sin(a); return {{{c, 0, s}, {0, 1, 0}, {-s, 0, c}}}; }
Mat3 rotZ(double a) { double c = std::cos(a), s = std::sin(a); return {{{c, -s, 0}, {s, c, 0}, {0, 0, 1}}}; }

// Roll (Z) first, then pitch (X), then yaw (Y): M = Ry * Rx * Rz
Mat3 euler(double yawDeg, double pitchDeg, double rollDeg) {
    const double k = std::numbers::pi / 180;
    return rotY(yawDeg * k) * rotX(pitchDeg * k) * rotZ(rollDeg * k);
}

bool same(const Mat3& A, const Mat3& B) {
    for (int r = 0; r < 3; r++) for (int c = 0; c < 3; c++) if (std::fabs(A.m[r][c] - B.m[r][c]) > 1e-9) return false;
    return true;
}

int main() {
    std::cout << std::boolalpha;
    // At pitch 90, only (yaw - roll) matters:
    std::cout << "yaw 30, roll 0   == yaw 50, roll 20 ? " << same(euler(30, 90, 0), euler(50, 90, 20)) << "\n";
    std::cout << "yaw 30, roll 0   == yaw 0, roll -30 ? " << same(euler(30, 90, 0), euler(0, 90, -30)) << "\n";
    // At pitch 45 the same trick fails, as it should:
    std::cout << "pitch 45: yaw 30 == yaw 50, roll 20 ? " << same(euler(30, 45, 0), euler(50, 45, 20)) << "\n";
}
yaw 30, roll 0   == yaw 50, roll 20 ? true
yaw 30, roll 0   == yaw 0, roll -30 ? true
pitch 45: yaw 30 == yaw 50, roll 20 ? false

Three different angle triples give the identical orientation at 90° pitch. In practice this shows up as:

  • a camera that spins wildly when you look straight up or down, because a tiny change in orientation needs a huge change in yaw and roll;
  • interpolation between two Euler triples that takes a weird, wobbly path, or goes the long way round (from yaw 350° to yaw 10° through 180°);
  • animation curves that suddenly flip 180° in two channels at once.

How games cope

  1. Clamp pitch for first-person cameras to about ±89°, and never roll. With only yaw and pitch, a camera can never lock. Almost every FPS does this.
  2. Store orientations as quaternions (or matrices) internally, and convert to Euler angles only for display and editing. Unity, Unreal and Godot all store quaternions or bases under the hood; the Euler fields in the inspector are derived from them.
  3. Never interpolate Euler angles between arbitrary orientations. Interpolate quaternions with slerp, which the next lessons cover.

Tip: Reading Euler angles back from an engine is not reliable: an orientation has many Euler representations, so setting (0, 180, 0) may read back as (180, 0, 180). Keep your own yaw/pitch floats for a camera controller instead of reading them back from the transform every frame.

Test yourself

What is gimbal lock?

  1. A bug in floating-point math
  2. Losing a degree of freedom when two Euler rotation axes line up
  3. When a quaternion is not normalized
  4. A camera that cannot move

How do most first-person cameras avoid gimbal lock?

  1. They use three Euler angles with roll
  2. They clamp pitch to about ±89° and never roll
  3. They use matrices with negative scale
  4. They store degrees instead of radians

Why do two engines show different orientations for the same Euler triple?

  1. They use different rotation orders (and axis conventions)
  2. One uses radians
  3. Euler angles are random
  4. They normalize differently