skip to content

Physics Engines & Tuning

Knobs shared by middleware such as Box2D, Jolt, PhysX and Havok: sleeping and islands, solver iterations, world scale and determinism. Interviewers ask it to see you can tune a real simulation.

on this pageshow

explore

questions

5

How do sleeping and simulation islands work in physics middleware, and why might toppled furniture keep costing CPU after it stops?

level: middleimportance: must knowfreq 48%

answer

  1. connected groups of touching bodies
  2. static bodies do not join groups
  3. speed below threshold for a while
  4. whole island sleeps or none
  5. what keeps waking it

basics

~20 s

Middleware groups dynamic bodies linked by contacts or joints into islands and sleeps a whole island once every body has stayed below a speed threshold for a set time. One jittering or repeatedly woken body keeps its island simulated.

solid answer

~40 s

An **island** is a connected group of dynamic bodies joined by contacts or joints; static bodies never join one, so the floor does not merge every pile into a single island. Each body tracks how long its linear and angular speeds have stayed below **sleep thresholds**; when every body in an island has been quiet for long enough, the island stops being integrated and solved. Any new contact from an awake body, an applied force or velocity, or a removed support wakes the whole island. Toppled furniture that stays awake usually has one piece that never settles (a bad mass ratio, contact jitter, mis-scaled thresholds), gameplay code that touches a body every frame, or a kinematic object rubbing against the pile. Debug-draw sleep state per body to find the culprit.

code

pseudocode · 18 lines
pseudocode
// runs once per fixed physics step, after the solver
for each contact or joint linking an awake body to a sleeping body:
    wakeIsland(sleepingBody)

// static bodies are never added, so a shared floor does not merge piles
islands = connectedComponents(awakeDynamicBodies, linkedBy = contactsAndJoints)

for island in islands:
    for body in island.bodies:
        if length(body.linearVelocity) > linearSleepSpeed or length(body.angularVelocity) > angularSleepSpeed:
            body.quietTime = 0
        else:
            body.quietTime = body.quietTime + stepLength
    if minimum(body.quietTime for body in island.bodies) >= timeToSleep:
        for body in island.bodies:
            body.asleep = true
            body.linearVelocity = (0, 0, 0)
            body.angularVelocity = (0, 0, 0)

go deeper

for a junior

Recall that resting bodies sleep to save work, and that touching, pushing or removing support wakes them.

for a middle

Explain islands as connected groups of dynamic bodies, why static bodies stay out, and the threshold-plus-time-window sleep test.

for a senior

Diagnose a heap that never sleeps by debug-drawing sleep state and finding the jittering body, the kinematic contact or the per-frame wake from code.

for a principal

Set policy for debris: how long it stays dynamic, when it freezes or despawns, and island-size limits that bound wake-up spikes.

## What sleeping saves A physics step does real work for every dynamic body: integrate its velocity and position, update its bounds in the broad phase, generate contacts and solve them. In most games the majority of dynamic bodies are at rest most of the time: debris after an explosion, furniture after a fight, crates on shelves. **Sleeping** lets the middleware skip that work for bodies that are not moving, while keeping them in the world so that anything that hits them later can wake them. ## Islands: why bodies sleep in groups Bodies cannot sleep one at a time. A crate resting on another crate depends on the contact with it; if the lower crate slept while the upper one kept being solved, the upper one would push on something that no longer responds. So the middleware builds **simulation islands**: - An island is a **connected component** of a graph whose nodes are awake dynamic bodies and whose edges are contacts and joints. - **Static bodies are not nodes.** Two piles resting on the same floor stay separate islands, otherwise one level-sized island would never sleep. - **Kinematic bodies** (moved by code, infinite mass) usually do not merge islands either, but a moving kinematic body in contact keeps the bodies it touches awake. - Many middleware also use islands as independent units of work that can be solved on separate threads; some solvers parallelise differently, but the sleep rule is still per island. The rule is **all or nothing**: an island goes to sleep only when every body in it qualifies, and waking one body wakes the island. ## The sleep test 1. After the solver, each body compares its linear and angular speed with two thresholds. 2. A body above either threshold resets its quiet timer; a body below both adds the step length to it. 3. When the smallest quiet timer in an island reaches a time window, every body in the island is put to sleep and its velocities are zeroed. The **time window** is what stops a pendulum from freezing at the top of its swing, where its speed passes through zero for an instant. Thresholds and window are tuning knobs: too high a threshold and slowly tipping objects freeze at an angle; too low and solver noise keeps bodies awake forever. ## What wakes an island - A new contact with an awake body (a thrown object, the player walking into the pile). - An applied force, impulse or velocity through the middleware's interface. - A joint partner waking. - Losing support: in many middleware, removing or moving a body that others rest on wakes the bodies touching it. Code that teleports a body by writing its transform may or may not wake neighbours, so check the documentation of the middleware in use. ## Why the furniture keeps costing CPU After a fight, twenty chairs, a table and a bookcase lie in one connected heap. If the profiler still shows the solver busy a minute later, the usual causes are: | Cause | Symptom | Fix | |---|---|---| | One body never settles | a chair leg vibrates on the bookcase | fix the mass ratio, slop or unit scale behind the jitter | | Gameplay code wakes it each frame | a wind or buoyancy force applied unconditionally | apply forces only when they change, or skip sleeping bodies | | Kinematic contact | an animated ceiling fan or a character's kinematic hit boxes touch the heap | move it out of contact or filter the collision | | Thresholds wrong for the scale | nothing in the level ever sleeps | rescale thresholds or fix the units | | Sleeping disabled | a body flagged never to sleep joined the heap | allow sleep on debris | One large island is also a cost spike waiting to happen: when anything touches the heap, all of it wakes in the same step. Breaking debris into separate piles, freezing settled cosmetic debris as static, or removing it after a while keeps islands small. ## Diagnosing it Turn on the middleware's debug view and colour bodies by sleep state; the body that stays awake in a sleeping-coloured heap is the culprit. Log island count and the size of the largest island per step. If the largest island keeps its size while nothing visible moves, something is keeping it awake.

  • Why does the sleep test need a time window and not just a speed threshold?
    A body's speed can pass through zero for an instant while it is still moving: a pendulum at the top of its swing, a crate rocking back. Requiring the speed to stay low for a short window means only bodies that have really settled sleep, instead of freezing mid-motion.
  • A crate on a shelf stays asleep in midair after the shelf is deleted; what happened?
    Nothing woke it. A sleeping body is not simulated, so losing its support does not move it unless the removal wakes the bodies that touched the removed one. Many middleware do that on removal, but code that disables or teleports bodies by other means may skip it; wake neighbours explicitly when gameplay removes support.
  • Why are static bodies kept out of islands?
    They never move, so nothing about them needs solving, and they connect everything resting on them. If the floor were an island node, every pile in the level would be one island and nothing could sleep while any single object moved.

  • Each body sleeps on its own regardless of what it touches
  • The floor belongs to the island of everything resting on it
  • A speed threshold alone is enough to decide sleep
  • Sleeping bodies are removed from the world until touched
  • Raising the sleep threshold has no downside
  • Applying a zero force each frame cannot affect performance
open as a page

Why does physics middleware expect a world built in metres and kilograms, and what goes wrong when a game uses another scale?

level: juniorimportance: should knowfreq 42%

basics

~20 s

Middleware tunes gravity, contact tolerances and sleep thresholds as absolute numbers in metres, kilograms and seconds. At another scale those numbers are off by the scale factor: objects fall too slowly, or jitter and never rest.

open as a page

In physics middleware, what do the contact distance and the penetration slop control, and what symptoms show each is set too small or too large?

level: seniorimportance: should knowfreq 33%

basics

~20 s

Contact distance is how far apart shapes may be when a contact is already created; penetration slop is overlap that correction deliberately leaves alone. Too small gives late contacts and jitter; too large gives sinking, snags and extra cost.

open as a page

When a rigid-body simulation misbehaves, how do you choose between more velocity iterations, more position iterations, substeps or a shorter fixed step?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Rule out units and mass ratios, then match knob to symptom: velocity iterations for sliding stacks and soft joints, position iterations for lingering overlap and separating joints, substeps for stiffness, and a shorter step when collision detection itself is too coarse.

open as a page

How would you set a physics determinism policy for replays when a replay recorded on one platform diverges on another?

level: principalimportance: should knowfreq 30%

basics

~20 s

Decide first whether replays must play across platforms. If so, either enable the middleware's cross-platform determinism and enforce identical inputs, order and versions, or record body states instead of inputs; checksums per step locate divergence.

open as a page