Skip to content
Lesson 11 of 14

Step 1 of 5 · Reading · ~4 min

Read

Collision Response

Rotational Dynamics

Up to this point your engine has probably treated bodies as points: apply a force, get linear acceleration, integrate velocity and position. But real rigid bodies rotate — a force applied off-center spins them as well as pushing them. This lesson extends the integrator to handle angular motion alongside linear motion, using the rotational analogues of Newton's laws.

The linear/angular analogy

Every linear quantity has a rotational counterpart:

LinearAngularRelationship
mass mmoment of inertia Iresistance to acceleration
force Ftorque τcause of acceleration
velocity vangular velocity ωrate of change of position
position porientation θthe thing being tracked
a = F/mα = τ/INewton's second law, rotational form

Moment of inertia I plays the same role for rotation that mass plays for translation: it's "how hard is this to spin." A point mass far from the rotation axis has high I and is hard to spin up or slow down; a mass concentrated near the axis has low I.

Torque from an off-center force

If a force F is applied at an offset r from the center of mass (rather than exactly at the center of mass), it produces both linear acceleration (as always) and torque. In 2D, torque is the scalar cross product of the offset and the force:

torque = rx * fy - ry * fx

This is the z-component of the 3D cross product r × F, collapsed to a scalar because in 2D all rotation happens around the z-axis. Push straight through the center of mass (r = 0) and torque is zero — no spin, pure translation. Push off to the side and you get spin: the sign of the torque tells you which way (positive = counterclockwise, by the usual convention, given y-up axes).

Where I comes from

Unlike mass, I depends on how the mass is distributed, so each shape has its own formula (all about an axis through the center of mass):

ShapeI
Solid disc / cylinder, radius r0.5 * m * r^2
Hollow ring, radius rm * r^2
Rectangle w by h(1/12) * m * (w^2 + h^2)
Solid sphere (3D), radius r(2/5) * m * r^2

The ring has the largest I for its mass because every gram sits at the maximum distance from the axis; the solid disc has exactly half that, because most of its mass is nearer the center. Same mass, same radius, twice the resistance to spin - that is the whole content of "inertia depends on distribution". The exercise below takes I as an input, so you can substitute whichever shape you are simulating.

Semi-implicit (symplectic) Euler, extended to rotation

Physics engines almost universally use semi-implicit Euler rather than plain (explicit) Euler, because it's far more numerically stable for oscillatory/constrained systems — it updates velocity first, then uses the new velocity to update position, rather than using the old velocity:

ax = fx / m
ay = fy / m
torque = rx * fy - ry * fx
alpha = torque / I

vx += ax * dt
vy += ay * dt
omega += alpha * dt

px += vx * dt      # uses the ALREADY-UPDATED vx
py += vy * dt
theta += omega * dt   # uses the ALREADY-UPDATED omega

Notice the pattern repeats identically for angular quantities — omega/theta update exactly the way vx,vy/px,py do, just with alpha and torque standing in for acceleration and force. Once you've built the linear integrator, extending it to rotation is almost entirely "do the same operations to a parallel set of variables."

Edge cases

  • Infinite mass / infinite inertia bodies (static or kinematic objects, often represented with mass = 0 or I = 0 meaning "immovable" by convention, inverted internally as invMass = 0) should not divide by zero — guard alpha = 0 when I == 0, and similarly for ax, ay when mass == 0.
  • Units and sign convention for theta — this exercise uses radians and the standard math convention (positive = counterclockwise), which matches rx*fy - ry*fx as the 2D cross product formula.
  • This is a single-step, single-body update — real engines run this every physics tick (fixed timestep) and follow it immediately with the collision-response phase (contacts change the velocities this same integrator will pick up next step).
Up nextSleeping & StabilityProduction

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…