Skip to main content

time_of_impact

Function time_of_impact 

Source
pub fn time_of_impact(
    collider1: &Collider,
    position1: RVector,
    rotation1: impl Into<Quat>,
    velocity1: Vec3,
    collider2: &Collider,
    position2: RVector,
    rotation2: impl Into<Quat>,
    velocity2: Vec3,
    max_time_of_impact: f32,
) -> Result<Option<TimeOfImpact>, UnsupportedShape>
Expand description

Computes when two moving Colliders hit each other for the first time.

Returns Ok(None) if the time of impact is greater than max_time_of_impact and Err(UnsupportedShape) if either of the collider shapes is not supported.

ยงExample

use avian3d::{collision::collider::contact_query::time_of_impact, math::RVec3, prelude::*};
use bevy::prelude::*;

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

let result = time_of_impact(
    &collider1,         // Collider 1
    RVec3::NEG_X * 5.0, // Position 1
    Quat::default(),    // Rotation 1
    Vec3::X,            // Linear velocity 1
    &collider2,         // Collider 2
    RVec3::X * 5.0,     // Position 2
    Quat::default(),    // Rotation 2
    Vec3::NEG_X,        // Linear velocity 2
    100.0,              // Maximum time of impact
)
.expect("Unsupported collider shape");

assert_eq!(result.unwrap().time_of_impact, 4.5);