From 0b1266b8126cf4204637340d01e2a41dd4948f77 Mon Sep 17 00:00:00 2001 From: lawnjelly Date: Wed, 28 Feb 2024 08:24:32 +0000 Subject: [PATCH] Fix physics platform crash Physics body previously stored the RID of a collision object and accessed it on the next frame, leading to a crash if the object had been deleted. This PR checks the object still exists via the ObjectID prior to access. --- scene/3d/physics/character_body_3d.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/scene/3d/physics/character_body_3d.cpp b/scene/3d/physics/character_body_3d.cpp index b71b9519a96..6759033358f 100644 --- a/scene/3d/physics/character_body_3d.cpp +++ b/scene/3d/physics/character_body_3d.cpp @@ -56,8 +56,15 @@ bool CharacterBody3D::move_and_slide() { excluded = (platform_wall_layers & platform_layer) == 0; } if (!excluded) { - //this approach makes sure there is less delay between the actual body velocity and the one we saved - PhysicsDirectBodyState3D *bs = PhysicsServer3D::get_singleton()->body_get_direct_state(platform_rid); + PhysicsDirectBodyState3D *bs = nullptr; + + // We need to check the platform_rid object still exists before accessing. + // A valid RID is no guarantee that the object has not been deleted. + if (ObjectDB::get_instance(platform_object_id)) { + //this approach makes sure there is less delay between the actual body velocity and the one we saved + bs = PhysicsServer3D::get_singleton()->body_get_direct_state(platform_rid); + } + if (bs) { Vector3 local_position = gt.origin - bs->get_transform().origin; current_platform_velocity = bs->get_velocity_at_local_position(local_position);