# Scene queries

Scene queries are geometric queries that take all the colliders of the physics world into account. These queries are available through the QueryPipeline.

## Ray-casting#

Ray-casting is a geometric query that finds one or several colliders intersecting a half-line. Ray-casting is an extremely common operation that covers a wide variety of use-cases: firing bullets, character controllers, rendering (for ray-tracing), etc.

A ray is defined by its origin and its direction: it can be interpreted as a single point moving in a straight line towards the ray direction.

##### info

In addition to the ray geometric information, ray-casting method allow additional control over the behavior of the ray cast like limiting the length of the ray and ignoring some colliders. See the detailed ray-cast arguments description after the next example.

There are multiple ray-casting methods yielding more or less detailed results (see example bellow). The more results you get, the more computationally expensive the ray-cast will be.

/* Cast a ray inside of a system. */
fn cast_ray(rapier_context: Res<RapierContext>) {
let ray_pos = Vec2::new(1.0, 2.0);
let ray_dir = Vec2::new(0.0, 1.0);
let max_toi = 4.0;
let solid = true;
let groups = InteractionGroups::all();
let filter = None;
if let Some((entity, toi)) = rapier_context.cast_ray(
ray_pos, ray_dir, max_toi, solid, groups, filter
) {
// The first collider hit has the entity entity and it hit after
// the ray travelled a distance equal to ray_dir * toi.
let hit_point = ray_pos + ray_dir * toi;
println!("Entity {:?} hit at point {}", entity, hit_point);
}
if let Some((entity, intersection)) = rapier_context.cast_ray_and_get_normal(
ray_pos, ray_dir, max_toi, solid, groups, filter
) {
// This is similar to QueryPipeline::cast_ray illustrated above except
// that it also returns the normal of the collider shape at the hit point.
let hit_point = intersection.point;
let hit_normal = intersection.normal;
println!("Entity {:?} hit at point {} with normal {}", entity, hit_point, hit_normal);
}
rapier_context.intersections_with_ray(
ray_pos, ray_dir, max_toi, solid, groups, filter,
|entity, intersection| {
// Callback called on each collider hit by the ray.
let hit_point = intersection.point;
let hit_normal = intersection.normal;
println!("Entity {:?} hit at point {} with normal {}", entity, hit_point, hit_normal);
true // Return false instead if we want to stop searching for other hits.
});
}
}

Aside from the ray being cast, all these ray-casting methods take a few extra parameters for controlling the behavior of the ray-cast:

• max_toi : is the maximum "time-of-impact" that can be reported by the ray-cast. The notion of "time-of-impact" refer to the fact that a ray can be seen as a point starting at ray.origin moving at a linear velocity equal to ray.dir. Therefore, max_toi limits the ray-cast to the segment: [ray.origin, ray.origin + ray.dir * max_toi].
• solid: this argument controls the behavior of the ray-cast if ray.origin is inside of a shape: if solid is true then the hit point will be the ray origin itself (toi = 0.0) because the interior of the shape will be assumed to be filled with material. If solid is false then the shape will be assumed to have an empty interior and the hit point will be the first time the ray hits the shape's boundary. The following 2D example illustrates the difference between the two scenarios. The ray is in green and the resulting hit point circled in red:

• groups: just like colliders, the ray is given a collision group. The ray-cast will only test intersections with colliders with collision groups compatible with the ray's collision group (using the bitwise test described in the collision groups section).

• filter: if collision groups are not flexible enough, a custom closure can be given optionally. The ray-cast will only test intersection with colliders for which the filter closure returns true.

## Shape-casting#

Shape-casting (aka. sweep tests) is the big brother of ray-casting. The only difference with ray-cast is that instead of being a point travelling along a straight line, we have a complete shape travelling along a straight line. This is typically used for character controllers in games to determine by how much the player can move before it hits the environment.

##### info

Just like ray-casting, it is possible to control the behavior of the shape-casting like limiting the distance travelled by the shape cast, and ignoring some colliders. See the details about the max_toi, groups and filter arguments in the ray-casting section.

There is only one shape-casting method: RapierContext::cast_shape. This method has similar arguments as RapierContext::cast_ray except that the ray is replaced by three arguments: the shape being cast, the initial position of the shape (this is analog to ray.origin) and the linear velocity the shape is travelling at (this is analog to ray.dir):

/* Cast a shape inside of a system. */
fn cast_shape(rapier_context: Res<RapierContext>) {
let shape = Collider::cuboid(1.0, 2.0);
let shape_pos = Vec2::new(1.0, 2.0);
let shape_rot = 0.8;
let shape_vel = Vec2::new(0.1, 0.4);
let max_toi = 4.0;
let groups = InteractionGroups::all();
let filter = None;
if let Some((entity, hit)) = query_pipeline.cast_shape(
shape_pos, shape_rot, shape_vel, &shape, max_toi, groups, filter
) {
// The first collider hit has the entity entity. The hit is a
// structure containing details about the hit configuration.
println!("Hit the entity {:?} with the configuration: {:?}", entity, hit);
}
}

The result of the shape-casting includes the handle of the first collider being hit, as well as detailed information about the geometry of the hit:

• hit.toi: indicates the time of impact between the shape and the collider hit. This means that after travelling a distance of shape_vel * hit.toi the collider and the cast shape are exactly touching. If hit.toi == 0.0 then the shape is already intersecting a collider at its initial position.
• hit.witness1: indicates the contact point when the cast shape and the collider are touching, expressed in the local-space of the collider hit by the shape.
• hit.witness2: indicates the contact point when the cast shape and the collider are touching, expressed in the local-space of the cast shape.
• hit.normal1: indicates the normal at the contact point hit.witness1, expressed in the local-space of the collider hit by the shape.
• hit.normal2: indicates the normal at the contact point hit.witness2, expressed in the local-space of the cast shape.

## Point projection#

Point projection will either project a point on the closest collider of the scene (RapierContext::project_point), or will enumerate every collider containing given point (RapierContext::intersections_with_point).

/* Project a point inside of a system. */
fn project_point(rapier_context: Res<RapierContext>) {
let point = Vec2::new(1.0, 2.0);
let solid = true;
let groups = InteractionGroups::all();
let filter = None;
if let Some((entity, projection)) = rapier_context.project_point(
&point, solid, groups, filter
) {
// The collider closest to the point has this handle.
println!("Projected point on entity {:?}. Point projection: {}", entity, projection.point);
println!("Point was inside of the collider shape: {}", projection.is_inside);
}
rapier_context.intersections_with_point(
&point, groups, filter, |entity| {
// Callback called on each collider with a shape containing the point.
println!("The entity {:?} contains the point.", entity);
// Return false instead if we want to stop searching for other colliders containing this point.
true
}
);
}

The solid, group and filter arguments are used to control the way point-projection works: by ignoring the shape interiors and/or ignoring some colliders. See their description on the ray-casting section.

## 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::intersections_with_shape searches for all the colliders with shapes intersecting the given shape.
• The approximate intersection test RapierContext::colliders_with_aabb_intersecting_aabb 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.

/* 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 groups = InteractionGroups::all();
let filter = None;
rapier_context.intersections_with_shape(
&shape_pos, &shape_rot, &shape, groups, 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.
});
}

Use the group and filter arguments to exclude some colliders from these intersection tests. See their description on the ray-casting section.