Function avian3d::collision::contact_query::closest_points

source ·
pub fn closest_points(
    collider1: &Collider,
    position1: impl Into<Position>,
    rotation1: impl Into<Rotation>,
    collider2: &Collider,
    position2: impl Into<Position>,
    rotation2: impl Into<Rotation>,
    max_distance: Scalar
) -> Result<ClosestPoints, UnsupportedShape>
Expand description

Computes the ClosestPoints between two Colliders.

Returns Err(UnsupportedShape) if either of the collider shapes is not supported.

§Example

use avian3d::prelude::{contact_query::*, *};
use bevy::prelude::*;

let collider1 = Collider::sphere(0.5);
let collider2 = Collider::cuboid(1.0, 1.0, 1.0);

// The shapes are intersecting
assert_eq!(
    closest_points(
        &collider1,
        Vec3::default(),
        Quat::default(),
        &collider2,
        Vec3::default(),
        Quat::default(),
        2.0,
    )
    .expect("Unsupported collider shape"),
    ClosestPoints::Intersecting,
);

// The shapes are not intersecting but the distance between the closest points is below 2.0
assert_eq!(
    closest_points(
        &collider1,
        Vec3::default(),
        Quat::default(),
        &collider2,
        Vec3::X * 1.5,
        Quat::default(),
        2.0,
    )
    .expect("Unsupported collider shape"),
    ClosestPoints::WithinMargin(Vec3::X * 0.5, Vec3::X * 1.0),
);

// The shapes are not intersecting and the distance between the closest points exceeds 2.0
assert_eq!(
    closest_points(
        &collider1,
        Vec3::default(),
        Quat::default(),
        &collider2,
        Vec3::X * 5.0,
        Quat::default(),
        2.0,
    )
    .expect("Unsupported collider shape"),
    ClosestPoints::OutsideMargin,
);