jolt_query_collectors.h 5.8 KB

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