jolt_body_accessor_3d.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**************************************************************************/
  2. /* jolt_body_accessor_3d.h */
  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. #ifndef JOLT_BODY_ACCESSOR_3D_H
  31. #define JOLT_BODY_ACCESSOR_3D_H
  32. #include "../objects/jolt_object_3d.h"
  33. #include "Jolt/Jolt.h"
  34. #include "Jolt/Physics/Body/BodyLockInterface.h"
  35. #include <variant>
  36. class JoltArea3D;
  37. class JoltBody3D;
  38. class JoltShapedObject3D;
  39. class JoltSpace3D;
  40. class JoltBodyAccessor3D {
  41. protected:
  42. struct BodyIDSpan {
  43. BodyIDSpan(const JPH::BodyID *p_ptr, int p_count) :
  44. ptr(p_ptr), count(p_count) {}
  45. const JPH::BodyID *ptr;
  46. int count;
  47. };
  48. virtual void _acquire_internal(const JPH::BodyID *p_ids, int p_id_count) = 0;
  49. virtual void _release_internal() = 0;
  50. const JoltSpace3D *space = nullptr;
  51. const JPH::BodyLockInterface *lock_iface = nullptr;
  52. std::variant<JPH::BodyID, JPH::BodyIDVector, BodyIDSpan> ids;
  53. public:
  54. explicit JoltBodyAccessor3D(const JoltSpace3D *p_space);
  55. virtual ~JoltBodyAccessor3D() = 0;
  56. void acquire(const JPH::BodyID *p_ids, int p_id_count);
  57. void acquire(const JPH::BodyID &p_id);
  58. void acquire_active();
  59. void acquire_all();
  60. void release();
  61. bool is_acquired() const { return lock_iface != nullptr; }
  62. bool not_acquired() const { return lock_iface == nullptr; }
  63. const JoltSpace3D &get_space() const { return *space; }
  64. const JPH::BodyID *get_ids() const;
  65. int get_count() const;
  66. const JPH::BodyID &get_at(int p_index) const;
  67. };
  68. class JoltBodyReader3D final : public JoltBodyAccessor3D {
  69. virtual void _acquire_internal(const JPH::BodyID *p_ids, int p_id_count) override;
  70. virtual void _release_internal() override;
  71. JPH::BodyLockInterface::MutexMask mutex_mask = 0;
  72. public:
  73. explicit JoltBodyReader3D(const JoltSpace3D *p_space);
  74. const JPH::Body *try_get(const JPH::BodyID &p_id) const;
  75. const JPH::Body *try_get(int p_index) const;
  76. const JPH::Body *try_get() const;
  77. };
  78. class JoltBodyWriter3D final : public JoltBodyAccessor3D {
  79. virtual void _acquire_internal(const JPH::BodyID *p_ids, int p_id_count) override;
  80. virtual void _release_internal() override;
  81. JPH::BodyLockInterface::MutexMask mutex_mask = 0;
  82. public:
  83. explicit JoltBodyWriter3D(const JoltSpace3D *p_space);
  84. JPH::Body *try_get(const JPH::BodyID &p_id) const;
  85. JPH::Body *try_get(int p_index) const;
  86. JPH::Body *try_get() const;
  87. };
  88. template <typename TBodyAccessor>
  89. class JoltScopedBodyAccessor3D {
  90. TBodyAccessor inner;
  91. public:
  92. JoltScopedBodyAccessor3D(const JoltSpace3D &p_space, const JPH::BodyID *p_ids, int p_id_count) :
  93. inner(&p_space) { inner.acquire(p_ids, p_id_count); }
  94. JoltScopedBodyAccessor3D(const JoltSpace3D &p_space, const JPH::BodyID &p_id) :
  95. inner(&p_space) { inner.acquire(p_id); }
  96. JoltScopedBodyAccessor3D(const JoltScopedBodyAccessor3D &p_other) = delete;
  97. JoltScopedBodyAccessor3D(JoltScopedBodyAccessor3D &&p_other) = default;
  98. ~JoltScopedBodyAccessor3D() { inner.release(); }
  99. const JoltSpace3D &get_space() const { return inner.get_space(); }
  100. int get_count() const { return inner.get_count(); }
  101. const JPH::BodyID &get_at(int p_index) const { return inner.get_at(p_index); }
  102. JoltScopedBodyAccessor3D &operator=(const JoltScopedBodyAccessor3D &p_other) = delete;
  103. JoltScopedBodyAccessor3D &operator=(JoltScopedBodyAccessor3D &&p_other) = default;
  104. decltype(auto) try_get(const JPH::BodyID &p_id) const { return inner.try_get(p_id); }
  105. decltype(auto) try_get(int p_index) const { return inner.try_get(p_index); }
  106. decltype(auto) try_get() const { return inner.try_get(); }
  107. };
  108. template <typename TAccessor, typename TBody>
  109. class JoltAccessibleBody3D {
  110. TAccessor accessor;
  111. TBody *body = nullptr;
  112. public:
  113. JoltAccessibleBody3D(const JoltSpace3D &p_space, const JPH::BodyID &p_id) :
  114. accessor(p_space, p_id), body(accessor.try_get()) {}
  115. bool is_valid() const { return body != nullptr; }
  116. bool is_invalid() const { return body == nullptr; }
  117. JoltObject3D *as_object() const {
  118. if (body != nullptr) {
  119. return reinterpret_cast<JoltObject3D *>(body->GetUserData());
  120. } else {
  121. return nullptr;
  122. }
  123. }
  124. JoltShapedObject3D *as_shaped() const {
  125. if (JoltObject3D *object = as_object(); object != nullptr && object->is_shaped()) {
  126. return reinterpret_cast<JoltShapedObject3D *>(body->GetUserData());
  127. } else {
  128. return nullptr;
  129. }
  130. }
  131. JoltBody3D *as_body() const {
  132. if (JoltObject3D *object = as_object(); object != nullptr && object->is_body()) {
  133. return reinterpret_cast<JoltBody3D *>(body->GetUserData());
  134. } else {
  135. return nullptr;
  136. }
  137. }
  138. JoltArea3D *as_area() const {
  139. if (JoltObject3D *object = as_object(); object != nullptr && object->is_area()) {
  140. return reinterpret_cast<JoltArea3D *>(body->GetUserData());
  141. } else {
  142. return nullptr;
  143. }
  144. }
  145. TBody *operator->() const { return body; }
  146. TBody &operator*() const { return *body; }
  147. explicit operator TBody *() const { return body; }
  148. };
  149. template <typename TAccessor, typename TBody>
  150. class JoltAccessibleBodies3D {
  151. TAccessor accessor;
  152. public:
  153. JoltAccessibleBodies3D(const JoltSpace3D &p_space, const JPH::BodyID *p_ids, int p_id_count) :
  154. accessor(p_space, p_ids, p_id_count) {}
  155. JoltAccessibleBody3D<TAccessor, TBody> operator[](int p_index) const {
  156. const JPH::BodyID &body_id = p_index < accessor.get_count() ? accessor.get_at(p_index) : JPH::BodyID();
  157. return JoltAccessibleBody3D<TAccessor, TBody>(accessor.get_space(), body_id);
  158. }
  159. };
  160. typedef JoltScopedBodyAccessor3D<JoltBodyReader3D> JoltScopedBodyReader3D;
  161. typedef JoltScopedBodyAccessor3D<JoltBodyWriter3D> JoltScopedBodyWriter3D;
  162. typedef JoltAccessibleBody3D<JoltScopedBodyReader3D, const JPH::Body> JoltReadableBody3D;
  163. typedef JoltAccessibleBody3D<JoltScopedBodyWriter3D, JPH::Body> JoltWritableBody3D;
  164. typedef JoltAccessibleBodies3D<JoltScopedBodyReader3D, const JPH::Body> JoltReadableBodies3D;
  165. typedef JoltAccessibleBodies3D<JoltScopedBodyWriter3D, JPH::Body> JoltWritableBodies3D;
  166. #endif // JOLT_BODY_ACCESSOR_3D_H