btDbvtBroadphase vs btAxisSweep3 in Ammo.js
This article compares the architectural differences, underlying
algorithms, and use cases of btDbvtBroadphase and
btAxisSweep3 within the Ammo.js physics engine.
Understanding these differences is critical for optimizing collision
detection performance in 3D web applications, as the broadphase stage
filters out non-colliding object pairs before expensive narrowphase
physics calculations occur.
Architectural Foundations
The primary difference between btDbvtBroadphase and
btAxisSweep3 lies in their core data structures and how
they partition 3D space to track object boundaries.
btDbvtBroadphase (Dynamic Bounding Volume Tree)
btDbvtBroadphase is built on a Dynamic Bounding Volume
Tree (DBVT). It organizes the Axis-Aligned Bounding Boxes (AABBs) of
physics bodies into a hierarchical, binary tree structure.
- Structure: As objects move, the tree dynamically deforms, rebalancing itself to keep search and insertion times optimized.
- Space Boundaries: This algorithm is completely boundless. You do not need to define the size of your physics world beforehand, making it ideal for infinite or exceptionally large virtual environments.
- Query Mechanism: It utilizes tree-traversal algorithms to detect overlapping bounding boxes, quickly discarding branches of the tree where collisions are mathematically impossible.
btAxisSweep3 (Sweep and Prune)
btAxisSweep3 utilizes a Sweep and Prune (SAP) algorithm.
Instead of a tree hierarchy, it projects the min/max coordinates of each
object’s AABB onto the X, Y, and Z axes, maintaining three sorted lists
of coordinate points.
- Structure: It relies on highly optimized sorting algorithms (typically insertion sort) to keep these coordinate lists updated as objects move.
- Space Boundaries: Unlike DBVT,
btAxisSweep3requires strict, predefined world boundaries (a minimum and maximum 3D vector) and a maximum number of handles/proxies when initialized. If an object moves outside these predefined boundaries, it will trigger an error or fail to detect collisions. - Query Mechanism: Overlaps are detected by checking interval overlaps along the sorted axes. Because insertion sort is extremely fast (\(O(n)\)) when objects only move slightly from frame to frame, this approach is highly efficient for temporal coherence.
Performance and Use Case Comparison
Choosing between these two broadphase implementations depends heavily on the layout and dynamics of your scene.
Dynamic Environments and Open Worlds
btDbvtBroadphase is the industry standard for
general-purpose physics scenes. Because it does not require world
boundaries, it easily accommodates open-world games, infinite runners,
or scenes where objects are spawned at unpredictable coordinates. It
handles rapid, chaotic movements and teleportation across the map with
minimal performance degradation because tree node insertion and deletion
are highly optimized.
Densely Packed, Structured Scenes
btAxisSweep3 can outperform DBVT in scenes with
thousands of objects that move incrementally or remain static, provided
the entire simulation fits within a strict bounding box. However, if
many objects move rapidly across large distances, the sorting lists
suffer from “thrashing,” causing CPU spikes as the algorithm forces
sorted elements to swap positions repeatedly.
Memory Overhead
btDbvtBroadphase generally requires slightly more memory
overhead to maintain the pointers for its binary tree nodes.
btAxisSweep3 is more memory-efficient because it stores
flat, contiguous arrays of sorted axis endpoints, making it highly
cache-friendly for CPU architectures.