1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-30 16:26:50 +00:00

Add signal to inform joint that body has exited tree

This commit is contained in:
Marcel Admiraal
2020-12-26 14:23:30 +00:00
parent 41e9028868
commit 026aa4381d
5 changed files with 96 additions and 5 deletions

View File

@@ -482,7 +482,7 @@ void SpaceBullet::remove_rigid_body(RigidBodyBullet *p_body) {
int constraints = btBody->getNumConstraintRefs(); int constraints = btBody->getNumConstraintRefs();
if (constraints > 0) { if (constraints > 0) {
WARN_PRINT("A body connected to joints was removed. Ensure bodies are disconnected from joints before removing them."); ERR_PRINT("A body connected to joints was removed.");
for (int i = 0; i < constraints; i++) { for (int i = 0; i < constraints; i++) {
dynamicsWorld->removeConstraint(btBody->getConstraintRef(i)); dynamicsWorld->removeConstraint(btBody->getConstraintRef(i));
} }

View File

@@ -32,8 +32,38 @@
#include "core/config/engine.h" #include "core/config/engine.h"
#include "physics_body_2d.h" #include "physics_body_2d.h"
#include "scene/scene_string_names.h"
#include "servers/physics_server_2d.h" #include "servers/physics_server_2d.h"
void Joint2D::_disconnect_signals() {
Node *node_a = get_node_or_null(a);
PhysicsBody2D *body_a = Object::cast_to<PhysicsBody2D>(node_a);
if (body_a) {
body_a->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Joint2D::_body_exit_tree));
}
Node *node_b = get_node_or_null(b);
PhysicsBody2D *body_b = Object::cast_to<PhysicsBody2D>(node_b);
if (body_b) {
body_b->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Joint2D::_body_exit_tree));
}
}
void Joint2D::_body_exit_tree(const ObjectID &p_body_id) {
_disconnect_signals();
Object *object = ObjectDB::get_instance(p_body_id);
PhysicsBody2D *body = Object::cast_to<PhysicsBody2D>(object);
ERR_FAIL_NULL(body);
RID body_rid = body->get_rid();
if (ba == body_rid) {
a = NodePath();
}
if (bb == body_rid) {
b = NodePath();
}
_update_joint();
}
void Joint2D::_update_joint(bool p_only_free) { void Joint2D::_update_joint(bool p_only_free) {
if (joint.is_valid()) { if (joint.is_valid()) {
if (ba.is_valid() && bb.is_valid() && exclude_from_collision) { if (ba.is_valid() && bb.is_valid() && exclude_from_collision) {
@@ -51,8 +81,8 @@ void Joint2D::_update_joint(bool p_only_free) {
return; return;
} }
Node *node_a = has_node(get_node_a()) ? get_node(get_node_a()) : (Node *)nullptr; Node *node_a = get_node_or_null(a);
Node *node_b = has_node(get_node_b()) ? get_node(get_node_b()) : (Node *)nullptr; Node *node_b = get_node_or_null(b);
PhysicsBody2D *body_a = Object::cast_to<PhysicsBody2D>(node_a); PhysicsBody2D *body_a = Object::cast_to<PhysicsBody2D>(node_a);
PhysicsBody2D *body_b = Object::cast_to<PhysicsBody2D>(node_b); PhysicsBody2D *body_b = Object::cast_to<PhysicsBody2D>(node_b);
@@ -107,6 +137,9 @@ void Joint2D::_update_joint(bool p_only_free) {
ba = body_a->get_rid(); ba = body_a->get_rid();
bb = body_b->get_rid(); bb = body_b->get_rid();
body_a->connect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Joint2D::_body_exit_tree), make_binds(body_a->get_instance_id()));
body_b->connect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Joint2D::_body_exit_tree), make_binds(body_b->get_instance_id()));
PhysicsServer2D::get_singleton()->joint_disable_collisions_between_bodies(joint, exclude_from_collision); PhysicsServer2D::get_singleton()->joint_disable_collisions_between_bodies(joint, exclude_from_collision);
} }
@@ -115,6 +148,10 @@ void Joint2D::set_node_a(const NodePath &p_node_a) {
return; return;
} }
if (joint.is_valid()) {
_disconnect_signals();
}
a = p_node_a; a = p_node_a;
_update_joint(); _update_joint();
} }
@@ -127,6 +164,11 @@ void Joint2D::set_node_b(const NodePath &p_node_b) {
if (b == p_node_b) { if (b == p_node_b) {
return; return;
} }
if (joint.is_valid()) {
_disconnect_signals();
}
b = p_node_b; b = p_node_b;
_update_joint(); _update_joint();
} }
@@ -142,6 +184,7 @@ void Joint2D::_notification(int p_what) {
} break; } break;
case NOTIFICATION_EXIT_TREE: { case NOTIFICATION_EXIT_TREE: {
if (joint.is_valid()) { if (joint.is_valid()) {
_disconnect_signals();
_update_joint(true); _update_joint(true);
} }
} break; } break;

View File

@@ -49,6 +49,8 @@ class Joint2D : public Node2D {
String warning; String warning;
protected: protected:
void _disconnect_signals();
void _body_exit_tree(const ObjectID &p_body_id);
void _update_joint(bool p_only_free = false); void _update_joint(bool p_only_free = false);
void _notification(int p_what); void _notification(int p_what);

View File

@@ -30,6 +30,37 @@
#include "physics_joint_3d.h" #include "physics_joint_3d.h"
#include "scene/scene_string_names.h"
void Joint3D::_disconnect_signals() {
Node *node_a = get_node_or_null(a);
PhysicsBody3D *body_a = Object::cast_to<PhysicsBody3D>(node_a);
if (body_a) {
body_a->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Joint3D::_body_exit_tree));
}
Node *node_b = get_node_or_null(b);
PhysicsBody3D *body_b = Object::cast_to<PhysicsBody3D>(node_b);
if (body_b) {
body_b->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Joint3D::_body_exit_tree));
}
}
void Joint3D::_body_exit_tree(const ObjectID &p_body_id) {
_disconnect_signals();
Object *object = ObjectDB::get_instance(p_body_id);
PhysicsBody3D *body = Object::cast_to<PhysicsBody3D>(object);
ERR_FAIL_NULL(body);
RID body_rid = body->get_rid();
if (ba == body_rid) {
a = NodePath();
}
if (bb == body_rid) {
b = NodePath();
}
_update_joint();
}
void Joint3D::_update_joint(bool p_only_free) { void Joint3D::_update_joint(bool p_only_free) {
if (joint.is_valid()) { if (joint.is_valid()) {
if (ba.is_valid() && bb.is_valid()) { if (ba.is_valid() && bb.is_valid()) {
@@ -47,8 +78,8 @@ void Joint3D::_update_joint(bool p_only_free) {
return; return;
} }
Node *node_a = has_node(get_node_a()) ? get_node(get_node_a()) : (Node *)nullptr; Node *node_a = get_node_or_null(a);
Node *node_b = has_node(get_node_b()) ? get_node(get_node_b()) : (Node *)nullptr; Node *node_b = get_node_or_null(b);
PhysicsBody3D *body_a = Object::cast_to<PhysicsBody3D>(node_a); PhysicsBody3D *body_a = Object::cast_to<PhysicsBody3D>(node_a);
PhysicsBody3D *body_b = Object::cast_to<PhysicsBody3D>(node_b); PhysicsBody3D *body_b = Object::cast_to<PhysicsBody3D>(node_b);
@@ -97,8 +128,11 @@ void Joint3D::_update_joint(bool p_only_free) {
PhysicsServer3D::get_singleton()->joint_set_solver_priority(joint, solver_priority); PhysicsServer3D::get_singleton()->joint_set_solver_priority(joint, solver_priority);
ba = body_a->get_rid(); ba = body_a->get_rid();
body_a->connect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Joint3D::_body_exit_tree), make_binds(body_a->get_instance_id()));
if (body_b) { if (body_b) {
bb = body_b->get_rid(); bb = body_b->get_rid();
body_b->connect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Joint3D::_body_exit_tree), make_binds(body_b->get_instance_id()));
} }
PhysicsServer3D::get_singleton()->joint_disable_collisions_between_bodies(joint, exclude_from_collision); PhysicsServer3D::get_singleton()->joint_disable_collisions_between_bodies(joint, exclude_from_collision);
@@ -109,6 +143,10 @@ void Joint3D::set_node_a(const NodePath &p_node_a) {
return; return;
} }
if (joint.is_valid()) {
_disconnect_signals();
}
a = p_node_a; a = p_node_a;
_update_joint(); _update_joint();
} }
@@ -121,6 +159,11 @@ void Joint3D::set_node_b(const NodePath &p_node_b) {
if (b == p_node_b) { if (b == p_node_b) {
return; return;
} }
if (joint.is_valid()) {
_disconnect_signals();
}
b = p_node_b; b = p_node_b;
_update_joint(); _update_joint();
} }
@@ -147,6 +190,7 @@ void Joint3D::_notification(int p_what) {
} break; } break;
case NOTIFICATION_EXIT_TREE: { case NOTIFICATION_EXIT_TREE: {
if (joint.is_valid()) { if (joint.is_valid()) {
_disconnect_signals();
_update_joint(true); _update_joint(true);
} }
} break; } break;

View File

@@ -49,6 +49,8 @@ class Joint3D : public Node3D {
String warning; String warning;
protected: protected:
void _disconnect_signals();
void _body_exit_tree(const ObjectID &p_body_id);
void _update_joint(bool p_only_free = false); void _update_joint(bool p_only_free = false);
void _notification(int p_what); void _notification(int p_what);