avian3d::collision::contact_query

Function distance

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

Computes the minimum distance separating two Colliders.

Returns 0.0 if the colliders are touching or penetrating, and Err(UnsupportedShape) if either of the collider shapes is not supported.

ยงExample

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

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

// The distance is 1.0
assert_eq!(
    distance(
        &collider1,
        Vec3::default(),
        Quat::default(),
        &collider2,
        Vec3::X * 2.0,
        Quat::default(),
    )
    .expect("Unsupported collider shape"),
    1.0,
);

// The colliders are penetrating, so the distance is 0.0
assert_eq!(
    distance(
        &collider1,
        Vec3::default(),
        Quat::default(),
        &collider2,
        Vec3::default(),
        Quat::default(),
    )
    .expect("Unsupported collider shape"),
    0.0,
);