jolt_query_collectors.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**************************************************************************/
  2. /* jolt_query_collectors.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_QUERY_COLLECTORS_H
  31. #define JOLT_QUERY_COLLECTORS_H
  32. #include "../jolt_project_settings.h"
  33. #include "jolt_space_3d.h"
  34. #include "Jolt/Jolt.h"
  35. #include "Jolt/Core/STLLocalAllocator.h"
  36. #include "Jolt/Physics/Collision/InternalEdgeRemovingCollector.h"
  37. #include "Jolt/Physics/Collision/Shape/Shape.h"
  38. template <typename TBase, int TDefaultCapacity>
  39. class JoltQueryCollectorAll final : public TBase {
  40. public:
  41. typedef typename TBase::ResultType Hit;
  42. typedef JPH::Array<Hit, JPH::STLLocalAllocator<Hit, TDefaultCapacity>> HitArray;
  43. private:
  44. HitArray hits;
  45. public:
  46. JoltQueryCollectorAll() {
  47. hits.reserve(TDefaultCapacity);
  48. }
  49. bool had_hit() const {
  50. return !hits.is_empty();
  51. }
  52. int get_hit_count() const {
  53. return hits.size();
  54. }
  55. const Hit &get_hit(int p_index) const {
  56. return hits[p_index];
  57. }
  58. void reset() { Reset(); }
  59. virtual void Reset() override {
  60. TBase::Reset();
  61. hits.clear();
  62. }
  63. virtual void AddHit(const Hit &p_hit) override {
  64. hits.push_back(p_hit);
  65. }
  66. };
  67. template <typename TBase>
  68. class JoltQueryCollectorAny final : public TBase {
  69. public:
  70. typedef typename TBase::ResultType Hit;
  71. private:
  72. Hit hit;
  73. bool valid = false;
  74. public:
  75. bool had_hit() const { return valid; }
  76. const Hit &get_hit() const { return hit; }
  77. void reset() {
  78. Reset();
  79. }
  80. virtual void Reset() override {
  81. TBase::Reset();
  82. valid = false;
  83. }
  84. virtual void AddHit(const Hit &p_hit) override {
  85. hit = p_hit;
  86. valid = true;
  87. TBase::ForceEarlyOut();
  88. }
  89. };
  90. template <typename TBase, int TDefaultCapacity>
  91. class JoltQueryCollectorAnyMulti final : public TBase {
  92. public:
  93. typedef typename TBase::ResultType Hit;
  94. typedef JPH::Array<Hit, JPH::STLLocalAllocator<Hit, TDefaultCapacity>> HitArray;
  95. private:
  96. HitArray hits;
  97. int max_hits = 0;
  98. public:
  99. explicit JoltQueryCollectorAnyMulti(int p_max_hits = TDefaultCapacity) :
  100. max_hits(p_max_hits) {
  101. hits.reserve(TDefaultCapacity);
  102. }
  103. bool had_hit() const {
  104. return hits.size() > 0;
  105. }
  106. int get_hit_count() const {
  107. return hits.size();
  108. }
  109. const Hit &get_hit(int p_index) const {
  110. return hits[p_index];
  111. }
  112. void reset() {
  113. Reset();
  114. }
  115. virtual void Reset() override {
  116. TBase::Reset();
  117. hits.clear();
  118. }
  119. virtual void AddHit(const Hit &p_hit) override {
  120. if ((int)hits.size() < max_hits) {
  121. hits.push_back(p_hit);
  122. }
  123. if ((int)hits.size() == max_hits) {
  124. TBase::ForceEarlyOut();
  125. }
  126. }
  127. };
  128. template <typename TBase>
  129. class JoltQueryCollectorClosest final : public TBase {
  130. public:
  131. typedef typename TBase::ResultType Hit;
  132. private:
  133. Hit hit;
  134. bool valid = false;
  135. public:
  136. bool had_hit() const { return valid; }
  137. const Hit &get_hit() const { return hit; }
  138. void reset() {
  139. Reset();
  140. }
  141. virtual void Reset() override {
  142. TBase::Reset();
  143. valid = false;
  144. }
  145. virtual void AddHit(const Hit &p_hit) override {
  146. const float early_out = p_hit.GetEarlyOutFraction();
  147. if (!valid || early_out < hit.GetEarlyOutFraction()) {
  148. TBase::UpdateEarlyOutFraction(early_out);
  149. hit = p_hit;
  150. valid = true;
  151. }
  152. }
  153. };
  154. template <typename TBase, int TDefaultCapacity>
  155. class JoltQueryCollectorClosestMulti final : public TBase {
  156. public:
  157. typedef typename TBase::ResultType Hit;
  158. typedef JPH::Array<Hit, JPH::STLLocalAllocator<Hit, TDefaultCapacity + 1>> HitArray;
  159. private:
  160. HitArray hits;
  161. int max_hits = 0;
  162. public:
  163. explicit JoltQueryCollectorClosestMulti(int p_max_hits = TDefaultCapacity) :
  164. max_hits(p_max_hits) {
  165. hits.reserve(TDefaultCapacity + 1);
  166. }
  167. bool had_hit() const {
  168. return hits.size() > 0;
  169. }
  170. int get_hit_count() const {
  171. return hits.size();
  172. }
  173. const Hit &get_hit(int p_index) const {
  174. return hits[p_index];
  175. }
  176. void reset() {
  177. Reset();
  178. }
  179. virtual void Reset() override {
  180. TBase::Reset();
  181. hits.clear();
  182. }
  183. virtual void AddHit(const Hit &p_hit) override {
  184. typename HitArray::const_iterator E = hits.cbegin();
  185. for (; E != hits.cend(); ++E) {
  186. if (p_hit.GetEarlyOutFraction() < E->GetEarlyOutFraction()) {
  187. break;
  188. }
  189. }
  190. hits.insert(E, p_hit);
  191. if ((int)hits.size() > max_hits) {
  192. hits.resize(max_hits);
  193. }
  194. }
  195. };
  196. #endif // JOLT_QUERY_COLLECTORS_H