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
r3IntersectShapesearches for all the colliders with shapes intersecting the given shape. - The approximate intersection test
r3IntersectAabbConservativesearches 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 call tor3Step): 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
R2SharedShape *shape = r2CuboidSharedShape(r2Vector(1.0, 2.0));
R2Pose shape_pos = r2Pose(r2Vector(0.0, 1.0), r2Rotation(0.2));
R2QueryOptions options = r2DefaultQueryOptions();
// Get the number of colliders intersecting the shape, then copy their handles.
size_t count = r2IntersectShape(world, &options, shape_pos, shape, NULL, 0);
R2ColliderHandle *handles = malloc(count * sizeof(*handles));
count = r2IntersectShape(world, &options, shape_pos, shape, handles, count);
for (size_t i = 0; i < count; i++) {
printf("The collider %u intersects our shape.\n", handles[i].index);
}
free(handles);
r2FreeSharedShape(shape);
R2Aabb aabb = {r2Vector(-1.0, -2.0), r2Vector(1.0, 2.0)};
count = r2IntersectAabbConservative(world, &options, aabb, NULL, 0);
handles = malloc(count * sizeof(*handles));
count = r2IntersectAabbConservative(world, &options, aabb, handles, count);
for (size_t i = 0; i < count; i++) {
printf("The collider %u has an AABB intersecting our test AABB.\n", handles[i].index);
}
free(handles);
R3SharedShape *shape = r3CuboidSharedShape(r3Vector(1.0, 2.0, 3.0));
// The rotation is given as a scaled axis, i.e., an axis multiplied by the angle.
R3Vector scaled_axis = r3Vector(0.2, 0.7, 0.1);
R3Pose shape_pos = r3Pose(r3Vector(0.0, 1.0, 0.0),
r3RotationFromAxisAngle(scaled_axis, r3VectorLength(scaled_axis)));
R3QueryOptions options = r3DefaultQueryOptions();
// Get the number of colliders intersecting the shape, then copy their handles.
size_t count = r3IntersectShape(world, &options, shape_pos, shape, NULL, 0);
R3ColliderHandle *handles = malloc(count * sizeof(*handles));
count = r3IntersectShape(world, &options, shape_pos, shape, handles, count);
for (size_t i = 0; i < count; i++) {
printf("The collider %u intersects our shape.\n", handles[i].index);
}
free(handles);
r3FreeSharedShape(shape);
R3Aabb aabb = {r3Vector(-1.0, -2.0, -3.0), r3Vector(1.0, 2.0, 3.0)};
count = r3IntersectAabbConservative(world, &options, aabb, NULL, 0);
handles = malloc(count * sizeof(*handles));
count = r3IntersectAabbConservative(world, &options, aabb, handles, count);
for (size_t i = 0; i < count; i++) {
printf("The collider %u has an AABB intersecting our test AABB.\n", handles[i].index);
}
free(handles);
Both functions copy the handles of the colliders found into a buffer given by the application, as described at the
beginning of this page. The AABB to test is an R3Aabb, given by its minimum (mins) and maximum (maxs) corners.
It is possible to only apply the scene query to a subsets of the colliders using a query filter