Store ObjectID instead of pointer for KinematicCollision owner

This commit is contained in:
Haoyu Qiu 2024-04-14 23:07:41 +08:00
parent d00734053f
commit 4176102a5e
12 changed files with 8 additions and 44 deletions

View file

@ -501,7 +501,7 @@ Ref<KinematicCollision2D> CharacterBody2D::_get_slide_collision(int p_bounce) {
// Create a new instance when the cached reference is invalid or still in use in script.
if (slide_colliders[p_bounce].is_null() || slide_colliders[p_bounce]->get_reference_count() > 1) {
slide_colliders.write[p_bounce].instantiate();
slide_colliders.write[p_bounce]->owner = this;
slide_colliders.write[p_bounce]->owner_id = get_instance_id();
}
slide_colliders.write[p_bounce]->result = motion_results[p_bounce];
@ -745,11 +745,3 @@ void CharacterBody2D::_bind_methods() {
CharacterBody2D::CharacterBody2D() :
PhysicsBody2D(PhysicsServer2D::BODY_MODE_KINEMATIC) {
}
CharacterBody2D::~CharacterBody2D() {
for (int i = 0; i < slide_colliders.size(); i++) {
if (slide_colliders[i].is_valid()) {
slide_colliders.write[i]->owner = nullptr;
}
}
}

View file

@ -111,7 +111,6 @@ public:
PlatformOnLeave get_platform_on_leave() const;
CharacterBody2D();
~CharacterBody2D();
private:
real_t margin = 0.08;

View file

@ -59,6 +59,7 @@ real_t KinematicCollision2D::get_depth() const {
}
Object *KinematicCollision2D::get_local_shape() const {
PhysicsBody2D *owner = Object::cast_to<PhysicsBody2D>(ObjectDB::get_instance(owner_id));
if (!owner) {
return nullptr;
}

View file

@ -40,7 +40,7 @@ class PhysicsBody2D;
class KinematicCollision2D : public RefCounted {
GDCLASS(KinematicCollision2D, RefCounted);
PhysicsBody2D *owner = nullptr;
ObjectID owner_id;
friend class PhysicsBody2D;
friend class CharacterBody2D;
PhysicsServer2D::MotionResult result;

View file

@ -30,8 +30,6 @@
#include "physics_body_2d.h"
#include "scene/scene_string_names.h"
void PhysicsBody2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("move_and_collide", "motion", "test_only", "safe_margin", "recovery_as_collision"), &PhysicsBody2D::_move, DEFVAL(false), DEFVAL(0.08), DEFVAL(false));
ClassDB::bind_method(D_METHOD("test_move", "from", "motion", "collision", "safe_margin", "recovery_as_collision"), &PhysicsBody2D::test_move, DEFVAL(Variant()), DEFVAL(0.08), DEFVAL(false));
@ -48,12 +46,6 @@ PhysicsBody2D::PhysicsBody2D(PhysicsServer2D::BodyMode p_mode) :
set_pickable(false);
}
PhysicsBody2D::~PhysicsBody2D() {
if (motion_cache.is_valid()) {
motion_cache->owner = nullptr;
}
}
Ref<KinematicCollision2D> PhysicsBody2D::_move(const Vector2 &p_motion, bool p_test_only, real_t p_margin, bool p_recovery_as_collision) {
PhysicsServer2D::MotionParameters parameters(get_global_transform(), p_motion, p_margin);
parameters.recovery_as_collision = p_recovery_as_collision;
@ -64,7 +56,7 @@ Ref<KinematicCollision2D> PhysicsBody2D::_move(const Vector2 &p_motion, bool p_t
// Create a new instance when the cached reference is invalid or still in use in script.
if (motion_cache.is_null() || motion_cache->get_reference_count() > 1) {
motion_cache.instantiate();
motion_cache->owner = this;
motion_cache->owner_id = get_instance_id();
}
motion_cache->result = result;

View file

@ -56,8 +56,6 @@ public:
TypedArray<PhysicsBody2D> get_collision_exceptions();
void add_collision_exception_with(Node *p_node); //must be physicsbody
void remove_collision_exception_with(Node *p_node);
virtual ~PhysicsBody2D();
};
#endif // PHYSICS_BODY_2D_H

View file

@ -704,7 +704,7 @@ Ref<KinematicCollision3D> CharacterBody3D::_get_slide_collision(int p_bounce) {
// Create a new instance when the cached reference is invalid or still in use in script.
if (slide_colliders[p_bounce].is_null() || slide_colliders[p_bounce]->get_reference_count() > 1) {
slide_colliders.write[p_bounce].instantiate();
slide_colliders.write[p_bounce]->owner = this;
slide_colliders.write[p_bounce]->owner_id = get_instance_id();
}
slide_colliders.write[p_bounce]->result = motion_results[p_bounce];
@ -936,11 +936,3 @@ void CharacterBody3D::_validate_property(PropertyInfo &p_property) const {
CharacterBody3D::CharacterBody3D() :
PhysicsBody3D(PhysicsServer3D::BODY_MODE_KINEMATIC) {
}
CharacterBody3D::~CharacterBody3D() {
for (int i = 0; i < slide_colliders.size(); i++) {
if (slide_colliders[i].is_valid()) {
slide_colliders.write[i]->owner = nullptr;
}
}
}

View file

@ -113,7 +113,6 @@ public:
PlatformOnLeave get_platform_on_leave() const;
CharacterBody3D();
~CharacterBody3D();
private:
real_t margin = 0.001;

View file

@ -67,6 +67,7 @@ real_t KinematicCollision3D::get_angle(int p_collision_index, const Vector3 &p_u
Object *KinematicCollision3D::get_local_shape(int p_collision_index) const {
ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, nullptr);
PhysicsBody3D *owner = Object::cast_to<PhysicsBody3D>(ObjectDB::get_instance(owner_id));
if (!owner) {
return nullptr;
}

View file

@ -40,7 +40,7 @@ class PhysicsBody3D;
class KinematicCollision3D : public RefCounted {
GDCLASS(KinematicCollision3D, RefCounted);
PhysicsBody3D *owner = nullptr;
ObjectID owner_id;
friend class PhysicsBody3D;
friend class CharacterBody3D;
PhysicsServer3D::MotionResult result;

View file

@ -30,8 +30,6 @@
#include "physics_body_3d.h"
#include "scene/scene_string_names.h"
void PhysicsBody3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("move_and_collide", "motion", "test_only", "safe_margin", "recovery_as_collision", "max_collisions"), &PhysicsBody3D::_move, DEFVAL(false), DEFVAL(0.001), DEFVAL(false), DEFVAL(1));
ClassDB::bind_method(D_METHOD("test_move", "from", "motion", "collision", "safe_margin", "recovery_as_collision", "max_collisions"), &PhysicsBody3D::test_move, DEFVAL(Variant()), DEFVAL(0.001), DEFVAL(false), DEFVAL(1));
@ -58,12 +56,6 @@ PhysicsBody3D::PhysicsBody3D(PhysicsServer3D::BodyMode p_mode) :
set_body_mode(p_mode);
}
PhysicsBody3D::~PhysicsBody3D() {
if (motion_cache.is_valid()) {
motion_cache->owner = nullptr;
}
}
TypedArray<PhysicsBody3D> PhysicsBody3D::get_collision_exceptions() {
List<RID> exceptions;
PhysicsServer3D::get_singleton()->body_get_collision_exceptions(get_rid(), &exceptions);
@ -102,7 +94,7 @@ Ref<KinematicCollision3D> PhysicsBody3D::_move(const Vector3 &p_motion, bool p_t
// Create a new instance when the cached reference is invalid or still in use in script.
if (motion_cache.is_null() || motion_cache->get_reference_count() > 1) {
motion_cache.instantiate();
motion_cache->owner = this;
motion_cache->owner_id = get_instance_id();
}
motion_cache->result = result;

View file

@ -65,8 +65,6 @@ public:
TypedArray<PhysicsBody3D> get_collision_exceptions();
void add_collision_exception_with(Node *p_node); //must be physicsbody
void remove_collision_exception_with(Node *p_node);
virtual ~PhysicsBody3D();
};
#endif // PHYSICS_BODY_3D_H