time_of_impact_query3d/
time_of_impact_query3d.rs

1use parry3d::math::{Pose, Vector};
2use parry3d::query::{self, ShapeCastOptions};
3use parry3d::shape::{Ball, Cuboid};
4
5fn main() {
6    let cuboid = Cuboid::new(Vector::new(1.0, 1.0, 1.0));
7    let ball = Ball::new(1.0);
8
9    let cuboid_pos = Pose::identity();
10    let ball_pos_intersecting = Pose::translation(1.0, 1.0, 1.0);
11    let ball_pos_will_touch = Pose::translation(2.0, 2.0, 2.0);
12    let ball_pos_wont_touch = Pose::translation(3.0, 3.0, 3.0);
13
14    let cuboid_vel1 = Vector::new(-1.0, 1.0, 1.0);
15    let cuboid_vel2 = Vector::new(1.0, 1.0, 1.0);
16
17    let ball_vel1 = Vector::new(2.0, 2.0, 2.0);
18    let ball_vel2 = Vector::new(-0.5, -0.5, -0.5);
19
20    let toi_intersecting = query::cast_shapes(
21        &ball_pos_intersecting,
22        ball_vel1,
23        &ball,
24        &cuboid_pos,
25        cuboid_vel1,
26        &cuboid,
27        ShapeCastOptions::default(),
28    )
29    .unwrap();
30    let toi_will_touch = query::cast_shapes(
31        &ball_pos_will_touch,
32        ball_vel2,
33        &ball,
34        &cuboid_pos,
35        cuboid_vel2,
36        &cuboid,
37        ShapeCastOptions::default(),
38    )
39    .unwrap();
40    let toi_wont_touch = query::cast_shapes(
41        &ball_pos_wont_touch,
42        ball_vel1,
43        &ball,
44        &cuboid_pos,
45        cuboid_vel1,
46        &cuboid,
47        ShapeCastOptions::default(),
48    )
49    .unwrap();
50
51    assert_eq!(toi_intersecting.map(|hit| hit.time_of_impact), Some(0.0));
52    assert!(toi_will_touch.is_some() && toi_will_touch.unwrap().time_of_impact > 0.0);
53    assert_eq!(toi_wont_touch.map(|hit| hit.time_of_impact), None);
54}