Browse Source

Merge pull request #106693 from mihe/jolt/area-cleanup

Remove force enter/exit logic from `JoltArea3D`
Thaddeus Crews 3 months ago
parent
commit
dcde70f409

+ 5 - 96
modules/jolt_physics/objects/jolt_area_3d.cpp

@@ -197,82 +197,13 @@ void JoltArea3D::_notify_body_exited(const JPH::BodyID &p_body_id) {
 	}
 }
 
-void JoltArea3D::_force_bodies_entered() {
+void JoltArea3D::_remove_all_overlaps() {
 	for (KeyValue<JPH::BodyID, Overlap> &E : bodies_by_id) {
-		Overlap &body = E.value;
-
-		if (unlikely(body.shape_pairs.is_empty())) {
-			continue;
-		}
-
-		for (const KeyValue<ShapeIDPair, ShapeIndexPair> &P : body.shape_pairs) {
-			body.pending_removed.erase(P.value);
-			body.pending_added.push_back(P.value);
-		}
-
-		_events_changed();
-	}
-}
-
-void JoltArea3D::_force_bodies_exited(bool p_remove) {
-	for (KeyValue<JPH::BodyID, Overlap> &E : bodies_by_id) {
-		const JPH::BodyID &id = E.key;
-		Overlap &body = E.value;
-
-		if (unlikely(body.shape_pairs.is_empty())) {
-			continue;
-		}
-
-		for (const KeyValue<ShapeIDPair, ShapeIndexPair> &P : body.shape_pairs) {
-			body.pending_added.erase(P.value);
-			body.pending_removed.push_back(P.value);
-		}
-
-		_events_changed();
-
-		if (p_remove) {
-			body.shape_pairs.clear();
-			_notify_body_exited(id);
-		}
-	}
-}
-
-void JoltArea3D::_force_areas_entered() {
-	for (KeyValue<JPH::BodyID, Overlap> &E : areas_by_id) {
-		Overlap &area = E.value;
-
-		if (unlikely(area.shape_pairs.is_empty())) {
-			continue;
-		}
-
-		for (const KeyValue<ShapeIDPair, ShapeIndexPair> &P : area.shape_pairs) {
-			area.pending_removed.erase(P.value);
-			area.pending_added.push_back(P.value);
-		}
-
-		_events_changed();
+		_notify_body_exited(E.key);
 	}
-}
-
-void JoltArea3D::_force_areas_exited(bool p_remove) {
-	for (KeyValue<JPH::BodyID, Overlap> &E : areas_by_id) {
-		Overlap &area = E.value;
-
-		if (unlikely(area.shape_pairs.is_empty())) {
-			continue;
-		}
 
-		for (const KeyValue<ShapeIDPair, ShapeIndexPair> &P : area.shape_pairs) {
-			area.pending_added.erase(P.value);
-			area.pending_removed.push_back(P.value);
-		}
-
-		_events_changed();
-
-		if (p_remove) {
-			area.shape_pairs.clear();
-		}
-	}
+	bodies_by_id.clear();
+	areas_by_id.clear();
 }
 
 void JoltArea3D::_update_sleeping() {
@@ -304,15 +235,7 @@ void JoltArea3D::_update_default_gravity() {
 void JoltArea3D::_space_changing() {
 	JoltShapedObject3D::_space_changing();
 
-	if (space != nullptr) {
-		// Ideally we would rely on our contact listener to report all the exits when we move
-		// between (or out of) spaces, but because our Jolt body is going to be destroyed when we
-		// leave this space the contact listener won't be able to retrieve the corresponding area
-		// and as such cannot report any exits, so we're forced to do it manually instead.
-		_force_bodies_exited(true);
-		_force_areas_exited(true);
-	}
-
+	_remove_all_overlaps();
 	_dequeue_call_queries();
 }
 
@@ -328,22 +251,10 @@ void JoltArea3D::_events_changed() {
 }
 
 void JoltArea3D::_body_monitoring_changed() {
-	if (is_monitoring_bodies()) {
-		_force_bodies_entered();
-	} else {
-		_force_bodies_exited(false);
-	}
-
 	_update_sleeping();
 }
 
 void JoltArea3D::_area_monitoring_changed() {
-	if (is_monitoring_areas()) {
-		_force_areas_entered();
-	} else {
-		_force_areas_exited(false);
-	}
-
 	_update_sleeping();
 }
 
@@ -637,7 +548,6 @@ void JoltArea3D::body_shape_entered(const JPH::BodyID &p_body_id, const JPH::Sub
 
 bool JoltArea3D::body_shape_exited(const JPH::BodyID &p_body_id, const JPH::SubShapeID &p_other_shape_id, const JPH::SubShapeID &p_self_shape_id) {
 	Overlap *overlap = bodies_by_id.getptr(p_body_id);
-
 	if (overlap == nullptr) {
 		return false;
 	}
@@ -659,7 +569,6 @@ void JoltArea3D::area_shape_entered(const JPH::BodyID &p_body_id, const JPH::Sub
 
 bool JoltArea3D::area_shape_exited(const JPH::BodyID &p_body_id, const JPH::SubShapeID &p_other_shape_id, const JPH::SubShapeID &p_self_shape_id) {
 	Overlap *overlap = areas_by_id.getptr(p_body_id);
-
 	if (overlap == nullptr) {
 		return false;
 	}

+ 1 - 5
modules/jolt_physics/objects/jolt_area_3d.h

@@ -133,11 +133,7 @@ private:
 	void _notify_body_entered(const JPH::BodyID &p_body_id);
 	void _notify_body_exited(const JPH::BodyID &p_body_id);
 
-	void _force_bodies_entered();
-	void _force_bodies_exited(bool p_remove);
-
-	void _force_areas_entered();
-	void _force_areas_exited(bool p_remove);
+	void _remove_all_overlaps();
 
 	void _update_sleeping();
 	void _update_group_filter();