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.intersect_shapesearches for all the colliders with shapes intersecting the given shape. - The approximate intersection test
QueryPipeline.intersect_aabb_conservativesearches 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 toPhysicsWorld.step): 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.
shape = rp.SharedShape.cuboid(1.0, 2.0, 3.0)
shape_pos = rp.Isometry3(translation=(0.0, 1.0, 0.0), rotation=rp.rotation_from_angle((0.2, 0.7, 0.1)))
query_filter = rp.QueryFilter()
query_pipeline = world.query_pipeline
def on_shape_intersection(handle):
print(f"The collider {handle} intersects our shape.")
return True # Return `False` to stop the search.
query_pipeline.intersect_shape(shape_pos, shape, on_shape_intersection, filter=query_filter)
aabb = rp.Aabb(mins=(-1.0, -2.0, -3.0), maxs=(1.0, 2.0, 3.0))
def on_aabb_intersection(handle):
print(f"The collider {handle} has an AABB intersecting our test AABB.")
return True # Return `False` to stop the search.
query_pipeline.intersect_aabb_conservative(aabb, on_aabb_intersection, filter=query_filter)
Both methods call the given function with the handle of each collider found, until this function returns False. The
AABB to test is an Aabb, given by its minimum (mins) and maximum (maxs) corners. If you only need to know
whether at least one collider has an AABB intersecting the given AABB, QueryPipeline.test_aabb returns this as a
boolean.
It is possible to only apply the scene query to a subsets of the colliders using a query filter