Handle closest points query results with ClosestPoints
masterThe ClosestPoints enum represents the outcome of a proximity query between two shapes, considering a maximum search distance (max_dist). It is useful for AI perception, trigger zones, and physics optimizations.
Variants
Intersecting: The shapes are overlapping or touching. For detailed contact information like penetration depth and normals, use thecontactfunction instead.WithinMargin(Vector, Vector): The shapes are separated but within the specifiedmax_dist. It contains two world-space vectors:- The first vector is the closest point on the surface of the first shape.
- The second vector is the closest point on the surface of the second shape.
Disjoint: The shapes are separated by more than the specifiedmax_dist. No closest points are computed, which saves computation time.
Use Cases
- AI perception: Finding the nearest enemy within a detection range.
- Trigger zones: Detecting when objects approach a specific threshold distance.
- LOD systems: Computing detailed interactions only for nearby objects.
- Physics optimization: Skipping expensive computations for distant pairs.
# #[cfg(all(feature = "dim3", feature = "f32"))] {
use parry3d::query::{closest_points, ClosestPoints};
use parry3d::shape::Ball;
use parry3d::math::Pose;
let ball1 = Ball::new(1.0);
let ball2 = Ball::new(1.0);
let pos1 = Pose::translation(0.0, 0.0, 0.0);
let pos2 = Pose::translation(5.0, 0.0, 0.0);
// Search for closest points within 10.0 units
let result = closest_points(&pos1, &ball1, &pos2, &ball2, 10.0).unwrap();
match result {
ClosestPoints::Intersecting => {
println!("Shapes are overlapping!");
}
ClosestPoints::WithinMargin(pt1, pt2) => {
println!("Closest point on shape1: {:?}", pt1);
println!("Closest point on shape2: {:?}", pt2);
let distance = (pt2 - pt1).length();
println!("Distance between shapes: {}", distance);
}
ClosestPoints::Disjoint => {
println!("Shapes are more than 10.0 units apart");
}
}
# }