Skip to content

Physics: broad-phase collision is O(n²) #556

Description

@stormmuller

Problem

createBroadPhaseEcsSystem (src/physics/systems/broad-phase-ecs-system.ts or equivalent) performs a brute-force all-pairs AABB overlap test — a nested loop over every entity against every other entity. There is no spatial partitioning (grid, BVH, sweep-and-prune, etc).

Impact

Broad-phase cost grows quadratically with body count, so simulations will start dropping frames well before reaching even a few hundred dynamic bodies. This caps the scale of anything built with the physics module (crowds, particle-like rigid bodies, destructible terrain debris, etc).

Suggested fix

Introduce a spatial partitioning structure for the broad phase — a uniform/spatial hash grid is usually the simplest fit for a 2D engine like this, but a sweep-and-prune or BVH would also work. Should reduce average-case broad-phase cost to roughly O(n) for typically-distributed scenes while keeping the existing narrow-phase (SAT) and solver untouched.

Acceptance criteria

  • Broad phase no longer performs a full O(n²) all-pairs scan for the common case
  • Existing physics unit tests continue to pass
  • A benchmark/stress-test scene (e.g. the stress-test demo) shows improved frame time at high body counts

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