Conversation
- The broad phase now emits new collision pairs and stores all pairs in a HashSet - Contact status and kind is now tracked with `ContactPairFlags` instead of booleans - The narrow phase adds new collision pairs, updates existing pairs, and responds to state changes separately instead of overwriting and doing extra work for persistent contact - State changes are tracked with bit vectors (bit sets), which are fast to iterate serially - The narrow phase is responsible for collision events instead of the `ContactReportingPlugin`
- Renamed `BroadCollisionPairs` to `BroadPhasePairSet` - Added `BroadPhasePairSet` for fast pair lookup with new `PairKey` - Improve broad phase docs
…pt-in - Removed `BroadPhaseAddedPairs` - Renamed `BroadPhasePairSet` to `BroadPhasePairs` - Moved contact creation to broad phase to improve persistence - Removed some graph querying overhead from contact pair removal by using the `EdgeIndex` directly - Made collision events opt-in with `CollisionEventsEnabled` component - Improved a lot of docs
|
I think there is a new issue on this branch (doesn't exist on (Here it's overflowed a signed to unsigned u32 The index is sometimes positive, sometimes equal to the length.) This is the kind of code I use: I can work around this by also removing the |
It's intentional that
Hmm 🤔 I have a test scene that is changing |
I see, that makes sense.
I need to update the purported cause -- it apparently has nothing directly to do with swapping I will work on a smaller reproducible case. |
|
OK, I found an easy reproducible, though likely it means painful debugging. The actual cause is, adding and removing colliders during a simulation. I just modified the |
|
Thanks, I'll try that! I did also get a repro for panics when changing This is slightly different from the panic you got though. Either way, I'll look into these and see if I can fix them... Edit: ah this actually looks like the same panic as your new repro |
|
@eswartz I fixed some issues (this involved DGriffin91/obvhs#9) and it seems to work for me now. Could you try again when you have time, and check if there are any problems remaining? Remember to do a |
Yup, I saw the commits and the panics are gone for me too. Thanks! |
|
Merging! If someone notices unexpected regressions, please open an issue :) |
# Objective #927 broke wasm! It useds async tasks, and apparently you cannot use `block_on` there. ## Solution Perform tree optimization synchronously on wasm. I also added a `use_async_tasks` setting in case you want to disable it even in native contexts. ## Testing `bevy run --example bvh web`
# 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)
Objective
Implement a new broad phase algorithm using Bounding Volume Hierarchies (BVH) provided by OBVHS. This replaces our existing broad phase that uses Sweep and Prune (SAP).
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
ColliderTreetype that contains aBvh2, its collision proxies, and a workspace for reusing allocationsColliderTreesresource, containing a separateColliderTreefor dynamic, kinematic, static, and standalone (=no body) collidersColliderTreePluginthat managesColliderTreesfor a collider typeCColliderAabbs andEnlargedAabbs of collidersEnlargedAabb, or otherwise require updating the treeColliderTreeOptimizationsettings (default is an adaptive hybrid that switches between them)BroadPhaseCorePluginthat sets up the resources, system sets, and diagnostics required for the broad phaseBvhBroadPhasePluginthat implements a BVH-based broad phase algorithmTesting
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
bvhexample, for stress testing the collider trees with various different configurations.bvh.mp4
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:
After, with the BVH broad phase:
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.
Future Work