Skip to main content

scene_queries_point_projection

Point projection will either project a point on the closest collider of the scene (r3TryProjectPoint), or will enumerate every collider containing given point (r3IntersectPoint).

R2Vector point = r2Vector(1.0, 2.0);
R2Bool solid = 1;
R2Real max_dist = 12.0;
R2QueryOptions options = r2DefaultQueryOptions();

R2OptionalPointProjection result = r2TryProjectPoint(world, &options, point, max_dist, solid);
if (result.found) {
R2PointProjection projection = result.projection;
// The collider closest to the point has the handle `projection.collider`.
printf("Projected point on collider %u. Point projection: (%f, %f)\n", projection.collider.index,
(double)projection.point.x, (double)projection.point.y);
printf("Point was inside of the collider shape: %u\n", projection.is_inside);
}

// Get the number of colliders containing the point, then copy their handles.
size_t count = r2IntersectPoint(world, &options, point, NULL, 0);
R2ColliderHandle *handles = malloc(count * sizeof(*handles));
count = r2IntersectPoint(world, &options, point, handles, count);
for (size_t i = 0; i < count; i++) {
// Loop on each collider with a shape containing the point.
printf("The collider %u contains the point.\n", handles[i].index);
}
free(handles);

The resulting R3PointProjection (the projection field of the result) contains the handle of the collider the point was projected on, the projected point (in world-space), and whether the original point was inside of that collider (is_inside). If the point is inside of a shape, solid controls the result just like for ray-casting: with solid set to 1 the point is its own projection, whereas with solid set to 0 it is projected on the boundary of the shape. r3TryProjectPoint sets the found field of its result to 0 if no collider is closer than max_dist, whereas r3ProjectPoint reports this as the R3_NOT_FOUND error. Finally, r3IntersectPoint copies the handles of the colliders containing the point into a buffer given by the application, as described at the beginning of this page.

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