| 1 | /**************************************************************************/ |
| 2 | /* joint_2d.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "joint_2d.h" |
| 32 | |
| 33 | #include "scene/2d/physics/physics_body_2d.h" |
| 34 | |
| 35 | void Joint2D::_disconnect_signals() { |
| 36 | Node *node_a = get_node_or_null(a); |
| 37 | PhysicsBody2D *body_a = Object::cast_to<PhysicsBody2D>(node_a); |
| 38 | if (body_a) { |
| 39 | body_a->disconnect(SceneStringName(tree_exiting), callable_mp(this, &Joint2D::_body_exit_tree)); |
| 40 | } |
| 41 | |
| 42 | Node *node_b = get_node_or_null(b); |
| 43 | PhysicsBody2D *body_b = Object::cast_to<PhysicsBody2D>(node_b); |
| 44 | if (body_b) { |
| 45 | body_b->disconnect(SceneStringName(tree_exiting), callable_mp(this, &Joint2D::_body_exit_tree)); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void Joint2D::_body_exit_tree() { |
| 50 | _disconnect_signals(); |
| 51 | _update_joint(true); |
| 52 | update_configuration_warnings(); |
| 53 | } |
| 54 | |
| 55 | void Joint2D::_update_joint(bool p_only_free) { |
| 56 | if (ba.is_valid() && bb.is_valid() && exclude_from_collision) { |
| 57 | PhysicsServer2D::get_singleton()->joint_disable_collisions_between_bodies(joint, false); |
| 58 | } |
| 59 | |
| 60 | ba = RID(); |
| 61 | bb = RID(); |
| 62 | configured = false; |
| 63 | |
| 64 | if (p_only_free || !is_inside_tree()) { |
| 65 | PhysicsServer2D::get_singleton()->joint_clear(joint); |
| 66 | warning = String(); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | Node *node_a = get_node_or_null(a); |
| 71 | Node *node_b = get_node_or_null(b); |
| 72 | |
| 73 | PhysicsBody2D *body_a = Object::cast_to<PhysicsBody2D>(node_a); |
| 74 | PhysicsBody2D *body_b = Object::cast_to<PhysicsBody2D>(node_b); |
| 75 | |
| 76 | bool valid = false; |
| 77 | |
| 78 | if (node_a && !body_a && node_b && !body_b) { |
| 79 | warning = RTR("Node A and Node B must be PhysicsBody2Ds"); |
| 80 | } else if (node_a && !body_a) { |
| 81 | warning = RTR("Node A must be a PhysicsBody2D"); |
| 82 | } else if (node_b && !body_b) { |
| 83 | warning = RTR("Node B must be a PhysicsBody2D"); |
| 84 | } else if (!body_a || !body_b) { |
| 85 | warning = RTR("Joint is not connected to two PhysicsBody2Ds"); |
| 86 | } else if (body_a == body_b) { |
| 87 | warning = RTR("Node A and Node B must be different PhysicsBody2Ds"); |
| 88 | } else { |
| 89 | warning = String(); |
| 90 | valid = true; |
| 91 | } |
| 92 | |
| 93 | update_configuration_warnings(); |
| 94 | |
| 95 | if (!valid) { |
| 96 | PhysicsServer2D::get_singleton()->joint_clear(joint); |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | if (body_a) { |
| 101 | body_a->force_update_transform(); |
| 102 | } |
| 103 | |
| 104 | if (body_b) { |
| 105 | body_b->force_update_transform(); |
| 106 | } |
| 107 | |
| 108 | configured = true; |
| 109 | |
| 110 | _configure_joint(joint, body_a, body_b); |
| 111 | |
| 112 | ERR_FAIL_COND_MSG(!joint.is_valid(), "Failed to configure the joint."); |
| 113 | |
| 114 | PhysicsServer2D::get_singleton()->joint_set_param(joint, PhysicsServer2D::JOINT_PARAM_BIAS, bias); |
| 115 | |
| 116 | ba = body_a->get_rid(); |
| 117 | bb = body_b->get_rid(); |
| 118 | |
| 119 | if (!body_a->is_connected(SceneStringName(tree_exiting), callable_mp(this, &Joint2D::_body_exit_tree))) { |
| 120 | body_a->connect(SceneStringName(tree_exiting), callable_mp(this, &Joint2D::_body_exit_tree)); |
| 121 | } |
| 122 | if (!body_b->is_connected(SceneStringName(tree_exiting), callable_mp(this, &Joint2D::_body_exit_tree))) { |
| 123 | body_b->connect(SceneStringName(tree_exiting), callable_mp(this, &Joint2D::_body_exit_tree)); |
| 124 | } |
| 125 | |
| 126 | PhysicsServer2D::get_singleton()->joint_disable_collisions_between_bodies(joint, exclude_from_collision); |
| 127 | } |
| 128 | |
| 129 | void Joint2D::set_node_a(const NodePath &p_node_a) { |
| 130 | if (a == p_node_a) { |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | if (is_configured()) { |
| 135 | _disconnect_signals(); |
| 136 | } |
| 137 | |
| 138 | a = p_node_a; |
| 139 | if (Engine::get_singleton()->is_editor_hint()) { |
| 140 | // When in editor, the setter may be called as a result of node rename. |
| 141 | // It happens before the node actually changes its name, which triggers false warning. |
| 142 | callable_mp(this, &Joint2D::_update_joint).call_deferred(false); |
| 143 | } else { |
| 144 | _update_joint(); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | NodePath Joint2D::get_node_a() const { |
| 149 | return a; |
| 150 | } |
| 151 | |
| 152 | void Joint2D::set_node_b(const NodePath &p_node_b) { |
| 153 | if (b == p_node_b) { |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | if (is_configured()) { |
| 158 | _disconnect_signals(); |
| 159 | } |
| 160 | |
| 161 | b = p_node_b; |
| 162 | if (Engine::get_singleton()->is_editor_hint()) { |
| 163 | callable_mp(this, &Joint2D::_update_joint).call_deferred(false); |
| 164 | } else { |
| 165 | _update_joint(); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | NodePath Joint2D::get_node_b() const { |
| 170 | return b; |
| 171 | } |
| 172 | |
| 173 | void Joint2D::_notification(int p_what) { |
| 174 | switch (p_what) { |
| 175 | case NOTIFICATION_POST_ENTER_TREE: { |
| 176 | if (is_configured()) { |
| 177 | _disconnect_signals(); |
| 178 | } |
| 179 | _update_joint(); |
| 180 | } break; |
| 181 | |
| 182 | case NOTIFICATION_EXIT_TREE: { |
| 183 | if (is_configured()) { |
| 184 | _disconnect_signals(); |
| 185 | } |
| 186 | _update_joint(true); |
| 187 | } break; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | void Joint2D::set_bias(real_t p_bias) { |
| 192 | bias = p_bias; |
| 193 | if (joint.is_valid()) { |
| 194 | PhysicsServer2D::get_singleton()->joint_set_param(joint, PhysicsServer2D::JOINT_PARAM_BIAS, bias); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | real_t Joint2D::get_bias() const { |
| 199 | return bias; |
| 200 | } |
| 201 | |
| 202 | void Joint2D::set_exclude_nodes_from_collision(bool p_enable) { |
| 203 | if (exclude_from_collision == p_enable) { |
| 204 | return; |
| 205 | } |
| 206 | if (is_configured()) { |
| 207 | _disconnect_signals(); |
| 208 | } |
| 209 | _update_joint(true); |
| 210 | exclude_from_collision = p_enable; |
| 211 | _update_joint(); |
| 212 | } |
| 213 | |
| 214 | bool Joint2D::get_exclude_nodes_from_collision() const { |
| 215 | return exclude_from_collision; |
| 216 | } |
| 217 | |
| 218 | PackedStringArray Joint2D::get_configuration_warnings() const { |
| 219 | PackedStringArray warnings = Node2D::get_configuration_warnings(); |
| 220 | |
| 221 | if (!warning.is_empty()) { |
| 222 | warnings.push_back(warning); |
| 223 | } |
| 224 | |
| 225 | return warnings; |
| 226 | } |
| 227 | |
| 228 | void Joint2D::_bind_methods() { |
| 229 | ClassDB::bind_method(D_METHOD("set_node_a", "node"), &Joint2D::set_node_a); |
| 230 | ClassDB::bind_method(D_METHOD("get_node_a"), &Joint2D::get_node_a); |
| 231 | |
| 232 | ClassDB::bind_method(D_METHOD("set_node_b", "node"), &Joint2D::set_node_b); |
| 233 | ClassDB::bind_method(D_METHOD("get_node_b"), &Joint2D::get_node_b); |
| 234 | |
| 235 | ClassDB::bind_method(D_METHOD("set_bias", "bias"), &Joint2D::set_bias); |
| 236 | ClassDB::bind_method(D_METHOD("get_bias"), &Joint2D::get_bias); |
| 237 | |
| 238 | ClassDB::bind_method(D_METHOD("set_exclude_nodes_from_collision", "enable"), &Joint2D::set_exclude_nodes_from_collision); |
| 239 | ClassDB::bind_method(D_METHOD("get_exclude_nodes_from_collision"), &Joint2D::get_exclude_nodes_from_collision); |
| 240 | |
| 241 | ClassDB::bind_method(D_METHOD("get_rid"), &Joint2D::get_rid); |
| 242 | |
| 243 | ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_a", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody2D"), "set_node_a", "get_node_a"); |
| 244 | ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_b", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody2D"), "set_node_b", "get_node_b"); |
| 245 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bias", PROPERTY_HINT_RANGE, "0,0.9,0.001"), "set_bias", "get_bias"); |
| 246 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_collision"), "set_exclude_nodes_from_collision", "get_exclude_nodes_from_collision"); |
| 247 | } |
| 248 | |
| 249 | Joint2D::Joint2D() { |
| 250 | joint = PhysicsServer2D::get_singleton()->joint_create(); |
| 251 | set_hide_clip_children(true); |
| 252 | } |
| 253 | |
| 254 | Joint2D::~Joint2D() { |
| 255 | ERR_FAIL_NULL(PhysicsServer2D::get_singleton()); |
| 256 | PhysicsServer2D::get_singleton()->free_rid(joint); |
| 257 | } |