Rotation, Torque and Inertia
So far bodies were points: a push moved them, nothing made them spin. Real crates tumble when hit on a corner and barrels roll down slopes. Rotation has its own copy of every linear quantity, and once you see the pairs side by side it is less mysterious than it looks.
| Linear | Angular (2D) | Angular (3D) |
|---|---|---|
position x |
angle θ |
orientation quaternion q |
velocity v |
angular velocity ω (rad/s, a scalar) |
angular velocity ω (a vector: axis × speed) |
mass m |
moment of inertia I (scalar) |
inertia tensor I (3×3 matrix) |
force F |
torque τ = r × F (scalar) |
torque τ = r × F (vector) |
a = F / m |
α = τ / I |
α = I⁻¹ τ |
impulse Δv = J / m |
Δω = (r × J) / I |
Δω = I⁻¹ (r × J) |
Here r is the vector from the center of mass to the point where the force acts, and × is the cross product. In 2D the cross product of two vectors is the scalar r.x * F.y - r.y * F.x: positive means counter-clockwise.
Moment of inertia: how hard it is to spin
Mass resists linear acceleration; the moment of inertia resists angular acceleration. It depends on how far the mass is from the axis, so a hoop is harder to spin than a solid disc of the same mass. Engines compute it from the collider shape and density:
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::fixed << std::setprecision(3);
const double m = 2.0; // every shape has 2 kg
struct Shape { const char* name; double I; };
Shape shapes[] = {
{"solid disc, r = 0.5", 0.5 * m * 0.5 * 0.5},
{"thin hoop, r = 0.5", m * 0.5 * 0.5},
{"box 1 x 1", m * (1 * 1 + 1 * 1) / 12},
{"box 2 x 0.25 (plank)", m * (2 * 2 + 0.25 * 0.25) / 12},
{"rod, length 2 (about its center)", m * 2 * 2 / 12},
};
const double torque = 1.0, seconds = 1.0; // 1 N*m for 1 s
for (auto& s : shapes) {
double omega = torque / s.I * seconds;
std::cout << std::left << std::setw(34) << s.name << std::right << " I = " << s.I
<< " spin after 1 s: " << omega << " rad/s\n";
}
}solid disc, r = 0.5 I = 0.250 spin after 1 s: 4.000 rad/s
thin hoop, r = 0.5 I = 0.500 spin after 1 s: 2.000 rad/s
box 1 x 1 I = 0.333 spin after 1 s: 3.000 rad/s
box 2 x 0.25 (plank) I = 0.677 spin after 1 s: 1.477 rad/s
rod, length 2 (about its center) I = 0.667 spin after 1 s: 1.500 rad/s
The plank spins up about half as fast as the square box, and the hoop half as fast as the disc, although every shape weighs 2 kg. When an object in your game spins too easily or too stubbornly, the inertia (or a missing collider that the engine would compute it from) is usually the reason. Unity exposes Rigidbody.inertiaTensor and lets you override the automatic value; most engines also let you move the center of mass (Rigidbody.centerOfMass in Unity), which is how vehicles are made harder to flip.
Hitting a body off-center
An impulse through the center of mass only moves a body. The same impulse at a corner moves it and spins it. The velocity of any point on the body is then v + ω × r, which is what collision response needs at the contact point:
#include <iostream>
#include <iomanip>
#include <cmath>
struct Vec2 { double x, y; };
double cross(Vec2 a, Vec2 b) { return a.x * b.y - a.y * b.x; }
Vec2 crossScalar(double w, Vec2 r) { return {-w * r.y, w * r.x}; } // ω × r in 2D
double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }
int main() {
std::cout << std::fixed << std::setprecision(3);
const double m = 2, w = 1, h = 1;
const double invMass = 1 / m, invI = 1 / (m * (w * w + h * h) / 12);
Vec2 J{3, 0}; // a 3 N*s push to the right
Vec2 hitPoints[] = {{0, 0}, {-0.5, -0.5}, {-0.5, 0.5}};
const char* names[] = {"center", "bottom-left corner", "top-left corner"};
for (int i = 0; i < 3; i++) {
Vec2 r = hitPoints[i];
Vec2 v{J.x * invMass, J.y * invMass};
double omega = cross(r, J) * invI;
Vec2 topRight{0.5, 0.5};
Vec2 pv = crossScalar(omega, topRight);
std::cout << std::left << std::setw(19) << names[i] << std::right << ": v = (" << z0(v.x) << ", " << z0(v.y)
<< ") omega = " << std::setw(6) << z0(omega) << " rad/s top-right corner moves ("
<< z0(v.x + pv.x) << ", " << z0(v.y + pv.y) << ")\n";
}
}center : v = (1.500, 0.000) omega = 0.000 rad/s top-right corner moves (1.500, 0.000)
bottom-left corner : v = (1.500, 0.000) omega = 4.500 rad/s top-right corner moves (-0.750, 2.250)
top-left corner : v = (1.500, 0.000) omega = -4.500 rad/s top-right corner moves (3.750, -2.250)
Hit at the bottom-left, the box spins counter-clockwise (positive ω); hit at the top-left, clockwise. The linear velocity is the same in all three cases: where you push only changes the spin. This is how a bullet impulse applied at the hit point (AddForceAtPosition in Unity, AddImpulseAtLocation in Unreal, apply_impulse(impulse, position) in Godot) makes a crate tumble the right way.
Integrating orientation in 3D
In 2D, θ += ω * dt is all there is. In 3D the orientation is a unit quaternion, and its rate of change is dq/dt = ½ · (0, ω) · q (with ω in world space). Engines apply that with semi-implicit Euler and then renormalize, because each step slightly changes the quaternion's length:
#include <iostream>
#include <iomanip>
#include <cmath>
struct Quat { double w, x, y, z; };
Quat mul(Quat a, Quat b) {
return {a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z,
a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x,
a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w};
}
double length(Quat q) { return std::sqrt(q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z); }
Quat normalize(Quat q) { double l = length(q); return {q.w / l, q.x / l, q.y / l, q.z / l}; }
Quat integrate(Quat q, double wx, double wy, double wz, double dt) {
Quat spin = mul(Quat{0, wx, wy, wz}, q);
return {q.w + 0.5 * dt * spin.w, q.x + 0.5 * dt * spin.x, q.y + 0.5 * dt * spin.y, q.z + 0.5 * dt * spin.z};
}
int main() {
std::cout << std::fixed << std::setprecision(4);
const double PI = 3.14159265358979, dt = 1.0 / 60;
const double wy = PI / 2; // 90 degrees per second around +y
Quat raw{1, 0, 0, 0}, fixedQ{1, 0, 0, 0};
for (int step = 1; step <= 120; step++) {
raw = integrate(raw, 0, wy, 0, dt);
fixedQ = normalize(integrate(fixedQ, 0, wy, 0, dt));
if (step % 60 == 0) {
double angle = 2 * std::atan2(fixedQ.y, fixedQ.w) * 180 / PI;
std::cout << "t=" << step * dt << " s: angle " << angle << " deg, |q| raw " << length(raw)
<< ", normalized " << length(fixedQ) << "\n";
}
}
}t=1.0000 s: angle 89.9949 deg, |q| raw 1.0052, normalized 1.0000
t=2.0000 s: angle 179.9897 deg, |q| raw 1.0103, normalized 1.0000
The angle is right to within a few thousandths of a degree (the small shortfall is the first-order integration error), but without normalization the quaternion's length creeps upward, and a non-unit quaternion scales the object as well as rotating it. One normalize per step fixes it.
The inertia tensor
In 3D the moment of inertia becomes a 3×3 inertia tensor, because a body can be easy to spin around one axis and hard around another (a pencil spins easily around its length). Engines store it in the body's local frame as three numbers along the principal axes (Unity calls them inertiaTensor and inertiaTensorRotation), and each step rebuild the world-space inverse as R · I⁻¹ · Rᵀ, where R is the body's rotation matrix. You rarely touch it directly; just know that a stretched or scaled collider changes it, which is one more reason the field notes say never to scale colliders non-uniformly.
Test yourself
A 2 kg hoop and a 2 kg solid disc have the same radius. Which one spins up faster under the same torque?
- The hoop
- The solid disc
- They spin up equally fast
- Neither spins without a force at the center
The same impulse hits a box at its center and then at a corner. What differs between the two results?
- The linear velocity
- Only the angular velocity
- Both linear and angular velocity
- Nothing
Why do engines renormalize the orientation quaternion after integrating it?
- To convert it to Euler angles
- Each integration step slightly changes its length, and a non-unit quaternion distorts the object
- To remove gimbal lock
- Because ω must be a unit vector