jolt_query_collectors.h 6.1 KB

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