Skip to content

Commit

Permalink
fix last step issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ughuuu committed Aug 5, 2024
1 parent 152940d commit 5adffed
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions src/rapier_wrapper/physics_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use rapier::prelude::*;

use crate::rapier_wrapper::prelude::*;
use crate::servers::rapier_physics_singleton::PhysicsCollisionObjects;
use crate::spaces::rapier_space::RapierSpace;
#[derive(Default)]
pub struct OneWayDirection {
pub body1: bool,
Expand All @@ -28,6 +27,7 @@ pub struct PhysicsHooksCollisionFilter<'a> {
pub collision_filter_body_callback: &'a CollisionFilterCallback,
pub collision_modify_contacts_callback: &'a CollisionModifyContactsCallback,
pub physics_collision_objects: &'a PhysicsCollisionObjects,
pub last_step: Real,
}
impl<'a> PhysicsHooks for PhysicsHooksCollisionFilter<'a> {
fn filter_contact_pair(&self, context: &PairFilterContext) -> Option<SolverFlags> {
Expand Down Expand Up @@ -81,7 +81,6 @@ impl<'a> PhysicsHooks for PhysicsHooksCollisionFilter<'a> {
if let Some(contact) = context.manifold.find_deepest_contact() {
dist = -contact.dist;
}
let last_step = RapierSpace::get_last_step();
let rigid_body_1_linvel = one_way_direction.previous_linear_velocity1;
let rigid_body_2_linvel = one_way_direction.previous_linear_velocity2;
// ghost collisions
Expand All @@ -91,7 +90,7 @@ impl<'a> PhysicsHooks for PhysicsHooksCollisionFilter<'a> {
return;
}
let normal_dot_velocity = normal.dot(&rigid_body_1_linvel.normalize());
let velocity_magnitude = rigid_body_1_linvel.magnitude() * last_step;
let velocity_magnitude = rigid_body_1_linvel.magnitude() * self.last_step;
let length_along_normal = velocity_magnitude * Real::max(normal_dot_velocity, 0.0);
if normal_dot_velocity >= -DEFAULT_EPSILON {
let diff = dist - length_along_normal;
Expand All @@ -110,7 +109,7 @@ impl<'a> PhysicsHooks for PhysicsHooksCollisionFilter<'a> {
return;
}
let normal_dot_velocity = normal.dot(&rigid_body_2_linvel.normalize());
let velocity_magnitude = rigid_body_2_linvel.magnitude() * last_step;
let velocity_magnitude = rigid_body_2_linvel.magnitude() * self.last_step;
let length_along_normal = velocity_magnitude * Real::max(normal_dot_velocity, 0.0);
if normal_dot_velocity >= -DEFAULT_EPSILON {
let diff = dist - length_along_normal;
Expand All @@ -129,15 +128,15 @@ impl<'a> PhysicsHooks for PhysicsHooksCollisionFilter<'a> {
let body_margin1 = one_way_direction.pixel_body1_margin;
let motion_len = rigid_body_2_linvel.magnitude();
let velocity_dot1 = rigid_body_2_linvel.normalize().dot(&allowed_local_n1);
let length_along_normal = motion_len * Real::max(velocity_dot1, 0.0) * last_step;
let length_along_normal = motion_len * Real::max(velocity_dot1, 0.0) * self.last_step;
contact_is_pass_through |= velocity_dot1 <= DEFAULT_EPSILON
&& dist - length_along_normal > body_margin1
&& length_along_normal > DEFAULT_EPSILON;
} else if one_way_direction.body2 {
let velocity_dot2 = rigid_body_1_linvel.normalize().dot(&allowed_local_n2);
let motion_len = rigid_body_1_linvel.magnitude();
let body_margin2 = one_way_direction.pixel_body2_margin;
let length_along_normal = motion_len * Real::max(velocity_dot2, 0.0) * last_step;
let length_along_normal = motion_len * Real::max(velocity_dot2, 0.0) * self.last_step;
contact_is_pass_through |= velocity_dot2 <= DEFAULT_EPSILON
&& dist - length_along_normal > body_margin2
&& length_along_normal > DEFAULT_EPSILON;
Expand Down
1 change: 1 addition & 0 deletions src/rapier_wrapper/physics_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ impl PhysicsWorld {
collision_filter_body_callback: &collision_filter_body_callback,
collision_modify_contacts_callback: &collision_modify_contacts_callback,
physics_collision_objects,
last_step: RapierSpace::get_last_step(),
};
// Initialize the event collector.
let (collision_send, collision_recv) = crossbeam::channel::unbounded();
Expand Down

0 comments on commit 5adffed

Please sign in to comment.