| 1 | /**************************************************************************/ |
| 2 | /* collision_object_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 "collision_object_3d.h" |
| 32 | |
| 33 | #include "scene/resources/3d/shape_3d.h" |
| 34 | #include "scene/resources/mesh.h" |
| 35 | |
| 36 | void CollisionObject3D::_notification(int p_what) { |
| 37 | switch (p_what) { |
| 38 | case NOTIFICATION_ENTER_TREE: { |
| 39 | if (_are_collision_shapes_visible()) { |
| 40 | debug_shape_old_transform = get_global_transform(); |
| 41 | for (const KeyValue<uint32_t, ShapeData> &E : shapes) { |
| 42 | debug_shapes_to_update.insert(E.key); |
| 43 | } |
| 44 | _update_debug_shapes(); |
| 45 | } |
| 46 | #ifdef TOOLS_ENABLED |
| 47 | if (Engine::get_singleton()->is_editor_hint()) { |
| 48 | set_notify_local_transform(true); // Used for warnings and only in editor. |
| 49 | } |
| 50 | #endif |
| 51 | } break; |
| 52 | |
| 53 | case NOTIFICATION_EXIT_TREE: { |
| 54 | if (debug_shapes_count > 0) { |
| 55 | _clear_debug_shapes(); |
| 56 | } |
| 57 | } break; |
| 58 | |
| 59 | case NOTIFICATION_ENTER_WORLD: { |
| 60 | if (area) { |
| 61 | PhysicsServer3D::get_singleton()->area_set_transform(rid, get_global_transform()); |
| 62 | } else { |
| 63 | PhysicsServer3D::get_singleton()->body_set_state(rid, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform()); |
| 64 | } |
| 65 | |
| 66 | bool disabled = !is_enabled(); |
| 67 | |
| 68 | if (disabled && (disable_mode != DISABLE_MODE_REMOVE)) { |
| 69 | _apply_disabled(); |
| 70 | } |
| 71 | |
| 72 | if (!disabled || (disable_mode != DISABLE_MODE_REMOVE)) { |
| 73 | Ref<World3D> world_ref = get_world_3d(); |
| 74 | ERR_FAIL_COND(world_ref.is_null()); |
| 75 | RID space = world_ref->get_space(); |
| 76 | if (area) { |
| 77 | PhysicsServer3D::get_singleton()->area_set_space(rid, space); |
| 78 | } else { |
| 79 | PhysicsServer3D::get_singleton()->body_set_space(rid, space); |
| 80 | } |
| 81 | _space_changed(space); |
| 82 | } |
| 83 | |
| 84 | _update_pickable(); |
| 85 | } break; |
| 86 | |
| 87 | case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: { |
| 88 | update_configuration_warnings(); |
| 89 | } break; |
| 90 | |
| 91 | case NOTIFICATION_TRANSFORM_CHANGED: { |
| 92 | if (only_update_transform_changes) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | if (area) { |
| 97 | PhysicsServer3D::get_singleton()->area_set_transform(rid, get_global_transform()); |
| 98 | } else { |
| 99 | PhysicsServer3D::get_singleton()->body_set_state(rid, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform()); |
| 100 | } |
| 101 | |
| 102 | _on_transform_changed(); |
| 103 | } break; |
| 104 | |
| 105 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 106 | _update_pickable(); |
| 107 | } break; |
| 108 | |
| 109 | case NOTIFICATION_EXIT_WORLD: { |
| 110 | bool disabled = !is_enabled(); |
| 111 | |
| 112 | if (!disabled || (disable_mode != DISABLE_MODE_REMOVE)) { |
| 113 | if (callback_lock > 0) { |
| 114 | ERR_PRINT("Removing a CollisionObject node during a physics callback is not allowed and will cause undesired behavior. Remove with call_deferred() instead."); |
| 115 | } else { |
| 116 | if (area) { |
| 117 | PhysicsServer3D::get_singleton()->area_set_space(rid, RID()); |
| 118 | } else { |
| 119 | PhysicsServer3D::get_singleton()->body_set_space(rid, RID()); |
| 120 | } |
| 121 | _space_changed(RID()); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (disabled && (disable_mode != DISABLE_MODE_REMOVE)) { |
| 126 | _apply_enabled(); |
| 127 | } |
| 128 | } break; |
| 129 | |
| 130 | case NOTIFICATION_DISABLED: { |
| 131 | _apply_disabled(); |
| 132 | } break; |
| 133 | |
| 134 | case NOTIFICATION_ENABLED: { |
| 135 | _apply_enabled(); |
| 136 | } break; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | void CollisionObject3D::set_collision_layer(uint32_t p_layer) { |
| 141 | collision_layer = p_layer; |
| 142 | if (area) { |
| 143 | PhysicsServer3D::get_singleton()->area_set_collision_layer(get_rid(), p_layer); |
| 144 | } else { |
| 145 | PhysicsServer3D::get_singleton()->body_set_collision_layer(get_rid(), p_layer); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | uint32_t CollisionObject3D::get_collision_layer() const { |
| 150 | return collision_layer; |
| 151 | } |
| 152 | |
| 153 | void CollisionObject3D::set_collision_mask(uint32_t p_mask) { |
| 154 | collision_mask = p_mask; |
| 155 | if (area) { |
| 156 | PhysicsServer3D::get_singleton()->area_set_collision_mask(get_rid(), p_mask); |
| 157 | } else { |
| 158 | PhysicsServer3D::get_singleton()->body_set_collision_mask(get_rid(), p_mask); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | uint32_t CollisionObject3D::get_collision_mask() const { |
| 163 | return collision_mask; |
| 164 | } |
| 165 | |
| 166 | void CollisionObject3D::set_collision_layer_value(int p_layer_number, bool p_value) { |
| 167 | ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive."); |
| 168 | ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive."); |
| 169 | uint32_t collision_layer_new = get_collision_layer(); |
| 170 | if (p_value) { |
| 171 | collision_layer_new |= 1 << (p_layer_number - 1); |
| 172 | } else { |
| 173 | collision_layer_new &= ~(1 << (p_layer_number - 1)); |
| 174 | } |
| 175 | set_collision_layer(collision_layer_new); |
| 176 | } |
| 177 | |
| 178 | bool CollisionObject3D::get_collision_layer_value(int p_layer_number) const { |
| 179 | ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive."); |
| 180 | ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive."); |
| 181 | return get_collision_layer() & (1 << (p_layer_number - 1)); |
| 182 | } |
| 183 | |
| 184 | void CollisionObject3D::set_collision_mask_value(int p_layer_number, bool p_value) { |
| 185 | ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive."); |
| 186 | ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive."); |
| 187 | uint32_t mask = get_collision_mask(); |
| 188 | if (p_value) { |
| 189 | mask |= 1 << (p_layer_number - 1); |
| 190 | } else { |
| 191 | mask &= ~(1 << (p_layer_number - 1)); |
| 192 | } |
| 193 | set_collision_mask(mask); |
| 194 | } |
| 195 | |
| 196 | bool CollisionObject3D::get_collision_mask_value(int p_layer_number) const { |
| 197 | ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive."); |
| 198 | ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive."); |
| 199 | return get_collision_mask() & (1 << (p_layer_number - 1)); |
| 200 | } |
| 201 | |
| 202 | void CollisionObject3D::set_collision_priority(real_t p_priority) { |
| 203 | collision_priority = p_priority; |
| 204 | if (!area) { |
| 205 | PhysicsServer3D::get_singleton()->body_set_collision_priority(get_rid(), p_priority); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | real_t CollisionObject3D::get_collision_priority() const { |
| 210 | return collision_priority; |
| 211 | } |
| 212 | |
| 213 | void CollisionObject3D::set_disable_mode(DisableMode p_mode) { |
| 214 | if (disable_mode == p_mode) { |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | bool disabled = is_inside_tree() && !is_enabled(); |
| 219 | |
| 220 | if (disabled) { |
| 221 | // Cancel previous disable mode. |
| 222 | _apply_enabled(); |
| 223 | } |
| 224 | |
| 225 | disable_mode = p_mode; |
| 226 | |
| 227 | if (disabled) { |
| 228 | // Apply new disable mode. |
| 229 | _apply_disabled(); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | CollisionObject3D::DisableMode CollisionObject3D::get_disable_mode() const { |
| 234 | return disable_mode; |
| 235 | } |
| 236 | |
| 237 | void CollisionObject3D::_apply_disabled() { |
| 238 | switch (disable_mode) { |
| 239 | case DISABLE_MODE_REMOVE: { |
| 240 | if (is_inside_tree()) { |
| 241 | if (callback_lock > 0) { |
| 242 | ERR_PRINT("Disabling a CollisionObject node during a physics callback is not allowed and will cause undesired behavior. Disable with call_deferred() instead."); |
| 243 | } else { |
| 244 | if (area) { |
| 245 | PhysicsServer3D::get_singleton()->area_set_space(rid, RID()); |
| 246 | } else { |
| 247 | PhysicsServer3D::get_singleton()->body_set_space(rid, RID()); |
| 248 | } |
| 249 | _space_changed(RID()); |
| 250 | } |
| 251 | } |
| 252 | } break; |
| 253 | |
| 254 | case DISABLE_MODE_MAKE_STATIC: { |
| 255 | if (!area && (body_mode != PhysicsServer3D::BODY_MODE_STATIC)) { |
| 256 | PhysicsServer3D::get_singleton()->body_set_mode(rid, PhysicsServer3D::BODY_MODE_STATIC); |
| 257 | } |
| 258 | } break; |
| 259 | |
| 260 | case DISABLE_MODE_KEEP_ACTIVE: { |
| 261 | // Nothing to do. |
| 262 | } break; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | void CollisionObject3D::_apply_enabled() { |
| 267 | switch (disable_mode) { |
| 268 | case DISABLE_MODE_REMOVE: { |
| 269 | if (is_inside_tree()) { |
| 270 | RID space = get_world_3d()->get_space(); |
| 271 | if (area) { |
| 272 | PhysicsServer3D::get_singleton()->area_set_space(rid, space); |
| 273 | } else { |
| 274 | PhysicsServer3D::get_singleton()->body_set_space(rid, space); |
| 275 | } |
| 276 | _space_changed(space); |
| 277 | } |
| 278 | } break; |
| 279 | |
| 280 | case DISABLE_MODE_MAKE_STATIC: { |
| 281 | if (!area && (body_mode != PhysicsServer3D::BODY_MODE_STATIC)) { |
| 282 | PhysicsServer3D::get_singleton()->body_set_mode(rid, body_mode); |
| 283 | } |
| 284 | } break; |
| 285 | |
| 286 | case DISABLE_MODE_KEEP_ACTIVE: { |
| 287 | // Nothing to do. |
| 288 | } break; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | void CollisionObject3D::_input_event_call(Camera3D *p_camera, const Ref<InputEvent> &p_input_event, const Vector3 &p_pos, const Vector3 &p_normal, int p_shape) { |
| 293 | GDVIRTUAL_CALL(_input_event, p_camera, p_input_event, p_pos, p_normal, p_shape); |
| 294 | emit_signal(SceneStringName(input_event), p_camera, p_input_event, p_pos, p_normal, p_shape); |
| 295 | } |
| 296 | |
| 297 | void CollisionObject3D::_mouse_enter() { |
| 298 | GDVIRTUAL_CALL(_mouse_enter); |
| 299 | emit_signal(SceneStringName(mouse_entered)); |
| 300 | } |
| 301 | |
| 302 | void CollisionObject3D::_mouse_exit() { |
| 303 | GDVIRTUAL_CALL(_mouse_exit); |
| 304 | emit_signal(SceneStringName(mouse_exited)); |
| 305 | } |
| 306 | |
| 307 | void CollisionObject3D::set_body_mode(PhysicsServer3D::BodyMode p_mode) { |
| 308 | ERR_FAIL_COND(area); |
| 309 | |
| 310 | if (body_mode == p_mode) { |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | body_mode = p_mode; |
| 315 | |
| 316 | if (is_inside_tree() && !is_enabled() && (disable_mode == DISABLE_MODE_MAKE_STATIC)) { |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | PhysicsServer3D::get_singleton()->body_set_mode(rid, p_mode); |
| 321 | } |
| 322 | |
| 323 | void CollisionObject3D::_space_changed(const RID &p_new_space) { |
| 324 | } |
| 325 | |
| 326 | void CollisionObject3D::set_only_update_transform_changes(bool p_enable) { |
| 327 | only_update_transform_changes = p_enable; |
| 328 | } |
| 329 | |
| 330 | bool CollisionObject3D::is_only_update_transform_changes_enabled() const { |
| 331 | return only_update_transform_changes; |
| 332 | } |
| 333 | |
| 334 | void CollisionObject3D::_update_pickable() { |
| 335 | if (!is_inside_tree()) { |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | bool pickable = ray_pickable && is_visible_in_tree(); |
| 340 | if (area) { |
| 341 | PhysicsServer3D::get_singleton()->area_set_ray_pickable(rid, pickable); |
| 342 | } else { |
| 343 | PhysicsServer3D::get_singleton()->body_set_ray_pickable(rid, pickable); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | bool CollisionObject3D::_are_collision_shapes_visible() { |
| 348 | return is_inside_tree() && get_tree()->is_debugging_collisions_hint() && !Engine::get_singleton()->is_editor_hint(); |
| 349 | } |
| 350 | |
| 351 | void CollisionObject3D::_update_shape_data(uint32_t p_owner) { |
| 352 | if (_are_collision_shapes_visible()) { |
| 353 | if (debug_shapes_to_update.is_empty()) { |
| 354 | callable_mp(this, &CollisionObject3D::_update_debug_shapes).call_deferred(); |
| 355 | } |
| 356 | debug_shapes_to_update.insert(p_owner); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | void CollisionObject3D::_shape_changed(const Ref<Shape3D> &p_shape) { |
| 361 | for (KeyValue<uint32_t, ShapeData> &E : shapes) { |
| 362 | ShapeData &shapedata = E.value; |
| 363 | ShapeData::ShapeBase *shape_bases = shapedata.shapes.ptrw(); |
| 364 | for (int i = 0; i < shapedata.shapes.size(); i++) { |
| 365 | ShapeData::ShapeBase &s = shape_bases[i]; |
| 366 | if (s.shape == p_shape && s.debug_shape.is_valid()) { |
| 367 | Ref<Mesh> mesh = s.shape->get_debug_mesh(); |
| 368 | RS::get_singleton()->instance_set_base(s.debug_shape, mesh->get_rid()); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | void CollisionObject3D::_update_debug_shapes() { |
| 375 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 376 | |
| 377 | if (!is_inside_tree()) { |
| 378 | debug_shapes_to_update.clear(); |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | for (const uint32_t &shapedata_idx : debug_shapes_to_update) { |
| 383 | if (shapes.has(shapedata_idx)) { |
| 384 | ShapeData &shapedata = shapes[shapedata_idx]; |
| 385 | ShapeData::ShapeBase *shape_bases = shapedata.shapes.ptrw(); |
| 386 | for (int i = 0; i < shapedata.shapes.size(); i++) { |
| 387 | ShapeData::ShapeBase &s = shape_bases[i]; |
| 388 | if (s.shape.is_null() || shapedata.disabled) { |
| 389 | if (s.debug_shape.is_valid()) { |
| 390 | RS::get_singleton()->free_rid(s.debug_shape); |
| 391 | s.debug_shape = RID(); |
| 392 | --debug_shapes_count; |
| 393 | } |
| 394 | continue; |
| 395 | } |
| 396 | |
| 397 | if (s.debug_shape.is_null()) { |
| 398 | s.debug_shape = RS::get_singleton()->instance_create(); |
| 399 | RS::get_singleton()->instance_set_scenario(s.debug_shape, get_world_3d()->get_scenario()); |
| 400 | s.shape->connect_changed(callable_mp(this, &CollisionObject3D::_shape_changed).bind(s.shape), CONNECT_DEFERRED); |
| 401 | ++debug_shapes_count; |
| 402 | } |
| 403 | |
| 404 | Ref<Mesh> mesh = s.shape->get_debug_mesh(); |
| 405 | RS::get_singleton()->instance_set_base(s.debug_shape, mesh->get_rid()); |
| 406 | RS::get_singleton()->instance_set_transform(s.debug_shape, get_global_transform() * shapedata.xform); |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | debug_shapes_to_update.clear(); |
| 411 | } |
| 412 | |
| 413 | void CollisionObject3D::_clear_debug_shapes() { |
| 414 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 415 | |
| 416 | for (KeyValue<uint32_t, ShapeData> &E : shapes) { |
| 417 | ShapeData &shapedata = E.value; |
| 418 | ShapeData::ShapeBase *shape_bases = shapedata.shapes.ptrw(); |
| 419 | for (int i = 0; i < shapedata.shapes.size(); i++) { |
| 420 | ShapeData::ShapeBase &s = shape_bases[i]; |
| 421 | if (s.debug_shape.is_valid()) { |
| 422 | RS::get_singleton()->free_rid(s.debug_shape); |
| 423 | s.debug_shape = RID(); |
| 424 | if (s.shape.is_valid()) { |
| 425 | s.shape->disconnect_changed(callable_mp(this, &CollisionObject3D::_update_shape_data)); |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | debug_shapes_count = 0; |
| 431 | } |
| 432 | |
| 433 | void CollisionObject3D::_on_transform_changed() { |
| 434 | if (debug_shapes_count > 0 && !debug_shape_old_transform.is_equal_approx(get_global_transform())) { |
| 435 | debug_shape_old_transform = get_global_transform(); |
| 436 | for (KeyValue<uint32_t, ShapeData> &E : shapes) { |
| 437 | ShapeData &shapedata = E.value; |
| 438 | if (shapedata.disabled) { |
| 439 | continue; // If disabled then there are no debug shapes to update. |
| 440 | } |
| 441 | const ShapeData::ShapeBase *shape_bases = shapedata.shapes.ptr(); |
| 442 | for (int i = 0; i < shapedata.shapes.size(); i++) { |
| 443 | if (shape_bases[i].debug_shape.is_null()) { |
| 444 | continue; |
| 445 | } |
| 446 | RS::get_singleton()->instance_set_transform(shape_bases[i].debug_shape, debug_shape_old_transform * shapedata.xform); |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | void CollisionObject3D::set_ray_pickable(bool p_ray_pickable) { |
| 453 | ray_pickable = p_ray_pickable; |
| 454 | _update_pickable(); |
| 455 | } |
| 456 | |
| 457 | bool CollisionObject3D::is_ray_pickable() const { |
| 458 | return ray_pickable; |
| 459 | } |
| 460 | |
| 461 | void CollisionObject3D::_bind_methods() { |
| 462 | ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &CollisionObject3D::set_collision_layer); |
| 463 | ClassDB::bind_method(D_METHOD("get_collision_layer"), &CollisionObject3D::get_collision_layer); |
| 464 | ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &CollisionObject3D::set_collision_mask); |
| 465 | ClassDB::bind_method(D_METHOD("get_collision_mask"), &CollisionObject3D::get_collision_mask); |
| 466 | ClassDB::bind_method(D_METHOD("set_collision_layer_value", "layer_number", "value"), &CollisionObject3D::set_collision_layer_value); |
| 467 | ClassDB::bind_method(D_METHOD("get_collision_layer_value", "layer_number"), &CollisionObject3D::get_collision_layer_value); |
| 468 | ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &CollisionObject3D::set_collision_mask_value); |
| 469 | ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &CollisionObject3D::get_collision_mask_value); |
| 470 | ClassDB::bind_method(D_METHOD("set_collision_priority", "priority"), &CollisionObject3D::set_collision_priority); |
| 471 | ClassDB::bind_method(D_METHOD("get_collision_priority"), &CollisionObject3D::get_collision_priority); |
| 472 | ClassDB::bind_method(D_METHOD("set_disable_mode", "mode"), &CollisionObject3D::set_disable_mode); |
| 473 | ClassDB::bind_method(D_METHOD("get_disable_mode"), &CollisionObject3D::get_disable_mode); |
| 474 | ClassDB::bind_method(D_METHOD("set_ray_pickable", "ray_pickable"), &CollisionObject3D::set_ray_pickable); |
| 475 | ClassDB::bind_method(D_METHOD("is_ray_pickable"), &CollisionObject3D::is_ray_pickable); |
| 476 | ClassDB::bind_method(D_METHOD("set_capture_input_on_drag", "enable"), &CollisionObject3D::set_capture_input_on_drag); |
| 477 | ClassDB::bind_method(D_METHOD("get_capture_input_on_drag"), &CollisionObject3D::get_capture_input_on_drag); |
| 478 | ClassDB::bind_method(D_METHOD("get_rid"), &CollisionObject3D::get_rid); |
| 479 | ClassDB::bind_method(D_METHOD("create_shape_owner", "owner"), &CollisionObject3D::create_shape_owner); |
| 480 | ClassDB::bind_method(D_METHOD("remove_shape_owner", "owner_id"), &CollisionObject3D::remove_shape_owner); |
| 481 | ClassDB::bind_method(D_METHOD("get_shape_owners"), &CollisionObject3D::_get_shape_owners); |
| 482 | ClassDB::bind_method(D_METHOD("shape_owner_set_transform", "owner_id", "transform"), &CollisionObject3D::shape_owner_set_transform); |
| 483 | ClassDB::bind_method(D_METHOD("shape_owner_get_transform", "owner_id"), &CollisionObject3D::shape_owner_get_transform); |
| 484 | ClassDB::bind_method(D_METHOD("shape_owner_get_owner", "owner_id"), &CollisionObject3D::shape_owner_get_owner); |
| 485 | ClassDB::bind_method(D_METHOD("shape_owner_set_disabled", "owner_id", "disabled"), &CollisionObject3D::shape_owner_set_disabled); |
| 486 | ClassDB::bind_method(D_METHOD("is_shape_owner_disabled", "owner_id"), &CollisionObject3D::is_shape_owner_disabled); |
| 487 | ClassDB::bind_method(D_METHOD("shape_owner_add_shape", "owner_id", "shape"), &CollisionObject3D::shape_owner_add_shape); |
| 488 | ClassDB::bind_method(D_METHOD("shape_owner_get_shape_count", "owner_id"), &CollisionObject3D::shape_owner_get_shape_count); |
| 489 | ClassDB::bind_method(D_METHOD("shape_owner_get_shape", "owner_id", "shape_id"), &CollisionObject3D::shape_owner_get_shape); |
| 490 | ClassDB::bind_method(D_METHOD("shape_owner_get_shape_index", "owner_id", "shape_id"), &CollisionObject3D::shape_owner_get_shape_index); |
| 491 | ClassDB::bind_method(D_METHOD("shape_owner_remove_shape", "owner_id", "shape_id"), &CollisionObject3D::shape_owner_remove_shape); |
| 492 | ClassDB::bind_method(D_METHOD("shape_owner_clear_shapes", "owner_id"), &CollisionObject3D::shape_owner_clear_shapes); |
| 493 | ClassDB::bind_method(D_METHOD("shape_find_owner", "shape_index"), &CollisionObject3D::shape_find_owner); |
| 494 | |
| 495 | GDVIRTUAL_BIND(_input_event, "camera", "event", "event_position", "normal", "shape_idx"); |
| 496 | GDVIRTUAL_BIND(_mouse_enter); |
| 497 | GDVIRTUAL_BIND(_mouse_exit); |
| 498 | |
| 499 | ADD_SIGNAL(MethodInfo("input_event", PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_RESOURCE_TYPE, "Node"), PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"), PropertyInfo(Variant::VECTOR3, "event_position"), PropertyInfo(Variant::VECTOR3, "normal"), PropertyInfo(Variant::INT, "shape_idx"))); |
| 500 | ADD_SIGNAL(MethodInfo("mouse_entered")); |
| 501 | ADD_SIGNAL(MethodInfo("mouse_exited")); |
| 502 | |
| 503 | ADD_PROPERTY(PropertyInfo(Variant::INT, "disable_mode", PROPERTY_HINT_ENUM, "Remove,Make Static,Keep Active"), "set_disable_mode", "get_disable_mode"); |
| 504 | |
| 505 | ADD_GROUP("Collision", "collision_"); |
| 506 | ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer"); |
| 507 | ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask"); |
| 508 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_priority"), "set_collision_priority", "get_collision_priority"); |
| 509 | |
| 510 | ADD_GROUP("Input", "input_"); |
| 511 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "input_ray_pickable"), "set_ray_pickable", "is_ray_pickable"); |
| 512 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "input_capture_on_drag"), "set_capture_input_on_drag", "get_capture_input_on_drag"); |
| 513 | |
| 514 | BIND_ENUM_CONSTANT(DISABLE_MODE_REMOVE); |
| 515 | BIND_ENUM_CONSTANT(DISABLE_MODE_MAKE_STATIC); |
| 516 | BIND_ENUM_CONSTANT(DISABLE_MODE_KEEP_ACTIVE); |
| 517 | } |
| 518 | |
| 519 | uint32_t CollisionObject3D::create_shape_owner(Object *p_owner) { |
| 520 | ShapeData sd; |
| 521 | uint32_t id; |
| 522 | |
| 523 | if (shapes.is_empty()) { |
| 524 | id = 0; |
| 525 | } else { |
| 526 | id = shapes.back()->key() + 1; |
| 527 | } |
| 528 | |
| 529 | sd.owner_id = p_owner ? p_owner->get_instance_id() : ObjectID(); |
| 530 | |
| 531 | shapes[id] = sd; |
| 532 | |
| 533 | return id; |
| 534 | } |
| 535 | |
| 536 | void CollisionObject3D::remove_shape_owner(uint32_t owner) { |
| 537 | ERR_FAIL_COND(!shapes.has(owner)); |
| 538 | |
| 539 | shape_owner_clear_shapes(owner); |
| 540 | |
| 541 | shapes.erase(owner); |
| 542 | } |
| 543 | |
| 544 | void CollisionObject3D::shape_owner_set_disabled(uint32_t p_owner, bool p_disabled) { |
| 545 | ERR_FAIL_COND(!shapes.has(p_owner)); |
| 546 | |
| 547 | ShapeData &sd = shapes[p_owner]; |
| 548 | if (sd.disabled == p_disabled) { |
| 549 | return; |
| 550 | } |
| 551 | sd.disabled = p_disabled; |
| 552 | |
| 553 | for (int i = 0; i < sd.shapes.size(); i++) { |
| 554 | if (area) { |
| 555 | PhysicsServer3D::get_singleton()->area_set_shape_disabled(rid, sd.shapes[i].index, p_disabled); |
| 556 | } else { |
| 557 | PhysicsServer3D::get_singleton()->body_set_shape_disabled(rid, sd.shapes[i].index, p_disabled); |
| 558 | } |
| 559 | } |
| 560 | _update_shape_data(p_owner); |
| 561 | } |
| 562 | |
| 563 | bool CollisionObject3D::is_shape_owner_disabled(uint32_t p_owner) const { |
| 564 | ERR_FAIL_COND_V(!shapes.has(p_owner), false); |
| 565 | |
| 566 | return shapes[p_owner].disabled; |
| 567 | } |
| 568 | |
| 569 | void CollisionObject3D::get_shape_owners(List<uint32_t> *r_owners) { |
| 570 | for (const KeyValue<uint32_t, ShapeData> &E : shapes) { |
| 571 | r_owners->push_back(E.key); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | PackedInt32Array CollisionObject3D::_get_shape_owners() { |
| 576 | PackedInt32Array ret; |
| 577 | for (const KeyValue<uint32_t, ShapeData> &E : shapes) { |
| 578 | ret.push_back(E.key); |
| 579 | } |
| 580 | |
| 581 | return ret; |
| 582 | } |
| 583 | |
| 584 | void CollisionObject3D::shape_owner_set_transform(uint32_t p_owner, const Transform3D &p_transform) { |
| 585 | ERR_FAIL_COND(!shapes.has(p_owner)); |
| 586 | |
| 587 | ShapeData &sd = shapes[p_owner]; |
| 588 | sd.xform = p_transform; |
| 589 | for (int i = 0; i < sd.shapes.size(); i++) { |
| 590 | if (area) { |
| 591 | PhysicsServer3D::get_singleton()->area_set_shape_transform(rid, sd.shapes[i].index, p_transform); |
| 592 | } else { |
| 593 | PhysicsServer3D::get_singleton()->body_set_shape_transform(rid, sd.shapes[i].index, p_transform); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | _update_shape_data(p_owner); |
| 598 | } |
| 599 | Transform3D CollisionObject3D::shape_owner_get_transform(uint32_t p_owner) const { |
| 600 | ERR_FAIL_COND_V(!shapes.has(p_owner), Transform3D()); |
| 601 | |
| 602 | return shapes[p_owner].xform; |
| 603 | } |
| 604 | |
| 605 | Object *CollisionObject3D::shape_owner_get_owner(uint32_t p_owner) const { |
| 606 | ERR_FAIL_COND_V(!shapes.has(p_owner), nullptr); |
| 607 | |
| 608 | return ObjectDB::get_instance(shapes[p_owner].owner_id); |
| 609 | } |
| 610 | |
| 611 | void CollisionObject3D::shape_owner_add_shape(uint32_t p_owner, RequiredParam<Shape3D> rp_shape) { |
| 612 | ERR_FAIL_COND(!shapes.has(p_owner)); |
| 613 | EXTRACT_PARAM_OR_FAIL(p_shape, rp_shape); |
| 614 | |
| 615 | ShapeData &sd = shapes[p_owner]; |
| 616 | ShapeData::ShapeBase s; |
| 617 | s.index = total_subshapes; |
| 618 | s.shape = p_shape; |
| 619 | |
| 620 | if (area) { |
| 621 | PhysicsServer3D::get_singleton()->area_add_shape(rid, p_shape->get_rid(), sd.xform, sd.disabled); |
| 622 | } else { |
| 623 | PhysicsServer3D::get_singleton()->body_add_shape(rid, p_shape->get_rid(), sd.xform, sd.disabled); |
| 624 | } |
| 625 | sd.shapes.push_back(s); |
| 626 | |
| 627 | total_subshapes++; |
| 628 | |
| 629 | _update_shape_data(p_owner); |
| 630 | update_gizmos(); |
| 631 | } |
| 632 | |
| 633 | int CollisionObject3D::shape_owner_get_shape_count(uint32_t p_owner) const { |
| 634 | ERR_FAIL_COND_V(!shapes.has(p_owner), 0); |
| 635 | |
| 636 | return shapes[p_owner].shapes.size(); |
| 637 | } |
| 638 | |
| 639 | Ref<Shape3D> CollisionObject3D::shape_owner_get_shape(uint32_t p_owner, int p_shape) const { |
| 640 | ERR_FAIL_COND_V(!shapes.has(p_owner), Ref<Shape3D>()); |
| 641 | ERR_FAIL_INDEX_V(p_shape, shapes[p_owner].shapes.size(), Ref<Shape3D>()); |
| 642 | |
| 643 | return shapes[p_owner].shapes[p_shape].shape; |
| 644 | } |
| 645 | |
| 646 | int CollisionObject3D::shape_owner_get_shape_index(uint32_t p_owner, int p_shape) const { |
| 647 | ERR_FAIL_COND_V(!shapes.has(p_owner), -1); |
| 648 | ERR_FAIL_INDEX_V(p_shape, shapes[p_owner].shapes.size(), -1); |
| 649 | |
| 650 | return shapes[p_owner].shapes[p_shape].index; |
| 651 | } |
| 652 | |
| 653 | void CollisionObject3D::shape_owner_remove_shape(uint32_t p_owner, int p_shape) { |
| 654 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 655 | ERR_FAIL_COND(!shapes.has(p_owner)); |
| 656 | ERR_FAIL_INDEX(p_shape, shapes[p_owner].shapes.size()); |
| 657 | |
| 658 | ShapeData::ShapeBase &s = shapes[p_owner].shapes.write[p_shape]; |
| 659 | int index_to_remove = s.index; |
| 660 | |
| 661 | if (area) { |
| 662 | PhysicsServer3D::get_singleton()->area_remove_shape(rid, index_to_remove); |
| 663 | } else { |
| 664 | PhysicsServer3D::get_singleton()->body_remove_shape(rid, index_to_remove); |
| 665 | } |
| 666 | |
| 667 | if (s.debug_shape.is_valid()) { |
| 668 | RS::get_singleton()->free_rid(s.debug_shape); |
| 669 | if (s.shape.is_valid()) { |
| 670 | s.shape->disconnect_changed(callable_mp(this, &CollisionObject3D::_shape_changed)); |
| 671 | } |
| 672 | --debug_shapes_count; |
| 673 | } |
| 674 | |
| 675 | shapes[p_owner].shapes.remove_at(p_shape); |
| 676 | |
| 677 | for (KeyValue<uint32_t, ShapeData> &E : shapes) { |
| 678 | for (int i = 0; i < E.value.shapes.size(); i++) { |
| 679 | if (E.value.shapes[i].index > index_to_remove) { |
| 680 | E.value.shapes.write[i].index -= 1; |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | total_subshapes--; |
| 686 | } |
| 687 | |
| 688 | void CollisionObject3D::shape_owner_clear_shapes(uint32_t p_owner) { |
| 689 | ERR_FAIL_COND(!shapes.has(p_owner)); |
| 690 | |
| 691 | while (shape_owner_get_shape_count(p_owner) > 0) { |
| 692 | shape_owner_remove_shape(p_owner, 0); |
| 693 | } |
| 694 | |
| 695 | update_gizmos(); |
| 696 | } |
| 697 | |
| 698 | uint32_t CollisionObject3D::shape_find_owner(int p_shape_index) const { |
| 699 | ERR_FAIL_INDEX_V(p_shape_index, total_subshapes, UINT32_MAX); |
| 700 | |
| 701 | for (const KeyValue<uint32_t, ShapeData> &E : shapes) { |
| 702 | for (int i = 0; i < E.value.shapes.size(); i++) { |
| 703 | if (E.value.shapes[i].index == p_shape_index) { |
| 704 | return E.key; |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | //in theory it should be unreachable |
| 710 | ERR_FAIL_V_MSG(UINT32_MAX, "Can't find owner for shape index " + itos(p_shape_index) + "."); |
| 711 | } |
| 712 | |
| 713 | CollisionObject3D::CollisionObject3D(RID p_rid, bool p_area) { |
| 714 | rid = p_rid; |
| 715 | area = p_area; |
| 716 | set_notify_transform(true); |
| 717 | _define_ancestry(AncestralClass::COLLISION_OBJECT_3D); |
| 718 | |
| 719 | if (p_area) { |
| 720 | PhysicsServer3D::get_singleton()->area_attach_object_instance_id(rid, get_instance_id()); |
| 721 | } else { |
| 722 | PhysicsServer3D::get_singleton()->body_attach_object_instance_id(rid, get_instance_id()); |
| 723 | PhysicsServer3D::get_singleton()->body_set_mode(rid, body_mode); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | void CollisionObject3D::set_capture_input_on_drag(bool p_capture) { |
| 728 | capture_input_on_drag = p_capture; |
| 729 | } |
| 730 | |
| 731 | bool CollisionObject3D::get_capture_input_on_drag() const { |
| 732 | return capture_input_on_drag; |
| 733 | } |
| 734 | |
| 735 | PackedStringArray CollisionObject3D::get_configuration_warnings() const { |
| 736 | PackedStringArray warnings = Node3D::get_configuration_warnings(); |
| 737 | |
| 738 | if (shapes.is_empty()) { |
| 739 | warnings.push_back(RTR("This node has no shape, so it can't collide or interact with other objects.\nConsider adding a CollisionShape3D or CollisionPolygon3D as a child to define its shape.")); |
| 740 | } |
| 741 | |
| 742 | Vector3 scale = get_transform().get_basis().get_scale(); |
| 743 | if (!(Math::is_zero_approx(scale.x - scale.y) && Math::is_zero_approx(scale.y - scale.z))) { |
| 744 | warnings.push_back(RTR("With a non-uniform scale this node will probably not function as expected.\nPlease make its scale uniform (i.e. the same on all axes), and change the size in children collision shapes instead.")); |
| 745 | } |
| 746 | |
| 747 | return warnings; |
| 748 | } |
| 749 | |
| 750 | CollisionObject3D::CollisionObject3D() { |
| 751 | _define_ancestry(AncestralClass::COLLISION_OBJECT_3D); |
| 752 | |
| 753 | set_notify_transform(true); |
| 754 | //owner= |
| 755 | |
| 756 | //set_transform_notify(true); |
| 757 | } |
| 758 | |
| 759 | CollisionObject3D::~CollisionObject3D() { |
| 760 | ERR_FAIL_NULL(PhysicsServer3D::get_singleton()); |
| 761 | PhysicsServer3D::get_singleton()->free_rid(rid); |
| 762 | } |