scene_queries_intersection_test
Intersection tests will find all the colliders with a shape intersecting a given shape. This can be useful for, e.g., selecting all the objects that intersect a given area. There are two kind of intersection tests:
- The exact intersection test
RapierContext::intersect_shapesearches for all the colliders with shapes intersecting the given shape. - The approximate intersection test
RapierContext::intersect_aabb_conservativesearches for all the colliders with an AABB intersecting the given AABB. This does not check if the actual shapes of these colliders intersect the AABB. Note that the AABB taken into account is the one currently stored in the BVH of the broad-phase (updated by each simulation step): it isn't recomputed from the latest collider positions.
See the ray-casting section for details about intersection tests between a ray and the colliders on the scene. And see the point projection section for details about the intersection test between the colliders and a point.
- Example 2D
- Example 3D
/* Test intersections inside of a system. */
fn test_intersections(rapier_context: ReadRapierContext) {
let rapier_context = rapier_context.single().unwrap();
let shape = Collider::cuboid(1.0, 2.0);
let shape_pos = Vec2::new(0.0, 1.0);
let shape_rot = 0.8;
let filter = QueryFilter::default();
rapier_context.intersect_shape(shape_pos, shape_rot, &shape, filter, |entity, _collider| {
println!("The entity {:?} intersects our shape.", entity);
true // Return `false` instead if we want to stop searching for other colliders intersecting our shape.
});
let aabb = Aabb2d::new(Vec2::new(-1.0, -2.0), Vec2::new(1.0, 2.0));
rapier_context.intersect_aabb_conservative(aabb, filter, |entity, _collider| {
println!(
"The entity {:?} has an AABB intersecting our test AABB",
entity
);
true // Return `false` instead if we want to stop searching for other colliders with an intersecting AABB.
});
}
/* Test intersections inside of a system. */
fn test_intersections(rapier_context: ReadRapierContext) {
let rapier_context = rapier_context.single().unwrap();
let shape = Collider::cuboid(1.0, 2.0, 3.0);
let shape_pos = Vec3::new(0.0, 1.0, 2.0);
let shape_rot = Quat::from_rotation_z(0.8);
let filter = QueryFilter::default();
rapier_context.intersect_shape(shape_pos, shape_rot, &shape, filter, |entity, _collider| {
println!("The entity {:?} intersects our shape.", entity);
true // Return `false` instead if we want to stop searching for other colliders intersecting our shape.
});
let aabb = Aabb3d::new(Vec3::new(-1.0, -2.0, -3.0), Vec3::new(1.0, 2.0, 3.0));
rapier_context.intersect_aabb_conservative(aabb, filter, |entity, _collider| {
println!(
"The entity {:?} has an AABB intersecting our test AABB",
entity
);
true // Return `false` instead if we want to stop searching for other colliders with an intersecting AABB.
});
}
The closures given to these methods are called with the entity of each collider found, as well as its Rapier collider
(rapier::geometry::Collider), and can return false to stop the search. The AABB to test is given as a Bevy
Aabb2d in 2D, or Aabb3d in 3D.
It is possible to only apply the scene query to a subsets of the colliders using a query filter