Draft: Make spatial queries generic over colliders#810
Draft
NiseVoid wants to merge 2 commits intoavianphysics:mainfrom
Draft
Draft: Make spatial queries generic over colliders#810NiseVoid wants to merge 2 commits intoavianphysics:mainfrom
NiseVoid wants to merge 2 commits intoavianphysics:mainfrom
Conversation
abeb2aa to
e4cb487
Compare
e4cb487 to
1f375a7
Compare
1f375a7 to
5baf1b3
Compare
5baf1b3 to
8356db7
Compare
Member
|
Marking as Waiting-on-Author due to merge conflicts |
Jondolf
added a commit
that referenced
this pull request
Feb 2, 2026
# Objective Implement a new broad phase algorithm using [Bounding Volume Hierarchies (BVH)](https://en.wikipedia.org/wiki/Bounding_volume_hierarchy) provided by [OBVHS](https://github.com/DGriffin91/obvhs). This replaces our existing broad phase that uses [Sweep and Prune (SAP)](https://en.wikipedia.org/wiki/Sweep_and_prune). The goal is to greatly reduce overhead for large scenes with lots of colliders. Updating and querying the acceleration structures should be efficient, and static or sleeping bodies should have minimal runtime cost. The existing SAP broad phase is very inefficient at this, and scales poorly even with only static geometry. Additionally, we can reuse these BVHs for accelerating spatial queries. Currently, they just use Parry's BVH, which we update and manage very inefficiently. I will leave migrating spatial queries to the new BVHs for a follow-up however, to keep the diff more manageable. Huge thanks to @DGriffin91 for implementing incremental leaf insertion and removal, partial rebuilds, and more for OBVHS to better suit our needs <3 ## Solution - `ColliderTree` type that contains a `Bvh2`, its collision proxies, and a workspace for reusing allocations - `ColliderTrees` resource, containing a separate `ColliderTree` for dynamic, kinematic, static, and standalone (=no body) colliders - `ColliderTreePlugin` that manages `ColliderTrees` for a collider type `C` - Adds/removes colliders to/from trees, and updates proxy data - Updates `ColliderAabb`s and `EnlargedAabb`s of colliders - Manages "moved proxies": colliders that moved past their `EnlargedAabb`, or otherwise require updating the tree - Optimizes trees to maintain good query performance, in an async task that runs concurrently with the narrow phase and solver - Full rebuilds, partial rebuilds, or reinsertion based on `ColliderTreeOptimization` settings (default is an adaptive hybrid that switches between them) - `BroadPhaseCorePlugin` that sets up the resources, system sets, and diagnostics required for the broad phase - `BvhBroadPhasePlugin` that implements a BVH-based broad phase algorithm - Traverses each tree for every moved proxy (colliders that don't move beyond their enlarged AABB don't need to check for new overlaps) - Creates edges in the contact graph for every new overlap between enlarged AABBs ## Testing Ran examples and tested adding/removing/enabling/disabling colliders and changing various different configurations. Users also did some testing in real-world applications. ## Note for Reviewers The commit history here is a bit borked for some reason, and contains some older commits that are unrelated. The first real commit is "Implement dynamic BVH broad phase with OBVHS (WIP)" (a603c5e). --- ## Showcase Here is the new `bvh` example, for stress testing the collider trees with various different configurations. https://github.com/user-attachments/assets/8d0e6088-c101-4e37-a597-dce356ca9e0c ### Dynamic Scene Below is a test scene with 10k colliders with approximately 25% of them being moved each frame. Spatial queries are disabled. Before, with the SAP broad phase: <img width="1278" height="717" alt="Before" src="https://github.com/user-attachments/assets/3f7f78be-2148-4d0d-8eeb-69045b56a88a" /> After, with the BVH broad phase: <img width="1278" height="717" alt="After" src="https://github.com/user-attachments/assets/8bddc2f6-253a-4f1d-87c2-f1eff406ce82" /> Note that while the tree optimization has a fairly large cost here, in real-world applications the cost should often be more hidden, as it is done in parallel with the narrow phase and solver. ### Static Scene Below are the results before and after, for 40k static colliders with no movement. Spatial queries are disabled. Debug rendering is also disabled, as gizmos start to tank performance at this scale. <img width="590" height="500" alt="BVH Performance" src="https://github.com/user-attachments/assets/b5da19b1-0d81-4674-89ad-d59a78f0ddd0" /> ## Future Work - Make spatial queries use the new BVH (adopt #810) - Explore making collider trees more flexible, ex: allow users to easily create their own trees and perform queries on them - Try doing a small amount of reinsertion on the static tree at each step to improve its query performance - Optimize partial rebuilds further
Jondolf
added a commit
that referenced
this pull request
Feb 7, 2026
# Objective Fixes #403. #927 added `ColliderTrees` for the new BVH broad phase. We should reuse them for spatial queries instead of maintaining and using a separate BVH from Parry. ## Solution In short: - Add more traversal methods on `Bvh2` via extension traits (we should probably upstream these) - `sweep_traverse`, `sweep_traverse_miss`, `sweep_traverse_anyhit`, and `sweep_traverse_dynamic` - `squared_distance_traverse` and `squared_distance_traverse_dynamic` - Add methods for BVH traversal on `ColliderTree` - `ray_traverse_closest` and `ray_traverse_all` - `sweep_traverse_closest` and `sweep_traverse_all` - `squared_distance_traverse_closest` - `point_traverse` - `aabb_traverse` - Remove `SpatialQueryPipeline`, and use the `ColliderTrees` traversal methods for `SpatialQuery` This involved some other miscellaneous changes: - Shape casts now returns hits in arbitrary order when `max_hits > 1`, similar to ray casts. - `point2` and `normal2` were previously in local space, despite what the docs state. They are now in world space. ## Testing Tested different spatial queries in examples. --- ## Showcase Before, updating the spatial query pipeline was extremely expensive for large scenes with a lot of colliders: <img width="263" height="439" alt="Before" src="https://github.com/user-attachments/assets/0cc11950-cd69-434a-90f6-1fa517d255f9" /> (note that the tree optimization cost is partially hidden here, as it is run in parallel with the spatial query pipeline update) Now, using the much more optimized `ColliderTrees`, that overhead is gone: <img width="263" height="439" alt="After" src="https://github.com/user-attachments/assets/e39091f1-3a6f-4cfd-b22d-c8ac9c646b5f" /> ## Future Work - Generic collider types for spatial queries (#810)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Objective
It should be possible to use the spatial query API for non-standard colliders
Solution
Changelog
Migration Guide
TODO