| 1 | /**************************************************************************/ |
| 2 | /* physical_bone_simulator_3d.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 "physical_bone_simulator_3d.h" |
| 32 | |
| 33 | #include "scene/3d/physics/physical_bone_3d.h" |
| 34 | |
| 35 | void PhysicalBoneSimulator3D::_skeleton_changed(Skeleton3D *p_old, Skeleton3D *p_new) { |
| 36 | if (p_old) { |
| 37 | if (p_old->is_connected(SNAME("bone_list_changed"), callable_mp(this, &PhysicalBoneSimulator3D::_bone_list_changed))) { |
| 38 | p_old->disconnect(SNAME("bone_list_changed"), callable_mp(this, &PhysicalBoneSimulator3D::_bone_list_changed)); |
| 39 | } |
| 40 | if (p_old->is_connected(SceneStringName(pose_updated), callable_mp(this, &PhysicalBoneSimulator3D::_pose_updated))) { |
| 41 | p_old->disconnect(SceneStringName(pose_updated), callable_mp(this, &PhysicalBoneSimulator3D::_pose_updated)); |
| 42 | } |
| 43 | } |
| 44 | if (p_new) { |
| 45 | if (!p_new->is_connected(SNAME("bone_list_changed"), callable_mp(this, &PhysicalBoneSimulator3D::_bone_list_changed))) { |
| 46 | p_new->connect(SNAME("bone_list_changed"), callable_mp(this, &PhysicalBoneSimulator3D::_bone_list_changed)); |
| 47 | } |
| 48 | if (!p_new->is_connected(SceneStringName(pose_updated), callable_mp(this, &PhysicalBoneSimulator3D::_pose_updated))) { |
| 49 | p_new->connect(SceneStringName(pose_updated), callable_mp(this, &PhysicalBoneSimulator3D::_pose_updated)); |
| 50 | } |
| 51 | } |
| 52 | _bone_list_changed(); |
| 53 | } |
| 54 | |
| 55 | void PhysicalBoneSimulator3D::_bone_list_changed() { |
| 56 | bones.clear(); |
| 57 | Skeleton3D *skeleton = get_skeleton(); |
| 58 | if (!skeleton) { |
| 59 | return; |
| 60 | } |
| 61 | for (int i = 0; i < skeleton->get_bone_count(); i++) { |
| 62 | SimulatedBone sb; |
| 63 | sb.parent = skeleton->get_bone_parent(i); |
| 64 | sb.child_bones = skeleton->get_bone_children(i); |
| 65 | bones.push_back(sb); |
| 66 | } |
| 67 | _rebuild_physical_bones_cache(); |
| 68 | _pose_updated(); |
| 69 | } |
| 70 | |
| 71 | void PhysicalBoneSimulator3D::_pose_updated() { |
| 72 | Skeleton3D *skeleton = get_skeleton(); |
| 73 | if (!skeleton || simulating) { |
| 74 | return; |
| 75 | } |
| 76 | // If this triggers that means that we likely haven't rebuilt the bone list yet. |
| 77 | if (skeleton->get_bone_count() != (int)bones.size()) { |
| 78 | // NOTE: this is re-entrant and will call _pose_updated again. |
| 79 | _bone_list_changed(); |
| 80 | } else { |
| 81 | for (int i = 0; i < skeleton->get_bone_count(); i++) { |
| 82 | _bone_pose_updated(skeleton, i); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void PhysicalBoneSimulator3D::_bone_pose_updated(Skeleton3D *p_skeleton, int p_bone_id) { |
| 88 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_bone_id, bones.size()); |
| 89 | bones[p_bone_id].global_pose = p_skeleton->get_bone_global_pose(p_bone_id); |
| 90 | } |
| 91 | |
| 92 | void PhysicalBoneSimulator3D::_set_active(bool p_active) { |
| 93 | if (!Engine::get_singleton()->is_editor_hint()) { |
| 94 | _reset_physical_bones_state(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void PhysicalBoneSimulator3D::_reset_physical_bones_state() { |
| 99 | for (uint32_t i = 0; i < bones.size(); i += 1) { |
| 100 | if (bones[i].physical_bone) { |
| 101 | bones[i].physical_bone->reset_physics_simulation_state(); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | bool PhysicalBoneSimulator3D::is_simulating_physics() const { |
| 107 | return simulating; |
| 108 | } |
| 109 | |
| 110 | int PhysicalBoneSimulator3D::find_bone(const String &p_name) const { |
| 111 | Skeleton3D *skeleton = get_skeleton(); |
| 112 | if (!skeleton) { |
| 113 | return -1; |
| 114 | } |
| 115 | return skeleton->find_bone(p_name); |
| 116 | } |
| 117 | |
| 118 | String PhysicalBoneSimulator3D::get_bone_name(int p_bone) const { |
| 119 | Skeleton3D *skeleton = get_skeleton(); |
| 120 | if (!skeleton) { |
| 121 | return String(); |
| 122 | } |
| 123 | return skeleton->get_bone_name(p_bone); |
| 124 | } |
| 125 | |
| 126 | int PhysicalBoneSimulator3D::get_bone_count() const { |
| 127 | return bones.size(); |
| 128 | } |
| 129 | |
| 130 | bool PhysicalBoneSimulator3D::is_bone_parent_of(int p_bone, int p_parent_bone_id) const { |
| 131 | Skeleton3D *skeleton = get_skeleton(); |
| 132 | if (!skeleton) { |
| 133 | return false; |
| 134 | } |
| 135 | return skeleton->is_bone_parent_of(p_bone, p_parent_bone_id); |
| 136 | } |
| 137 | |
| 138 | void PhysicalBoneSimulator3D::bind_physical_bone_to_bone(int p_bone, PhysicalBone3D *p_physical_bone) { |
| 139 | const int bone_size = bones.size(); |
| 140 | ERR_FAIL_INDEX(p_bone, bone_size); |
| 141 | ERR_FAIL_COND(bones[p_bone].physical_bone); |
| 142 | ERR_FAIL_NULL(p_physical_bone); |
| 143 | bones[p_bone].physical_bone = p_physical_bone; |
| 144 | |
| 145 | _rebuild_physical_bones_cache(); |
| 146 | } |
| 147 | |
| 148 | void PhysicalBoneSimulator3D::unbind_physical_bone_from_bone(int p_bone) { |
| 149 | const int bone_size = bones.size(); |
| 150 | ERR_FAIL_INDEX(p_bone, bone_size); |
| 151 | bones[p_bone].physical_bone = nullptr; |
| 152 | |
| 153 | _rebuild_physical_bones_cache(); |
| 154 | } |
| 155 | |
| 156 | PhysicalBone3D *PhysicalBoneSimulator3D::get_physical_bone(int p_bone) { |
| 157 | const int bone_size = bones.size(); |
| 158 | ERR_FAIL_INDEX_V(p_bone, bone_size, nullptr); |
| 159 | |
| 160 | return bones[p_bone].physical_bone; |
| 161 | } |
| 162 | |
| 163 | PhysicalBone3D *PhysicalBoneSimulator3D::get_physical_bone_parent(int p_bone) { |
| 164 | const int bone_size = bones.size(); |
| 165 | ERR_FAIL_INDEX_V(p_bone, bone_size, nullptr); |
| 166 | |
| 167 | if (bones[p_bone].cache_parent_physical_bone) { |
| 168 | return bones[p_bone].cache_parent_physical_bone; |
| 169 | } |
| 170 | |
| 171 | return _get_physical_bone_parent(p_bone); |
| 172 | } |
| 173 | |
| 174 | PhysicalBone3D *PhysicalBoneSimulator3D::_get_physical_bone_parent(int p_bone) { |
| 175 | const int bone_size = bones.size(); |
| 176 | ERR_FAIL_INDEX_V(p_bone, bone_size, nullptr); |
| 177 | |
| 178 | const int parent_bone = bones[p_bone].parent; |
| 179 | if (parent_bone < 0) { |
| 180 | return nullptr; |
| 181 | } |
| 182 | |
| 183 | PhysicalBone3D *pb = bones[parent_bone].physical_bone; |
| 184 | if (pb) { |
| 185 | return pb; |
| 186 | } else { |
| 187 | return get_physical_bone_parent(parent_bone); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | void PhysicalBoneSimulator3D::_rebuild_physical_bones_cache() { |
| 192 | const int b_size = bones.size(); |
| 193 | for (int i = 0; i < b_size; ++i) { |
| 194 | PhysicalBone3D *parent_pb = _get_physical_bone_parent(i); |
| 195 | if (parent_pb != bones[i].cache_parent_physical_bone) { |
| 196 | bones[i].cache_parent_physical_bone = parent_pb; |
| 197 | if (bones[i].physical_bone) { |
| 198 | bones[i].physical_bone->_on_bone_parent_changed(); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | #ifndef DISABLE_DEPRECATED |
| 205 | void _pb_stop_simulation_compat(Node *p_node) { |
| 206 | PhysicalBoneSimulator3D *ps = Object::cast_to<PhysicalBoneSimulator3D>(p_node); |
| 207 | if (ps) { |
| 208 | return; // Prevent conflict. |
| 209 | } |
| 210 | for (int i = p_node->get_child_count() - 1; i >= 0; --i) { |
| 211 | _pb_stop_simulation_compat(p_node->get_child(i)); |
| 212 | } |
| 213 | PhysicalBone3D *pb = Object::cast_to<PhysicalBone3D>(p_node); |
| 214 | if (pb) { |
| 215 | pb->set_simulate_physics(false); |
| 216 | } |
| 217 | } |
| 218 | #endif // _DISABLE_DEPRECATED |
| 219 | |
| 220 | void _pb_stop_simulation(Node *p_node) { |
| 221 | for (int i = p_node->get_child_count() - 1; i >= 0; --i) { |
| 222 | PhysicalBone3D *pb = Object::cast_to<PhysicalBone3D>(p_node->get_child(i)); |
| 223 | if (!pb) { |
| 224 | continue; |
| 225 | } |
| 226 | _pb_stop_simulation(pb); |
| 227 | } |
| 228 | PhysicalBone3D *pb = Object::cast_to<PhysicalBone3D>(p_node); |
| 229 | if (pb) { |
| 230 | pb->set_simulate_physics(false); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void PhysicalBoneSimulator3D::physical_bones_stop_simulation() { |
| 235 | simulating = false; |
| 236 | _reset_physical_bones_state(); |
| 237 | #ifndef DISABLE_DEPRECATED |
| 238 | if (is_compat) { |
| 239 | Skeleton3D *sk = get_skeleton(); |
| 240 | if (sk) { |
| 241 | _pb_stop_simulation_compat(sk); |
| 242 | } |
| 243 | } else { |
| 244 | _pb_stop_simulation(this); |
| 245 | } |
| 246 | #else |
| 247 | _pb_stop_simulation(this); |
| 248 | #endif // _DISABLE_DEPRECATED |
| 249 | } |
| 250 | |
| 251 | #ifndef DISABLE_DEPRECATED |
| 252 | void _pb_start_simulation_compat(const PhysicalBoneSimulator3D *p_simulator, Node *p_node, const Vector<int> &p_sim_bones) { |
| 253 | PhysicalBoneSimulator3D *ps = Object::cast_to<PhysicalBoneSimulator3D>(p_node); |
| 254 | if (ps) { |
| 255 | return; // Prevent conflict. |
| 256 | } |
| 257 | for (int i = p_node->get_child_count() - 1; i >= 0; --i) { |
| 258 | _pb_start_simulation_compat(p_simulator, p_node->get_child(i), p_sim_bones); |
| 259 | } |
| 260 | PhysicalBone3D *pb = Object::cast_to<PhysicalBone3D>(p_node); |
| 261 | if (pb) { |
| 262 | if (p_sim_bones.is_empty()) { // If no bones are specified, activate ragdoll on full body. |
| 263 | pb->set_simulate_physics(true); |
| 264 | } else { |
| 265 | for (int i = p_sim_bones.size() - 1; i >= 0; --i) { |
| 266 | if (p_sim_bones[i] == pb->get_bone_id() || p_simulator->is_bone_parent_of(pb->get_bone_id(), p_sim_bones[i])) { |
| 267 | pb->set_simulate_physics(true); |
| 268 | break; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | #endif // _DISABLE_DEPRECATED |
| 275 | |
| 276 | void _pb_start_simulation(const PhysicalBoneSimulator3D *p_simulator, Node *p_node, const Vector<int> &p_sim_bones) { |
| 277 | for (int i = p_node->get_child_count() - 1; i >= 0; --i) { |
| 278 | PhysicalBone3D *pb = Object::cast_to<PhysicalBone3D>(p_node->get_child(i)); |
| 279 | if (!pb) { |
| 280 | continue; |
| 281 | } |
| 282 | _pb_start_simulation(p_simulator, pb, p_sim_bones); |
| 283 | } |
| 284 | PhysicalBone3D *pb = Object::cast_to<PhysicalBone3D>(p_node); |
| 285 | if (pb) { |
| 286 | if (p_sim_bones.is_empty()) { // If no bones are specified, activate ragdoll on full body. |
| 287 | pb->set_simulate_physics(true); |
| 288 | } else { |
| 289 | for (int i = p_sim_bones.size() - 1; i >= 0; --i) { |
| 290 | if (p_sim_bones[i] == pb->get_bone_id() || p_simulator->is_bone_parent_of(pb->get_bone_id(), p_sim_bones[i])) { |
| 291 | pb->set_simulate_physics(true); |
| 292 | break; |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | void PhysicalBoneSimulator3D::physical_bones_start_simulation_on(const TypedArray<StringName> &p_bones) { |
| 300 | _pose_updated(); |
| 301 | |
| 302 | simulating = true; |
| 303 | _reset_physical_bones_state(); |
| 304 | |
| 305 | Vector<int> sim_bones; |
| 306 | if (p_bones.size() > 0) { |
| 307 | sim_bones.resize(p_bones.size()); |
| 308 | int c = 0; |
| 309 | for (int i = sim_bones.size() - 1; i >= 0; --i) { |
| 310 | int bone_id = find_bone(p_bones[i]); |
| 311 | if (bone_id != -1) { |
| 312 | sim_bones.write[c++] = bone_id; |
| 313 | } |
| 314 | } |
| 315 | sim_bones.resize(c); |
| 316 | } |
| 317 | |
| 318 | #ifndef DISABLE_DEPRECATED |
| 319 | if (is_compat) { |
| 320 | Skeleton3D *sk = get_skeleton(); |
| 321 | if (sk) { |
| 322 | _pb_start_simulation_compat(this, sk, sim_bones); |
| 323 | } |
| 324 | } else { |
| 325 | _pb_start_simulation(this, this, sim_bones); |
| 326 | } |
| 327 | #else |
| 328 | _pb_start_simulation(this, this, sim_bones); |
| 329 | #endif // _DISABLE_DEPRECATED |
| 330 | } |
| 331 | |
| 332 | void _physical_bones_add_remove_collision_exception(bool p_add, Node *p_node, RID p_exception) { |
| 333 | for (int i = p_node->get_child_count() - 1; i >= 0; --i) { |
| 334 | _physical_bones_add_remove_collision_exception(p_add, p_node->get_child(i), p_exception); |
| 335 | } |
| 336 | |
| 337 | CollisionObject3D *co = Object::cast_to<CollisionObject3D>(p_node); |
| 338 | if (co) { |
| 339 | if (p_add) { |
| 340 | PhysicsServer3D::get_singleton()->body_add_collision_exception(co->get_rid(), p_exception); |
| 341 | } else { |
| 342 | PhysicsServer3D::get_singleton()->body_remove_collision_exception(co->get_rid(), p_exception); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | void PhysicalBoneSimulator3D::physical_bones_add_collision_exception(RID p_exception) { |
| 348 | _physical_bones_add_remove_collision_exception(true, this, p_exception); |
| 349 | } |
| 350 | |
| 351 | void PhysicalBoneSimulator3D::physical_bones_remove_collision_exception(RID p_exception) { |
| 352 | _physical_bones_add_remove_collision_exception(false, this, p_exception); |
| 353 | } |
| 354 | |
| 355 | Transform3D PhysicalBoneSimulator3D::get_bone_global_pose(int p_bone) const { |
| 356 | const int bone_size = bones.size(); |
| 357 | ERR_FAIL_INDEX_V(p_bone, bone_size, Transform3D()); |
| 358 | return bones[p_bone].global_pose; |
| 359 | } |
| 360 | |
| 361 | void PhysicalBoneSimulator3D::set_bone_global_pose(int p_bone, const Transform3D &p_pose) { |
| 362 | const int bone_size = bones.size(); |
| 363 | ERR_FAIL_INDEX(p_bone, bone_size); |
| 364 | bones[p_bone].global_pose = p_pose; |
| 365 | } |
| 366 | |
| 367 | void PhysicalBoneSimulator3D::_process_modification(double p_delta) { |
| 368 | Skeleton3D *skeleton = get_skeleton(); |
| 369 | if (!skeleton) { |
| 370 | return; |
| 371 | } |
| 372 | ERR_FAIL_COND(skeleton->get_bone_count() != (int)bones.size()); |
| 373 | for (int i = 0; i < skeleton->get_bone_count(); i++) { |
| 374 | if (!bones[i].physical_bone) { |
| 375 | continue; |
| 376 | } |
| 377 | if (bones[i].physical_bone->is_simulating_physics() == false) { |
| 378 | _bone_pose_updated(skeleton, i); |
| 379 | bones[i].physical_bone->reset_to_rest_position(); |
| 380 | } else if (simulating) { |
| 381 | skeleton->set_bone_global_pose(i, bones[i].global_pose); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | void PhysicalBoneSimulator3D::_bind_methods() { |
| 387 | ClassDB::bind_method(D_METHOD("is_simulating_physics"), &PhysicalBoneSimulator3D::is_simulating_physics); |
| 388 | |
| 389 | ClassDB::bind_method(D_METHOD("physical_bones_stop_simulation"), &PhysicalBoneSimulator3D::physical_bones_stop_simulation); |
| 390 | ClassDB::bind_method(D_METHOD("physical_bones_start_simulation", "bones"), &PhysicalBoneSimulator3D::physical_bones_start_simulation_on, DEFVAL(Array())); |
| 391 | ClassDB::bind_method(D_METHOD("physical_bones_add_collision_exception", "exception"), &PhysicalBoneSimulator3D::physical_bones_add_collision_exception); |
| 392 | ClassDB::bind_method(D_METHOD("physical_bones_remove_collision_exception", "exception"), &PhysicalBoneSimulator3D::physical_bones_remove_collision_exception); |
| 393 | } |
| 394 | |
| 395 | PhysicalBoneSimulator3D::PhysicalBoneSimulator3D() { |
| 396 | } |