Skip to main content

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 QueryPipeline::intersections_with_shapeRapierContext::intersections_with_shapeWorld.intersectionsWithShape searches for all the colliders with shapes intersecting the given shape.
  • The approximate intersection test QueryPipeline::colliders_with_aabb_intersecting_aabbRapierContext::colliders_with_aabb_intersecting_aabbWorld.collidersWithAabbIntersectingAabb searches 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.
info

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.

let shape = Cuboid::new(vector![1.0, 2.0]);
let shape_pos = Isometry::new(vector![0.0, 1.0], 0.8);
let filter = QueryFilter::default();

query_pipeline.intersections_with_shape(rigid_body_set,
collider_set, &shape_pos, &shape, filter, |handle| {
println!("The collider {:?} intersects our shape.", handle);
true // Return `false` instead if we want to stop searching for other colliders that contain this point.
}
);

let aabb = Aabb::new(point![-1.0, -2.0], point![1.0, 2.0]);
query_pipeline.colliders_with_aabb_intersecting_aabb(&aabb, |handle| {
println!("The collider {:?} has an AABB intersecting our test AABB", handle);
true // Return `false` instead if we want to stop searching for other colliders that contain this point.
});
/* Test intersections inside of a system. */
fn test_intersections(rapier_context: Res<RapierContext>) {
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.intersections_with_shape(shape_pos, shape_rot, &shape, filter, |entity| {
println!("The entity {:?} intersects our shape.", entity);
true // Return `false` instead if we want to stop searching for other colliders that contain this point.
});

let aabb = Aabb::from_min_max(Vec3::new(-1.0, -2.0, 0.0), Vec3::new(1.0, 2.0, 0.0));
rapier_context.colliders_with_aabb_intersecting_aabb(aabb, |entity| {
println!(
"The entity {:?} has an AABB intersecting our test AABB",
entity
);
true // Return `false` instead if we want to stop searching for other colliders that contain this point.
});
}
let shape = new RAPIER.Cuboid(1.0, 2.0);
let shapePos = { x: 1.0, y: 2.0 };
let shapeRot = 0.1;

world.intersectionsWithShape(shapePos, shapeRot, shape, (handle) => {
console.log("The collider", handle, "intersects our shape.");
return true; // Return `false` instead if we want to stop searching for other colliders that contain this point.
});

let aabbCenter = { x: -1.0, y: -2.0 };
let aabbHalfExtents = { x: 0.5, y: 0.6 };
world.collidersWithAabbIntersectingAabb(aabbCenter, aabbHalfExtents, (handle) => {
console.log("The collider", handle, "has an AABB intersecting our test AABB");
return true; // Return `false` instead if we want to stop searching for other colliders that contain this point.
});

It is possible to only apply the scene query to a subsets of the colliders using a query filter