skip to content

How do you compute the time of impact of two moving spheres within one physics step?

level: middleimportance: should knowfreq 45%

answer

  1. work in one sphere's frame
  2. relative start, relative displacement
  3. sum the radii, shrink one to a point
  4. a quadratic in the step fraction
  5. earlier root, inside [0, 1]

basics

~20 s

Subtract one sphere's motion from the other's so one stands still, then solve |s + d*t| = rA + rB for the step fraction t. The smaller root of that quadratic, if it lies in [0, 1], is the time of impact.

solid answer

~50 s

I work in sphere B's frame: the relative start is `s = cA - cB` and the relative displacement over the step is `d = dA - dB`. The spheres touch when their centres are `R = rA + rB` apart, so I solve `|s + d*t|^2 = R^2`, which expands to `dot(d,d)*t^2 + 2*dot(s,d)*t + dot(s,s) - R^2 = 0`. If they already overlap at `t = 0`, ordinary contact generation takes over; if `dot(s,d) >= 0` they are not approaching, and a negative discriminant means the paths miss. Otherwise the earlier root `t = (-dot(s,d) - sqrt(dot(s,d)^2 - dot(d,d)*(dot(s,s) - R^2))) / dot(d,d)` is the time of impact if it is at most 1, and the normal is `normalize(s + d*t)`. It is exact for straight-line motion, because a sphere looks the same at any rotation.

code

pseudocode · 20 lines
pseudocode
// cA, cB: centres at the start of the step; dA, dB: displacements over the step.
// Returns the step fraction t in [0, 1] of first contact, or NONE.
function sweptSphereTOI(cA, rA, dA, cB, rB, dB):
    s = cA - cB                    // relative start
    d = dA - dB                    // relative displacement over the step
    R = rA + rB
    c = dot(s, s) - R * R
    if c < 0:
        return 0                   // already overlapping: use ordinary contact generation
    b = dot(s, d)
    if b >= 0:
        return NONE                // not approaching (also covers zero relative motion)
    a = dot(d, d)                  // a > 0 here, because b != 0 means d != 0
    disc = b * b - a * c
    if disc < 0:
        return NONE                // the paths pass without touching
    t = (-b - sqrt(disc)) / a      // earlier root = first touch, never negative here
    if t > 1:
        return NONE                // contact happens after this step
    return t

go deeper

for a junior

Recall that a swept test finds when two shapes first touch along their paths, and that for spheres it reduces to one point moving toward one bigger sphere.

for a middle

Derive the quadratic in the relative frame, pick the earlier root, and walk through the overlap, separating, miss and too-late cases without prompting.

for a senior

Explain why the sweep is exact for spheres but only a bound for rotating boxes, and how impacts are ordered and the remaining step time is handled.

for a principal

Judge where cheap analytic sweeps are enough, such as spherical projectiles, and where the game must pay for iterative time-of-impact methods on rotating shapes.

## Reduce two moving spheres to one moving point A **swept test** asks not whether two shapes overlap at the end of a step, but whether they touch anywhere along their paths during it, and when. The answer is the **time of impact** (TOI), usually expressed as a fraction `t` of the step: 0 is the start, 1 is the end. Two spheres are the friendliest case. Sphere A has centre `cA`, radius `rA` and displacement `dA` over the step; sphere B has `cB`, `rB` and `dB`. Two moves make the problem small: - **Change frame.** Subtract B's motion from both. B stands still at the origin and A's centre moves from `s = cA - cB` along `d = dA - dB`. - **Move the radius.** Two spheres touch exactly when their centres are `R = rA + rB` apart. So grow B to radius `R` and shrink A to its centre point: the question becomes when a point moving along a straight line enters a sphere, which is the familiar ray-versus-sphere test. The moving-point view is a **Minkowski sum**: the set of centre positions at which A touches B is a sphere of radius `R` around B's centre. ## Set up and solve the quadratic 1. The centre separation at fraction `t` is `p(t) = s + d*t`. 2. Contact means `dot(p(t), p(t)) = R^2`. 3. Expanding gives `a*t^2 + 2*b*t + c = 0` with `a = dot(d, d)`, `b = dot(s, d)` and `c = dot(s, s) - R^2`. 4. Using the half-`b` form, the roots are `t = (-b +/- sqrt(b^2 - a*c)) / a`. 5. The **earlier** root is first contact; the later one is where A's centre would leave the grown sphere if it kept going through it. ## The cases a robust test handles - **Already overlapping** (`c < 0`): there is no first contact inside the step; report `t = 0` and let ordinary contact generation, which measures penetration, deal with it. - **Not approaching** (`b >= 0` with `c >= 0`): the distance is not shrinking at the start, and because it changes as a quadratic with `a >= 0` it can only grow afterwards, so there is no impact. This branch also catches zero relative motion. - **Miss** (`b^2 - a*c < 0`): the line of motion passes the grown sphere without touching it. - **Too late** (`t > 1`): contact happens after this step, so report nothing now; next step's test will find it. - **Hit** (`0 <= t <= 1`): the spheres first touch at that fraction of the step. When `c >= 0` and `b < 0`, the earlier root is never negative, because `sqrt(b^2 - a*c)` is at most `|b|`. The half-`b` form simply drops a factor of two from every term and keeps the formula short. ## Swept boxes work the same way, with limits Two axis-aligned boxes reduce the same way: grow box B by A's half-extents and treat A's centre as a point moving along `d`. Then each axis gives an entry and an exit time, the **latest entry** across the axes is first contact, the **earliest exit** is separation, and the pair hits when the latest entry is no later than the earliest exit and falls inside `[0, 1]`. The axis that produced the latest entry gives the contact normal. An axis along which `d` has no component needs a special case: the boxes either already overlap on that axis for the whole step or never do. | Shape pair | Exact for | Breaks down when | |---|---|---| | Sphere vs sphere | any straight-line motion | the path between samples is curved | | Axis-aligned box vs box | translation only | either body rotates, so its bounding box changes shape | | Sphere vs static plane | straight-line motion | the path between samples is curved | A sphere is the same at every orientation, so its sweep ignores rotation correctly. A tumbling crate's box changes during the step, so a swept box only bounds its motion: useful as a cheap early-out, not as its exact time of impact. Rotating convex shapes need an iterative method instead. ## What to do with the time of impact - **Advance to impact**: move both bodies to fraction `t`, generate a contact there, and resolve it. - **Spend the rest of the step**: the remaining `1 - t` of the step can be simulated after the response, or dropped. Dropping it is simpler but makes fast bodies lose motion at every impact. - **Order matters**: a body that hits several things in one step should handle the earliest impact first, because responding to it changes its path to the others. The contact normal at impact is `normalize(s + d*t)`, pointing from B's centre toward A's, and the contact point is B's centre at impact, `cB + dB*t`, plus `rB` along that normal.

  • How do you get the contact normal and contact point at the time of impact?
    Evaluate the relative separation at impact, `s + d*t`; its length is `rA + rB`, and normalising it gives the normal pointing from B's centre toward A's. The contact point is B's centre at impact, `cB + dB*t`, moved `rB` along that normal, which is also A's centre at impact moved `rA` against it.
  • Why is the swept-sphere result exact while a swept box is not for a tumbling crate?
    A sphere's shape does not depend on its orientation, so sweeping its centre along a straight line covers every point it occupies during the step. A crate's axis-aligned bounding box changes as it rotates, so sweeping one fixed box only bounds where the crate might be. Rotating convex shapes need an iterative time-of-impact method such as conservative advancement.
  • What happens to the rest of the step after a projectile's impact at fraction t?
    Either the simulation resolves the impact and then integrates the remaining `1 - t` of the step, possibly finding further impacts, or it drops the remainder. Dropping it is simpler and common, but a fast body loses part of its motion at every impact, which can read as sticking or sudden slowing.

  • Test each sphere's path separately against the other sphere's starting position.
  • Use the larger root of the quadratic as the time of impact.
  • A negative discriminant means the spheres are already overlapping.
  • Any real root is an impact, even one greater than 1.
  • A swept axis-aligned box gives an exact time of impact for a tumbling crate.