Skip to content

Use ColliderTrees for spatial queries#941

Merged
Jondolf merged 8 commits intomainfrom
obvhs-spatial-queries
Feb 7, 2026
Merged

Use ColliderTrees for spatial queries#941
Jondolf merged 8 commits intomainfrom
obvhs-spatial-queries

Conversation

@Jondolf
Copy link
Member

@Jondolf Jondolf commented 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:

Before

(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:

After

Future Work

@Jondolf Jondolf added this to the 0.6 milestone Feb 7, 2026
@Jondolf Jondolf added C-Performance Improvements or questions related to performance A-Spatial-Query Relates to spatial queries, such as ray casting, shape casting, and intersection tests M-Migration-Guide A breaking change to Avian's public API that needs to be noted in a migration guide D-Modest A moderate level of difficulty: suitable for simple features or challenging fixes C-Refinement Improves quality or results in some way, without fixing a clear bug or adding new functionality S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Feb 7, 2026
@Jondolf Jondolf enabled auto-merge (squash) February 7, 2026 12:15
@Jondolf Jondolf merged commit 309be19 into main Feb 7, 2026
6 checks passed
@Jondolf Jondolf deleted the obvhs-spatial-queries branch February 7, 2026 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Spatial-Query Relates to spatial queries, such as ray casting, shape casting, and intersection tests C-Performance Improvements or questions related to performance C-Refinement Improves quality or results in some way, without fixing a clear bug or adding new functionality D-Modest A moderate level of difficulty: suitable for simple features or challenging fixes M-Migration-Guide A breaking change to Avian's public API that needs to be noted in a migration guide S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize shapecasting with multiple hits

1 participant