Skip to content

Commit

Permalink
Store ObjectID instead of pointer for KinematicCollision owner
Browse files Browse the repository at this point in the history
  • Loading branch information
timothyqiu committed Apr 15, 2024
1 parent d007340 commit 4176102
Show file tree
Hide file tree
Showing 12 changed files with 8 additions and 44 deletions.
10 changes: 1 addition & 9 deletions scene/2d/physics/character_body_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
}
}
}
1 change: 0 additions & 1 deletion scene/2d/physics/character_body_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ class CharacterBody2D : public PhysicsBody2D {
PlatformOnLeave get_platform_on_leave() const;

CharacterBody2D();
~CharacterBody2D();

private:
real_t margin = 0.08;
Expand Down
1 change: 1 addition & 0 deletions scene/2d/physics/kinematic_collision_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion scene/2d/physics/kinematic_collision_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 1 addition & 9 deletions scene/2d/physics/physics_body_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions scene/2d/physics/physics_body_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ class PhysicsBody2D : public CollisionObject2D {
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
10 changes: 1 addition & 9 deletions scene/3d/physics/character_body_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
}
}
}
1 change: 0 additions & 1 deletion scene/3d/physics/character_body_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ class CharacterBody3D : public PhysicsBody3D {
PlatformOnLeave get_platform_on_leave() const;

CharacterBody3D();
~CharacterBody3D();

private:
real_t margin = 0.001;
Expand Down
1 change: 1 addition & 0 deletions scene/3d/physics/kinematic_collision_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion scene/3d/physics/kinematic_collision_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 1 addition & 9 deletions scene/3d/physics/physics_body_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions scene/3d/physics/physics_body_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ class PhysicsBody3D : public CollisionObject3D {
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

0 comments on commit 4176102

Please sign in to comment.