How do sleeping and simulation islands work in physics middleware, and why might toppled furniture keep costing CPU after it stops?
answer
- connected groups of touching bodies
- static bodies do not join groups
- speed below threshold for a while
- whole island sleeps or none
- what keeps waking it
basics
~20 sMiddleware 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 sAn **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// 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
Recall that resting bodies sleep to save work, and that touching, pushing or removing support wakes them.
Explain islands as connected groups of dynamic bodies, why static bodies stay out, and the threshold-plus-time-window sleep test.
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.
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