From fbc7f1d478fd494cfd66b015118c760e602ff484 Mon Sep 17 00:00:00 2001 From: Daylily-Zeleen Date: Fri, 3 Jan 2025 16:20:06 +0800 Subject: [PATCH] Remove invalid collision exceptions in PhysicsServerXD::body_get_collision_exceptions() --- .../godot_physics_server_2d.cpp | 15 +++++++++++++-- .../godot_physics_server_3d.cpp | 15 +++++++++++++-- modules/jolt_physics/jolt_physics_server_3d.cpp | 17 ++++++++++++++--- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/modules/godot_physics_2d/godot_physics_server_2d.cpp b/modules/godot_physics_2d/godot_physics_server_2d.cpp index 47df6a25267..58079942b51 100644 --- a/modules/godot_physics_2d/godot_physics_server_2d.cpp +++ b/modules/godot_physics_2d/godot_physics_server_2d.cpp @@ -924,8 +924,19 @@ void GodotPhysicsServer2D::body_get_collision_exceptions(RID p_body, List * GodotBody2D *body = body_owner.get_or_null(p_body); ERR_FAIL_NULL(body); - for (int i = 0; i < body->get_exceptions().size(); i++) { - p_exceptions->push_back(body->get_exceptions()[i]); + const VSet &exceptions = body->get_exceptions(); + LocalVector invalid_exceptions; + for (int i = 0; i < exceptions.size(); i++) { + RID exception = exceptions[i]; + if (body_owner.owns(exception)) { + p_exceptions->push_back(exception); + } else { + invalid_exceptions.push_back(exception); + } + } + + for (RID invalid_body : invalid_exceptions) { + body->remove_exception(invalid_body); } } diff --git a/modules/godot_physics_3d/godot_physics_server_3d.cpp b/modules/godot_physics_3d/godot_physics_server_3d.cpp index 35aa538ef51..698814c76f1 100644 --- a/modules/godot_physics_3d/godot_physics_server_3d.cpp +++ b/modules/godot_physics_3d/godot_physics_server_3d.cpp @@ -850,8 +850,19 @@ void GodotPhysicsServer3D::body_get_collision_exceptions(RID p_body, List * GodotBody3D *body = body_owner.get_or_null(p_body); ERR_FAIL_NULL(body); - for (int i = 0; i < body->get_exceptions().size(); i++) { - p_exceptions->push_back(body->get_exceptions()[i]); + const VSet &exceptions = body->get_exceptions(); + LocalVector invalid_exceptions; + for (int i = 0; i < exceptions.size(); i++) { + RID exception = exceptions[i]; + if (body_owner.owns(exception)) { + p_exceptions->push_back(exception); + } else { + invalid_exceptions.push_back(exception); + } + } + + for (RID invalid_body : invalid_exceptions) { + body->remove_exception(invalid_body); } } diff --git a/modules/jolt_physics/jolt_physics_server_3d.cpp b/modules/jolt_physics/jolt_physics_server_3d.cpp index d126f8bde7b..68eefed1643 100644 --- a/modules/jolt_physics/jolt_physics_server_3d.cpp +++ b/modules/jolt_physics/jolt_physics_server_3d.cpp @@ -862,11 +862,22 @@ void JoltPhysicsServer3D::body_remove_collision_exception(RID p_body, RID p_exce } void JoltPhysicsServer3D::body_get_collision_exceptions(RID p_body, List *p_exceptions) { - const JoltBody3D *body = body_owner.get_or_null(p_body); + JoltBody3D *body = body_owner.get_or_null(p_body); ERR_FAIL_NULL(body); - for (const RID &exception : body->get_collision_exceptions()) { - p_exceptions->push_back(exception); + const LocalVector &exceptions = body->get_collision_exceptions(); + LocalVector invalid_exceptions; + for (uint32_t i = 0; i < exceptions.size(); i++) { + RID exception = exceptions[i]; + if (body_owner.owns(exception)) { + p_exceptions->push_back(exception); + } else { + invalid_exceptions.push_back(exception); + } + } + + for (RID invalid_body : invalid_exceptions) { + body->remove_collision_exception(invalid_body); } }