TExcelTAD
Home › Math for Games › Axis-Angle Rotation

Axis-Angle Rotation

Every 3D rotation, no matter how it was built, is a rotation by some angle around some axis. That is Euler's rotation theorem, and it gives the most intuitive representation of a rotation: a unit vector â and an angle θ. A door swings around its hinge axis; a planet spins around its tilted pole; a wheel turns around its axle. Axis-angle is also the bridge to quaternions.

Rodrigues' rotation formula

To rotate a vector v around a unit axis â by θ, split v into the part along the axis (which doesn't move) and the part perpendicular to it (which turns in a circle). Putting the pieces together gives Rodrigues' formula:

text
v' = v * cos θ  +  (â × v) * sin θ  +  â * (â · v) * (1 - cos θ)
cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <cmath>
#include <numbers>

struct Vec3 { double x, y, z; };
Vec3 operator+(Vec3 a, Vec3 b) { return {a.x + b.x, a.y + b.y, a.z + b.z}; }
Vec3 operator*(Vec3 v, double s) { return {v.x * s, v.y * s, v.z * s}; }
double dot(Vec3 a, Vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
Vec3 cross(Vec3 a, Vec3 b) { return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; }
Vec3 normalize(Vec3 v) { return v * (1 / std::sqrt(dot(v, v))); }
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) << ")"; }

Vec3 rotateAxisAngle(Vec3 v, Vec3 axis, double angle) {     // axis must be unit length
    double c = std::cos(angle), s = std::sin(angle);
    return v * c + cross(axis, v) * s + axis * (dot(axis, v) * (1 - c));
}

int main() {
    std::cout << std::fixed << std::setprecision(3);
    const double k = std::numbers::pi / 180;
    Vec3 up{0, 1, 0};
    std::cout << "X around Y by 90:   " << rotateAxisAngle({1, 0, 0}, up, 90 * k) << "\n";

    // Around the diagonal axis (1,1,1), 120 degrees cycles the axes: X -> Y -> Z.
    Vec3 diag = normalize({1, 1, 1});
    std::cout << "X around diag 120:  " << rotateAxisAngle({1, 0, 0}, diag, 120 * k) << "\n";
    std::cout << "Y around diag 120:  " << rotateAxisAngle({0, 1, 0}, diag, 120 * k) << "\n";

    // A vector on the axis does not move at all.
    std::cout << "diag around diag:   " << rotateAxisAngle(diag, diag, 77 * k) << "\n";
}
X around Y by 90:   (0.000, 0.000, -1.000)
X around diag 120:  (0.000, 1.000, 0.000)
Y around diag 120:  (0.000, 0.000, 1.000)
diag around diag:   (0.577, 0.577, 0.577)

The 120° rotation about the diagonal is a nice sanity check: it sends X to Y and Y to Z, something no single Euler angle can do.

Axis-angle from two directions

"Rotate this arrow so it points at that one" is a common request (aligning a character's up with the ground normal, turning a cannon toward a target). The axis is the cross product of the two directions, and the angle comes from the dot product:

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

struct Vec3 { double x, y, z; };
Vec3 operator+(Vec3 a, Vec3 b) { return {a.x + b.x, a.y + b.y, a.z + b.z}; }
Vec3 operator*(Vec3 v, double s) { return {v.x * s, v.y * s, v.z * s}; }
double dot(Vec3 a, Vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
Vec3 cross(Vec3 a, Vec3 b) { return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; }
double length(Vec3 v) { return std::sqrt(dot(v, v)); }
Vec3 normalize(Vec3 v) { return v * (1 / length(v)); }
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) << ")"; }

Vec3 rotateAxisAngle(Vec3 v, Vec3 a, double t) {
    double c = std::cos(t), s = std::sin(t);
    return v * c + cross(a, v) * s + a * (dot(a, v) * (1 - c));
}

int main() {
    std::cout << std::fixed << std::setprecision(3);
    Vec3 from = normalize({0, 1, 0});            // character up
    Vec3 to = normalize({1, 2, 0});              // ground normal on a slope
    Vec3 axis = cross(from, to);
    double angle = std::acos(std::clamp(dot(from, to), -1.0, 1.0));
    axis = normalize(axis);                       // fails if from and to are parallel!
    std::cout << "axis " << axis << ", angle " << angle * 180 / std::numbers::pi << " deg\n";
    std::cout << "rotated 'from': " << rotateAxisAngle(from, axis, angle) << "\n";
    std::cout << "target 'to':    " << to << "\n";
}
axis (0.000, 0.000, -1.000), angle 26.565 deg
rotated 'from': (0.447, 0.894, 0.000)
target 'to':    (0.447, 0.894, 0.000)

When from and to are parallel the cross product is zero and has no direction. If they point the same way, no rotation is needed; if they point in opposite directions, any perpendicular axis works with 180°. Library functions like Unity's Quaternion.FromToRotation handle both cases for you.

Angular velocity

Axis-angle is also how physics describes spinning. An angular velocity is a vector whose direction is the spin axis and whose length is the speed in radians per second. Over a frame, the object rotates around â = ω / |ω| by |ω| * dt. Rigid-body engines (Rigidbody.angularVelocity in Unity, GetPhysicsAngularVelocityInRadians in Unreal) use this form.

Limits of axis-angle

Axis-angle is easy to read and easy to build, but combining two axis-angle rotations has no simple formula, and interpolating them is awkward. Quaternions store the same information (axis and angle) in a form that combines and interpolates cleanly. That is the next lesson.

Note: (â, θ) and (-â, -θ) are the same rotation, and so are θ and θ + 360°. Keep that in mind when you compare rotations numerically.

Test yourself

What happens to a vector that lies along the rotation axis?

  1. It flips
  2. It doesn't change
  3. It becomes zero
  4. It rotates by twice the angle

To rotate direction a onto direction b, which axis do you use?

  1. a + b
  2. normalize(cross(a, b))
  3. a - b
  4. The world up

What does the length of an angular velocity vector mean?

  1. The radius of the object
  2. The spin speed in radians per second
  3. The angle in degrees
  4. Nothing; it is always 1