Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect stop_on_penetration flag in ball-ball shape casting #206

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions crates/parry2d/tests/geometry/time_of_impact2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,70 @@ use parry2d::query;
use parry2d::query::details::ShapeCastOptions;
use parry2d::shape::{Ball, Cuboid, Polyline, Segment};

#[test]
fn ball_ball_intersecting_toi() {
let ball1 = Ball::new(1.0);
let ball2 = Ball::new(2.0);

let ball1_pos_intersecting = Isometry2::new(Vector2::new(1.0, 1.0), 0.0);
let ball2_pos = Isometry2::identity();

let ball1_vel_separating = Vector2::new(1.0, 2.0);
let ball1_vel_penetrating = Vector2::new(1.0, -2.0);
let ball2_vel = Vector2::zeros();

let toi_separating = query::cast_shapes(
&ball1_pos_intersecting,
&ball1_vel_separating,
&ball1,
&ball2_pos,
&ball2_vel,
&ball2,
ShapeCastOptions::default(),
)
.unwrap();

let toi_penetrating_ignore_pen = query::cast_shapes(
&ball1_pos_intersecting,
&ball1_vel_penetrating,
&ball1,
&ball2_pos,
&ball2_vel,
&ball2,
ShapeCastOptions {
stop_at_penetration: false,
..Default::default()
},
)
.unwrap();

let toi_separating_ignore_pen = query::cast_shapes(
&ball1_pos_intersecting,
&ball1_vel_separating,
&ball1,
&ball2_pos,
&ball2_vel,
&ball2,
ShapeCastOptions {
stop_at_penetration: false,
..Default::default()
},
)
.unwrap();

assert_eq!(toi_separating.map(|hit| hit.time_of_impact), Some(0.0));

assert_eq!(
toi_penetrating_ignore_pen.map(|hit| hit.time_of_impact),
Some(0.0),
);

assert_eq!(
toi_separating_ignore_pen.map(|hit| hit.time_of_impact),
None
);
}

#[test]
fn ball_cuboid_toi() {
let cuboid = Cuboid::new(Vector2::new(1.0, 1.0));
Expand Down
4 changes: 4 additions & 0 deletions src/query/shape_cast/shape_cast_ball_ball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ pub fn cast_shapes_ball_ball(
witness2 = Point::from(*normal2 * b2.radius);
}

if !options.stop_at_penetration && time_of_impact < 1.0e-5 && normal1.dot(vel12) >= 0.0 {
return None;
}

let status = if inside && center.coords.norm_squared() < rsum * rsum {
ShapeCastStatus::PenetratingOrWithinTargetDist
} else {
Expand Down