# Scene queries

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

## 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.

let ray = new RAPIER.Ray({ x: 1.0, y: 2.0 }, { x: 0.0, y: 1.0 });
let maxToi = 4.0;
let solid = true;
let hit = world.castRay(ray, maxToi, solid);
if (hit != null) {
// The first collider hit has the handle hit.colliderHandle and it hit after
// the ray travelled a distance equal to ray.dir * toi.
let hitPoint = ray.pointAt(hit.toi); // Same as: ray.origin + ray.dir * toi
console.log("Collider", hit.colliderHandle, "hit at point", hitPoint);
}
let hitWithNormal = world.castRayAndGetNormal(ray, maxToi, solid);
if (hitWithNormal != null) {
// 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 hitPoint = ray.pointAt(hitWithNormal.toi);
console.log("Collider", hitWithNormal.colliderHandle, "hit at point", hitPoint, "with normal", hitWithNormal.normal);
}
world.intersectionsWithRay(ray, maxToi, solid, (hit) => {
// Callback called on each collider hit by the ray.
let hitPoint = ray.pointAt(hit.toi);
console.log("Collider", hit.colliderHandle, "hit at point", hitPoint, "with normal", hit.normal);
return 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:

• maxToi : 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:

In addition, it is possible to only apply the scene query to a subsets of the colliders using a query filter.

## 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 and filter arguments in the ray-casting section.

There is only one shape-casting method: World.castShape. This method has similar arguments as World.castRay 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):

let shape = new RAPIER.Cuboid(1.0, 2.0);
let shapePos = { x: 0.0, y: 1.0 };
let shapeRot = 0.2;
let shapeVel = { x: 0.1, y: 0.4 };
let maxToi = 4.0;
let hit = world.castShape(shapePos, shapeRot, shapeVel, shape, maxToi);
if (hit != null) {
// The first collider hit has the handle handle. The hit is a
// structure containing details about the hit configuration.
console.log("Hit the collider", hit.colliderHandle, "at time", hit.toi);
}

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 shapeVel * 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 (World.projectPoint), or will enumerate every collider containing given point (World.intersectionsWithPoint).

let point = { x: 1.0, y: 2.0 };
let solid = true;
let proj = world.projectPoint(point, solid);
if (proj != null) {
// The collider closest to the point has this handle.
console.log("Projected point on collider ", proj.colliderHandle, ". Point projection: ", proj.point);
console.log("Point was inside of the collider shape: {}", proj.isInside);
}
world.intersectionsWithPoint(point, (handle) => {
// Callback called on each collider with a shape containing the point.
console.log("The collider", handle, "contains the point.");
// Return false instead if we want to stop searching for other colliders containing this point.
return true;
});

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

## 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 World.intersectionsWithShape searches for all the colliders with shapes intersecting the given shape.
• The approximate intersection test World.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 = 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

## Query filters#

It is common to exclude some colliders from being considered by a scene query. For example, a ray-cast performed for a character controller will usually want to skip the character itself. Sometimes, we may even want it to ignore both the character and any collider attached to a dynamic rigid-body, and ignore all sensors. To allow this filtering, most scene queries take several optional arguments that lets you describe what needs to be excluded. In particular, the arguments:

• flags allows you to discard whole families of colliders based on their types or their parent types (e.g. exclude all sensors and all the colliders attached to a dynamic rigid-body).
• groups is used to apply the collision group rules for the scene query. The scene query will only consider hits with colliders with collision groups compatible with this collision group (using the bitwise test described in the collision groups section).
• exclude_collider is the handle of one collider the query must ignore.
• exclude_rigid_body is the handle of one rigid-body with attached colliders the query must ignore.
• predicate is a user-defined closure to apply any filtering rule. This can be used if the other filtering options above are not flexible enough.

Here is an an example of usage of the query filters with ray-casting:

let ray = new RAPIER.Ray({ x: 1.0, y: 2.0 }, { x: 0.0, y: 1.0 });
let maxToi = 4.0;
let solid = true;
let filterFlags = QueryFilterFlags.EXCLUDE_DYNAMIC,
let filterGroups = 0x000b0001,
let filterExcludeRigidBody = player_rigid_body,
let filterPredicate = (collider: Collider) => collider.userData == 10.0,
let hit = world.castRay(ray, maxToi, solid, filterFlags, filterGroups, null, filterExcludeRigidBody, filterPredicate);
if (hit != null) {
// Handle the hit.
}