jolt_object_3d.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**************************************************************************/
  2. /* jolt_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. #include "jolt_object_3d.h"
  31. #include "../jolt_physics_server_3d.h"
  32. #include "../jolt_project_settings.h"
  33. #include "../spaces/jolt_layers.h"
  34. #include "../spaces/jolt_space_3d.h"
  35. #include "jolt_group_filter.h"
  36. void JoltObject3D::_remove_from_space() {
  37. if (unlikely(jolt_id.IsInvalid())) {
  38. return;
  39. }
  40. space->remove_body(jolt_id);
  41. jolt_id = JPH::BodyID();
  42. }
  43. void JoltObject3D::_reset_space() {
  44. ERR_FAIL_NULL(space);
  45. _space_changing();
  46. _remove_from_space();
  47. _add_to_space();
  48. _space_changed();
  49. }
  50. void JoltObject3D::_update_object_layer() {
  51. if (!in_space()) {
  52. return;
  53. }
  54. space->get_body_iface().SetObjectLayer(jolt_id, _get_object_layer());
  55. }
  56. void JoltObject3D::_collision_layer_changed() {
  57. _update_object_layer();
  58. }
  59. void JoltObject3D::_collision_mask_changed() {
  60. _update_object_layer();
  61. }
  62. JoltObject3D::JoltObject3D(ObjectType p_object_type) :
  63. object_type(p_object_type) {
  64. }
  65. JoltObject3D::~JoltObject3D() = default;
  66. Object *JoltObject3D::get_instance() const {
  67. return ObjectDB::get_instance(instance_id);
  68. }
  69. void JoltObject3D::set_space(JoltSpace3D *p_space) {
  70. if (space == p_space) {
  71. return;
  72. }
  73. _space_changing();
  74. if (space != nullptr) {
  75. _remove_from_space();
  76. }
  77. space = p_space;
  78. if (space != nullptr) {
  79. _add_to_space();
  80. }
  81. _space_changed();
  82. }
  83. void JoltObject3D::set_collision_layer(uint32_t p_layer) {
  84. if (p_layer == collision_layer) {
  85. return;
  86. }
  87. collision_layer = p_layer;
  88. _collision_layer_changed();
  89. }
  90. void JoltObject3D::set_collision_mask(uint32_t p_mask) {
  91. if (p_mask == collision_mask) {
  92. return;
  93. }
  94. collision_mask = p_mask;
  95. _collision_mask_changed();
  96. }
  97. bool JoltObject3D::can_collide_with(const JoltObject3D &p_other) const {
  98. return (collision_mask & p_other.get_collision_layer()) != 0;
  99. }
  100. bool JoltObject3D::can_interact_with(const JoltObject3D &p_other) const {
  101. if (const JoltBody3D *other_body = p_other.as_body()) {
  102. return can_interact_with(*other_body);
  103. } else if (const JoltArea3D *other_area = p_other.as_area()) {
  104. return can_interact_with(*other_area);
  105. } else if (const JoltSoftBody3D *other_soft_body = p_other.as_soft_body()) {
  106. return can_interact_with(*other_soft_body);
  107. } else {
  108. ERR_FAIL_V_MSG(false, vformat("Unhandled object type: '%d'. This should not happen. Please report this.", p_other.get_type()));
  109. }
  110. }
  111. String JoltObject3D::to_string() const {
  112. static const String fallback_name = "<unknown>";
  113. if (JoltPhysicsServer3D::get_singleton()->is_on_separate_thread()) {
  114. return fallback_name; // Calling `Object::to_string` is not thread-safe.
  115. }
  116. Object *instance = get_instance();
  117. return instance != nullptr ? instance->to_string() : fallback_name;
  118. }