jolt_body_accessor_3d.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**************************************************************************/
  2. /* jolt_body_accessor_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_body_accessor_3d.h"
  31. #include "jolt_space_3d.h"
  32. namespace {
  33. template <class... TTypes>
  34. struct VariantVisitors : TTypes... {
  35. using TTypes::operator()...;
  36. };
  37. template <class... TTypes>
  38. VariantVisitors(TTypes...) -> VariantVisitors<TTypes...>;
  39. } // namespace
  40. JoltBodyAccessor3D::JoltBodyAccessor3D(const JoltSpace3D *p_space) :
  41. space(p_space) {
  42. }
  43. JoltBodyAccessor3D::~JoltBodyAccessor3D() = default;
  44. void JoltBodyAccessor3D::acquire(const JPH::BodyID *p_ids, int p_id_count) {
  45. ERR_FAIL_NULL(space);
  46. lock_iface = &space->get_lock_iface();
  47. ids = BodyIDSpan(p_ids, p_id_count);
  48. _acquire_internal(p_ids, p_id_count);
  49. }
  50. void JoltBodyAccessor3D::acquire(const JPH::BodyID &p_id) {
  51. ERR_FAIL_NULL(space);
  52. lock_iface = &space->get_lock_iface();
  53. ids = p_id;
  54. _acquire_internal(&p_id, 1);
  55. }
  56. void JoltBodyAccessor3D::acquire_active() {
  57. const JPH::PhysicsSystem &physics_system = space->get_physics_system();
  58. acquire(physics_system.GetActiveBodiesUnsafe(JPH::EBodyType::RigidBody), (int)physics_system.GetNumActiveBodies(JPH::EBodyType::RigidBody));
  59. }
  60. void JoltBodyAccessor3D::acquire_all() {
  61. ERR_FAIL_NULL(space);
  62. lock_iface = &space->get_lock_iface();
  63. JPH::BodyIDVector *vector = std::get_if<JPH::BodyIDVector>(&ids);
  64. if (vector == nullptr) {
  65. ids = JPH::BodyIDVector();
  66. vector = std::get_if<JPH::BodyIDVector>(&ids);
  67. }
  68. space->get_physics_system().GetBodies(*vector);
  69. _acquire_internal(vector->data(), (int)vector->size());
  70. }
  71. void JoltBodyAccessor3D::release() {
  72. _release_internal();
  73. lock_iface = nullptr;
  74. }
  75. const JPH::BodyID *JoltBodyAccessor3D::get_ids() const {
  76. ERR_FAIL_COND_V(not_acquired(), nullptr);
  77. return std::visit(
  78. VariantVisitors{
  79. [](const JPH::BodyID &p_id) { return &p_id; },
  80. [](const JPH::BodyIDVector &p_vector) { return p_vector.data(); },
  81. [](const BodyIDSpan &p_span) { return p_span.ptr; } },
  82. ids);
  83. }
  84. int JoltBodyAccessor3D::get_count() const {
  85. ERR_FAIL_COND_V(not_acquired(), 0);
  86. return std::visit(
  87. VariantVisitors{
  88. [](const JPH::BodyID &p_id) { return 1; },
  89. [](const JPH::BodyIDVector &p_vector) { return (int)p_vector.size(); },
  90. [](const BodyIDSpan &p_span) { return p_span.count; } },
  91. ids);
  92. }
  93. const JPH::BodyID &JoltBodyAccessor3D::get_at(int p_index) const {
  94. CRASH_BAD_INDEX(p_index, get_count());
  95. return get_ids()[p_index];
  96. }
  97. void JoltBodyReader3D::_acquire_internal(const JPH::BodyID *p_ids, int p_id_count) {
  98. mutex_mask = lock_iface->GetMutexMask(p_ids, p_id_count);
  99. lock_iface->LockRead(mutex_mask);
  100. }
  101. void JoltBodyReader3D::_release_internal() {
  102. ERR_FAIL_COND(not_acquired());
  103. lock_iface->UnlockRead(mutex_mask);
  104. }
  105. JoltBodyReader3D::JoltBodyReader3D(const JoltSpace3D *p_space) :
  106. JoltBodyAccessor3D(p_space) {
  107. }
  108. const JPH::Body *JoltBodyReader3D::try_get(const JPH::BodyID &p_id) const {
  109. if (unlikely(p_id.IsInvalid())) {
  110. return nullptr;
  111. }
  112. ERR_FAIL_COND_V(not_acquired(), nullptr);
  113. return lock_iface->TryGetBody(p_id);
  114. }
  115. const JPH::Body *JoltBodyReader3D::try_get(int p_index) const {
  116. const int count = get_count();
  117. if (unlikely(p_index < 0 || p_index >= count)) {
  118. return nullptr;
  119. }
  120. return try_get(get_at(p_index));
  121. }
  122. const JPH::Body *JoltBodyReader3D::try_get() const {
  123. return try_get(0);
  124. }
  125. void JoltBodyWriter3D::_acquire_internal(const JPH::BodyID *p_ids, int p_id_count) {
  126. mutex_mask = lock_iface->GetMutexMask(p_ids, p_id_count);
  127. lock_iface->LockWrite(mutex_mask);
  128. }
  129. void JoltBodyWriter3D::_release_internal() {
  130. ERR_FAIL_COND(not_acquired());
  131. lock_iface->UnlockWrite(mutex_mask);
  132. }
  133. JoltBodyWriter3D::JoltBodyWriter3D(const JoltSpace3D *p_space) :
  134. JoltBodyAccessor3D(p_space) {
  135. }
  136. JPH::Body *JoltBodyWriter3D::try_get(const JPH::BodyID &p_id) const {
  137. if (unlikely(p_id.IsInvalid())) {
  138. return nullptr;
  139. }
  140. ERR_FAIL_COND_V(not_acquired(), nullptr);
  141. return lock_iface->TryGetBody(p_id);
  142. }
  143. JPH::Body *JoltBodyWriter3D::try_get(int p_index) const {
  144. const int count = get_count();
  145. if (unlikely(p_index < 0 || p_index >= count)) {
  146. return nullptr;
  147. }
  148. return try_get(get_at(p_index));
  149. }
  150. JPH::Body *JoltBodyWriter3D::try_get() const {
  151. return try_get(0);
  152. }