Skip to content

Commit

Permalink
fix areas removed issue (#187)
Browse files Browse the repository at this point in the history
- Fixes #186
- Fixes #185
  • Loading branch information
Ughuuu authored Aug 8, 2024
1 parent 6b1f91f commit b676e0d
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 93 deletions.
9 changes: 1 addition & 8 deletions src/bodies/rapier_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::*;
feature = "serde-serialize",
derive(serde::Serialize, serde::Deserialize)
)]
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
struct MonitorInfo {
#[cfg_attr(feature = "serde-serialize", serde(skip, default = "invalid_rid"))]
pub rid: Rid,
Expand Down Expand Up @@ -193,7 +194,6 @@ impl RapierArea {
if self.monitor_callback.is_none() {
return;
}
self.base.area_detection_counter += 1;
let handle_pair_hash = (collider_handle, area_collider_handle);
if let Some(monitored_object) = self.monitored_objects.get(&handle_pair_hash) {
// in case it already exited this frame and now it enters, cancel out the event
Expand Down Expand Up @@ -244,7 +244,6 @@ impl RapierArea {
if self.monitor_callback.is_none() {
return;
}
self.base.area_detection_counter -= 1;
let handle_pair_hash = (collider_handle, area_collider_handle);
if let Some(monitored_object) = self.monitored_objects.get(&handle_pair_hash) {
// in case it already entered this frame and now it exits, cancel out the event
Expand Down Expand Up @@ -288,7 +287,6 @@ impl RapierArea {
if !other_area.is_monitorable() {
return;
}
other_area.base.area_detection_counter += 1;
}
} else {
godot_error!("other area is null");
Expand Down Expand Up @@ -336,11 +334,6 @@ impl RapierArea {
if !other_area.is_monitorable() {
return;
}
if other_area.base.area_detection_counter == 0 {
godot_error!("Area is not being monitored");
return;
}
other_area.base.area_detection_counter -= 1;
}
}
let handle_pair_hash = (collider_handle, area_collider_handle);
Expand Down
6 changes: 0 additions & 6 deletions src/bodies/rapier_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,6 @@ impl RapierBody {
}

pub fn add_area(&mut self, p_area: &RapierArea, space: &mut RapierSpace) {
self.base.area_detection_counter += 1;
if p_area.has_any_space_override() {
let area_rid = p_area.get_base().get_rid();
let priority = p_area.get_priority();
Expand All @@ -550,11 +549,6 @@ impl RapierBody {
if !self.base.is_space_valid() {
return;
}
if self.base.area_detection_counter == 0 {
godot_error!("Area detection counter is zero");
return;
}
self.base.area_detection_counter -= 1;
self.areas.retain(|&x| x.rid != area);
self.on_area_updated(area, space);
}
Expand Down
45 changes: 19 additions & 26 deletions src/bodies/rapier_collision_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub trait IRapierCollisionObject: Sync {
physics_spaces: &mut PhysicsSpaces,
);
}
#[derive(Debug, PartialEq, Copy, Clone)]
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(
feature = "serde-serialize",
derive(serde::Serialize, serde::Deserialize)
Expand Down Expand Up @@ -176,7 +176,6 @@ pub struct RapierCollisionObject {
pub(crate) activation_angular_threshold: real,
pub(crate) activation_linear_threshold: real,
pub(crate) activation_time_until_sleep: real,
pub(crate) area_detection_counter: u32,
}
impl Default for RapierCollisionObject {
fn default() -> Self {
Expand Down Expand Up @@ -223,7 +222,6 @@ impl RapierCollisionObject {
activation_angular_threshold,
activation_linear_threshold,
activation_time_until_sleep,
area_detection_counter: 0,
}
}

Expand All @@ -236,7 +234,7 @@ impl RapierCollisionObject {
physics_shapes: &mut PhysicsShapes,
) -> ColliderHandle {
if shape.collider_handle != ColliderHandle::invalid() {
godot_error!("collider is invalid");
godot_error!("collider is valid");
}
let mut handle = ColliderHandle::invalid();
if let Some(shape_object) = physics_shapes.get_mut(&shape.shape) {
Expand Down Expand Up @@ -296,16 +294,14 @@ impl RapierCollisionObject {
// skip
continue;
}
if self.area_detection_counter > 0 {
// Keep track of body information for delayed removal
space.add_removed_collider(
shape.collider_handle,
self.rid,
self.instance_id,
i,
self.collision_object_type,
);
}
// Keep track of body information for delayed removal
space.add_removed_collider(
shape.collider_handle,
self.rid,
self.instance_id,
i,
self.collision_object_type,
);
physics_engine.collider_destroy(self.space_handle, shape.collider_handle);
shape.collider_handle = ColliderHandle::invalid();
}
Expand All @@ -320,17 +316,15 @@ impl RapierCollisionObject {
physics_engine: &mut PhysicsEngine,
) -> ColliderHandle {
if shape.collider_handle != ColliderHandle::invalid() {
if self.area_detection_counter > 0 {
if let Some(space) = physics_spaces.get_mut(&self.space) {
// Keep track of body information for delayed removal
space.add_removed_collider(
shape.collider_handle,
self.rid,
self.instance_id,
p_shape_index,
self.get_type(),
);
}
if let Some(space) = physics_spaces.get_mut(&self.space) {
// Keep track of body information for delayed removal
space.add_removed_collider(
shape.collider_handle,
self.rid,
self.instance_id,
p_shape_index,
self.get_type(),
);
}
physics_engine.collider_destroy(self.space_handle, shape.collider_handle);
}
Expand Down Expand Up @@ -394,7 +388,6 @@ impl RapierCollisionObject {
}
self.destroy_shapes(physics_engine, physics_spaces);
// Reset area detection counter to keep it consistent for new detections
self.area_detection_counter = 0;
if let Some(space) = physics_spaces.get_mut(&self.space) {
space.reset_space_if_empty(physics_engine);
}
Expand Down
33 changes: 18 additions & 15 deletions src/bodies/rapier_collision_object_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,14 @@ impl RapierCollisionObject {
.add_owner(collision_object.get_base().get_rid());
}
if !shape.disabled {
collision_object.get_base().create_shape(
shape,
p_index,
collision_object.init_material(),
physics_engine,
physics_shapes,
);
collision_object.get_mut_base().shapes[p_index].collider_handle =
collision_object.get_base().create_shape(
shape,
p_index,
collision_object.init_material(),
physics_engine,
physics_shapes,
);
collision_object.get_base().update_shape_transform(
&shape,
physics_engine,
Expand Down Expand Up @@ -234,24 +235,26 @@ impl RapierCollisionObject {
if p_index >= collision_object.get_base().shapes.len() {
return;
}
collision_object.get_mut_base().shapes[p_index].disabled = p_disabled;
let shape = collision_object.get_base().shapes[p_index];
if shape.disabled == p_disabled {
return;
}
collision_object.get_mut_base().shapes[p_index].disabled = p_disabled;
let shape = collision_object.get_base().shapes[p_index];
if shape.disabled {
collision_object.get_mut_base().shapes[p_index].collider_handle = collision_object
.get_base()
.destroy_shape(shape, p_index, physics_spaces, physics_engine);
}
if !shape.disabled {
collision_object.get_base().create_shape(
shape,
p_index,
collision_object.init_material(),
physics_engine,
physics_shapes,
);
collision_object.get_mut_base().shapes[p_index].collider_handle =
collision_object.get_base().create_shape(
shape,
p_index,
collision_object.init_material(),
physics_engine,
physics_shapes,
);
collision_object.get_base().update_shape_transform(
&shape,
physics_engine,
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 @@ -42,6 +42,7 @@ pub struct ContactPointInfo {
feature = "serde-serialize",
derive(serde::Serialize, serde::Deserialize)
)]
#[derive(PartialEq, Eq, Clone, Copy, Debug, Default)]
pub struct CollisionEventInfo {
pub collider1: ColliderHandle,
pub collider2: ColliderHandle,
Expand Down
2 changes: 1 addition & 1 deletion src/rapier_wrapper/user_data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Copy, Clone, Eq, Hash, PartialEq, Default)]
#[derive(Copy, Clone, Eq, Hash, PartialEq, Default, Debug)]
#[cfg_attr(
feature = "serde-serialize",
derive(serde::Serialize, serde::Deserialize)
Expand Down
1 change: 1 addition & 0 deletions src/spaces/rapier_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct SpaceExport<'a> {
pub inner: &'a PhysicsObjects,
pub space: &'a RapierSpace,
}
#[derive(Debug, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "serde-serialize", derive(serde::Serialize))]
pub struct RemovedColliderInfo {
#[cfg_attr(feature = "serde-serialize", serde(skip))]
Expand Down
59 changes: 22 additions & 37 deletions src/spaces/rapier_space_callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,42 +124,27 @@ impl RapierSpace {
let (mut rid1, mut rid2) = (Rid::Invalid, Rid::Invalid);
let (mut instance_id1, mut instance_id2) = (0, 0);
let (mut type1, mut type2) = (CollisionObjectType::Area, CollisionObjectType::Area);
if event_info.is_removed {
if let Some(body) = physics_collision_objects.get(&p_object1) {
rid1 = body.get_base().get_rid();
instance_id1 = body.get_base().get_instance_id();
type1 = body.get_base().get_type();
} else if let Some(removed_collider_info_1) =
self.get_removed_collider_info(&collider_handle1)
{
rid1 = removed_collider_info_1.rid;
instance_id1 = removed_collider_info_1.instance_id;
type1 = removed_collider_info_1.collision_object_type;
shape1 = removed_collider_info_1.shape_index;
}
if let Some(body) = physics_collision_objects.get(&p_object2) {
rid2 = body.get_base().get_rid();
instance_id2 = body.get_base().get_instance_id();
type2 = body.get_base().get_type();
} else if let Some(removed_collider_info_2) =
self.get_removed_collider_info(&collider_handle2)
{
rid2 = removed_collider_info_2.rid;
instance_id2 = removed_collider_info_2.instance_id;
type2 = removed_collider_info_2.collision_object_type;
shape2 = removed_collider_info_2.shape_index;
}
} else {
if let Some(body1) = physics_collision_objects.get(&p_object1) {
rid1 = body1.get_base().get_rid();
instance_id1 = body1.get_base().get_instance_id();
type1 = body1.get_base().get_type();
}
if let Some(body2) = physics_collision_objects.get(&p_object2) {
rid2 = body2.get_base().get_rid();
instance_id2 = body2.get_base().get_instance_id();
type2 = body2.get_base().get_type();
}
if let Some(removed_collider_info_1) = self.get_removed_collider_info(&collider_handle1) {
rid1 = removed_collider_info_1.rid;
p_object1 = rid1;
instance_id1 = removed_collider_info_1.instance_id;
type1 = removed_collider_info_1.collision_object_type;
shape1 = removed_collider_info_1.shape_index;
} else if let Some(body) = physics_collision_objects.get(&p_object1) {
rid1 = body.get_base().get_rid();
instance_id1 = body.get_base().get_instance_id();
type1 = body.get_base().get_type();
}
if let Some(removed_collider_info_2) = self.get_removed_collider_info(&collider_handle2) {
rid2 = removed_collider_info_2.rid;
p_object2 = rid2;
instance_id2 = removed_collider_info_2.instance_id;
type2 = removed_collider_info_2.collision_object_type;
shape2 = removed_collider_info_2.shape_index;
} else if let Some(body) = physics_collision_objects.get(&p_object2) {
rid2 = body.get_base().get_rid();
instance_id2 = body.get_base().get_instance_id();
type2 = body.get_base().get_type();
}
if event_info.is_sensor {
if rid1.is_invalid() || rid2.is_invalid() {
Expand Down Expand Up @@ -249,7 +234,7 @@ impl RapierSpace {
}
}
// collision object 2 area
if let Some(p_collision_object2) = p_collision_object2 {
if let Some(ref mut p_collision_object2) = p_collision_object2 {
if let Some(p_area2) = p_collision_object2.get_mut_area() {
if type1 == CollisionObjectType::Area {
if event_info.is_started {
Expand Down

0 comments on commit b676e0d

Please sign in to comment.