Merge pull request #43865 from madmiraal/fix-43852

Check joint nodes and generate configuration warning messages.
This commit is contained in:
Rémi Verschelde 2020-11-26 10:21:36 +01:00 committed by GitHub
commit 27933784f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 102 additions and 18 deletions

View File

@ -47,28 +47,52 @@ void Joint2D::_update_joint(bool p_only_free) {
}
if (p_only_free || !is_inside_tree()) {
warning = String();
return;
}
Node *node_a = has_node(get_node_a()) ? get_node(get_node_a()) : (Node *)nullptr;
Node *node_b = has_node(get_node_b()) ? get_node(get_node_b()) : (Node *)nullptr;
if (!node_a || !node_b) {
return;
}
PhysicsBody2D *body_a = Object::cast_to<PhysicsBody2D>(node_a);
PhysicsBody2D *body_b = Object::cast_to<PhysicsBody2D>(node_b);
if (!body_a || !body_b) {
if (node_a && !body_a && node_b && !body_b) {
warning = TTR("Node A and Node B must be PhysicsBody2Ds");
update_configuration_warning();
return;
}
if (node_a && !body_a) {
warning = TTR("Node A must be a PhysicsBody2D");
update_configuration_warning();
return;
}
if (node_b && !body_b) {
warning = TTR("Node B must be a PhysicsBody2D");
update_configuration_warning();
return;
}
if (!body_a || !body_b) {
warning = TTR("Joint is not connected to two PhysicsBody2Ds");
update_configuration_warning();
return;
}
if (body_a == body_b) {
warning = TTR("Node A and Node B must be different PhysicsBody2Ds");
update_configuration_warning();
return;
}
warning = String();
update_configuration_warning();
joint = _configure_joint(body_a, body_b);
if (!joint.is_valid()) {
return;
}
ERR_FAIL_COND_MSG(!joint.is_valid(), "Failed to configure the joint.");
PhysicsServer2D::get_singleton()->get_singleton()->joint_set_param(joint, PhysicsServer2D::JOINT_PARAM_BIAS, bias);
@ -141,6 +165,19 @@ bool Joint2D::get_exclude_nodes_from_collision() const {
return exclude_from_collision;
}
String Joint2D::get_configuration_warning() const {
String node_warning = Node2D::get_configuration_warning();
if (!warning.empty()) {
if (!node_warning.empty()) {
node_warning += "\n\n";
}
node_warning += warning;
}
return node_warning;
}
void Joint2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_node_a", "node"), &Joint2D::set_node_a);
ClassDB::bind_method(D_METHOD("get_node_a"), &Joint2D::get_node_a);
@ -154,8 +191,8 @@ void Joint2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_exclude_nodes_from_collision", "enable"), &Joint2D::set_exclude_nodes_from_collision);
ClassDB::bind_method(D_METHOD("get_exclude_nodes_from_collision"), &Joint2D::get_exclude_nodes_from_collision);
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_a", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "CollisionObject2D"), "set_node_a", "get_node_a");
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_b", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "CollisionObject2D"), "set_node_b", "get_node_b");
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_a", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody2D"), "set_node_a", "get_node_a");
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_b", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody2D"), "set_node_b", "get_node_b");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bias", PROPERTY_HINT_RANGE, "0,0.9,0.001"), "set_bias", "get_bias");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_collision"), "set_exclude_nodes_from_collision", "get_exclude_nodes_from_collision");
}

View File

@ -46,6 +46,7 @@ class Joint2D : public Node2D {
real_t bias;
bool exclude_from_collision;
String warning;
protected:
void _update_joint(bool p_only_free = false);
@ -56,6 +57,8 @@ protected:
static void _bind_methods();
public:
virtual String get_configuration_warning() const override;
void set_node_a(const NodePath &p_node_a);
NodePath get_node_a() const;

View File

@ -43,6 +43,7 @@ void Joint3D::_update_joint(bool p_only_free) {
}
if (p_only_free || !is_inside_tree()) {
warning = String();
return;
}
@ -52,19 +53,46 @@ void Joint3D::_update_joint(bool p_only_free) {
PhysicsBody3D *body_a = Object::cast_to<PhysicsBody3D>(node_a);
PhysicsBody3D *body_b = Object::cast_to<PhysicsBody3D>(node_b);
if (!body_a && body_b) {
SWAP(body_a, body_b);
if (node_a && !body_a && node_b && !body_b) {
warning = TTR("Node A and Node B must be PhysicsBody3Ds");
update_configuration_warning();
return;
}
if (node_a && !body_a) {
warning = TTR("Node A must be a PhysicsBody3D");
update_configuration_warning();
return;
}
if (node_b && !body_b) {
warning = TTR("Node B must be a PhysicsBody3D");
update_configuration_warning();
return;
}
if (!body_a && !body_b) {
warning = TTR("Joint is not connected to any PhysicsBody3Ds");
update_configuration_warning();
return;
}
if (body_a == body_b) {
warning = TTR("Node A and Node B must be different PhysicsBody3Ds");
update_configuration_warning();
return;
}
if (!body_a) {
return;
SWAP(body_a, body_b);
}
warning = String();
update_configuration_warning();
joint = _configure_joint(body_a, body_b);
if (!joint.is_valid()) {
return;
}
ERR_FAIL_COND_MSG(!joint.is_valid(), "Failed to configure the joint.");
PhysicsServer3D::get_singleton()->joint_set_solver_priority(joint, solver_priority);
@ -137,6 +165,19 @@ bool Joint3D::get_exclude_nodes_from_collision() const {
return exclude_from_collision;
}
String Joint3D::get_configuration_warning() const {
String node_warning = Node3D::get_configuration_warning();
if (!warning.empty()) {
if (!node_warning.empty()) {
node_warning += "\n\n";
}
node_warning += warning;
}
return node_warning;
}
void Joint3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_node_a", "node"), &Joint3D::set_node_a);
ClassDB::bind_method(D_METHOD("get_node_a"), &Joint3D::get_node_a);
@ -150,8 +191,8 @@ void Joint3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_exclude_nodes_from_collision", "enable"), &Joint3D::set_exclude_nodes_from_collision);
ClassDB::bind_method(D_METHOD("get_exclude_nodes_from_collision"), &Joint3D::get_exclude_nodes_from_collision);
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "nodes/node_a", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "CollisionObject3D"), "set_node_a", "get_node_a");
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "nodes/node_b", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "CollisionObject3D"), "set_node_b", "get_node_b");
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "nodes/node_a", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody3D"), "set_node_a", "get_node_a");
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "nodes/node_b", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody3D"), "set_node_b", "get_node_b");
ADD_PROPERTY(PropertyInfo(Variant::INT, "solver/priority", PROPERTY_HINT_RANGE, "1,8,1"), "set_solver_priority", "get_solver_priority");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collision/exclude_nodes"), "set_exclude_nodes_from_collision", "get_exclude_nodes_from_collision");

View File

@ -46,6 +46,7 @@ class Joint3D : public Node3D {
int solver_priority;
bool exclude_from_collision;
String warning;
protected:
void _update_joint(bool p_only_free = false);
@ -57,6 +58,8 @@ protected:
static void _bind_methods();
public:
virtual String get_configuration_warning() const override;
void set_node_a(const NodePath &p_node_a);
NodePath get_node_a() const;