time_of_impact_query2d/
time_of_impact_query2d.rs

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