Skip to content

Physics: add sleeping/deactivation for resting bodies #557

Description

@stormmuller

Problem

Bodies in src/physics are never put to sleep — every dynamic body is integrated and solved every tick regardless of whether it has come to rest. Production 2D physics engines (Box2D, Matter.js, Rapier2D) deactivate bodies whose linear/angular velocity stays below a threshold for a short time, skipping them in integration and the solver until something wakes them (a new contact, an applied force/impulse, a joint update).

Impact

Wasted CPU on scenes with lots of settled bodies (e.g. a pile of boxes at rest, a terrain full of debris) — the engine keeps re-solving contacts and re-integrating bodies that aren't moving.

Suggested fix

  • Track per-body linear/angular velocity against a "sleep threshold" over a short time window.
  • Mark bodies that stay under threshold as asleep; skip them in the integration and broad/narrow-phase/solver systems.
  • Wake a sleeping body when: a new/updated contact touches it, an impulse/force/torque is applied to it, or a connected joint's other body wakes.
  • Expose sleep threshold/time and a way to opt a body out of sleeping (e.g. for player-controlled bodies) via the rigid body component options.

Acceptance criteria

  • Resting bodies stop being integrated/solved after a configurable time-at-rest
  • Sleeping bodies wake correctly on new contact, applied impulse/force/torque, or joint interaction
  • Existing physics unit tests continue to pass
  • Documented in documentation-site/docs/docs/physics

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions