A rigid body is a shape with a mass, a moment of inertia, a position and an angle. Make two of them overlap and the engine has to answer three questions: where do they touch, how hard do they push, and which way do they turn. This lesson pairs with 22 - How to write a rigid body simulator by Ten Minute Physics, linked under Watch & read. Going by its chapter titles, that video covers rigid body properties and a position-based way to solve contacts; the video on writing a billiard simulation covers the vector operations and the dot product that every collision response uses. You build a small 2D engine of circles and convex polygons, using velocity impulses instead.
Start with mass. A polygon with counter-clockwise vertices vᵢ gives one number per edge, cᵢ = vᵢ × vᵢ₊₁ = xᵢ·yᵢ₊₁ - yᵢ·xᵢ₊₁. The area is ½Σcᵢ, the centroid is Σ(vᵢ + vᵢ₊₁)·cᵢ / 6A, and the inertia about the origin is ρ/12 · Σ cᵢ (vᵢ·vᵢ + vᵢ·vᵢ₊₁ + vᵢ₊₁·vᵢ₊₁). Move it to the centroid with the parallel axis rule, I = I₀ - m|centroid|². A clockwise polygon has negative area, which is a mistake worth rejecting with a ValueError.
import numpy as np
v = np.array([[0, 0], [2, 0], [0, 2]], float) # a right triangle, density 1
w = np.roll(v, -1, axis=0)
c = v[:, 0] * w[:, 1] - v[:, 1] * w[:, 0]
area = c.sum() / 2
centroid = ((v + w) * c[:, None]).sum(axis=0) / (6 * area)
about_origin = (c * ((v * v).sum(1) + (v * w).sum(1) + (w * w).sum(1))).sum() / 12
print(area, centroid.round(6).tolist(), round(about_origin - area * centroid @ centroid, 6))Contacts come next. Two circles overlap when their centres are closer than the sum of the radii, and the normal is the direction from one centre to the other. For convex polygons you use separating axes: if the shadows of the two shapes on some edge normal do not overlap, the shapes are apart. If every edge normal of both shapes shows an overlap, they collide, and the axis with the smallest overlap gives the normal and the depth. Orient each axis from A toward B before measuring, so that the depth is the distance B must move along the normal to get free.
An impulse changes velocity instantly, which is how a collision looks at the time scale of a frame. With r the vector from a body's centre to the contact point, that point moves with v + ω(-rᵧ, rₓ). The relative speed along the normal is vₙ = (v_B,point - v_A,point) · n. If vₙ is zero or positive the bodies are already separating and nothing happens. Otherwise the normal impulse is j = -(1 + e) vₙ / k, where e is the restitution and k is the effective inverse mass: 1/m_A + 1/m_B + (r_A × n)²/I_A + (r_B × n)²/I_B. The impulse j·n is added to B and subtracted from A, and each body's spin changes by r × J / I. The cross product of two 2D vectors is the single number a₀b₁ - a₁b₀.
A worked example with real numbers. A disc of mass 1 and radius 1 has I = ½, so 1/I = 2. It falls at speed 1 onto a static floor and touches it at r = (1, -1) from its centre, with the normal (0, 1) pointing up from the floor. Here r × n = 1, so k = 1 + 1² · 2 = 3 and j = 1/3 with e = 0.
import numpy as np
inv_m, inv_i = 1.0, 2.0
r, n = np.array([1.0, -1.0]), np.array([0.0, 1.0])
vel, omega = np.array([0.0, -1.0]), 0.0
cross = lambda a, b: a[0] * b[1] - a[1] * b[0]
vn = (vel + omega * np.array([-r[1], r[0]])) @ n
j = -vn / (inv_m + cross(r, n) ** 2 * inv_i)
vel = vel + j * n * inv_m
omega += inv_i * cross(r, j * n)
print(round(j, 4), vel.round(4).tolist(), round(omega, 4))
print((vel + omega * np.array([-r[1], r[0]])).round(4).tolist())The contact point stops approaching, yet the centre still sinks at 2/3 and the disc spins. The point now slides sideways at 2/3, and that is the speed friction would work against.
Two mistakes dominate. The first is the sign of the lever arm: if r × J has its terms swapped, the spin goes the wrong way, and the bodies still bounce, so the picture looks plausible while angular momentum is not conserved. The second is a normal that points from B to A. Then vₙ has the wrong sign, an approaching pair looks like a separating one or the impulse pulls the bodies together, and swapping the arguments of the contact function changes the answer. Smaller traps: bouncing a resting body with full restitution makes it jitter, and the friction clamp uses the normal impulse of this contact, not a constant.
How do you know it works? The tests compare against methods that share no code with yours: a fan of triangles integrates mass, centroid and inertia; an elastic hit must conserve momentum, angular momentum about the origin and kinetic energy; a brute-force grid of sample points confirms that pushing B along the normal by the depth separates the polygons and that no shorter move in any tested direction does. A crate dropped on a floor must settle at the height of its half-size. None of this shows that stacks of many boxes stay stable, that fast bodies cannot tunnel through thin ones, or that the single contact point per pair is enough for every resting position. Circle against polygon pairs are not handled at all.