broad_phase_basic.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**************************************************************************/
  2. /* broad_phase_basic.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 "broad_phase_basic.h"
  31. #include "core/list.h"
  32. #include "core/print_string.h"
  33. BroadPhaseSW::ID BroadPhaseBasic::create(CollisionObjectSW *p_object, int p_subindex, const AABB &p_aabb, bool p_static) {
  34. ERR_FAIL_COND_V(p_object == nullptr, 0);
  35. current++;
  36. Element e;
  37. e.owner = p_object;
  38. e._static = false;
  39. e.subindex = p_subindex;
  40. element_map[current] = e;
  41. return current;
  42. }
  43. void BroadPhaseBasic::move(ID p_id, const AABB &p_aabb) {
  44. Map<ID, Element>::Element *E = element_map.find(p_id);
  45. ERR_FAIL_COND(!E);
  46. E->get().aabb = p_aabb;
  47. }
  48. void BroadPhaseBasic::recheck_pairs(ID p_id) {
  49. // Not supported.
  50. }
  51. void BroadPhaseBasic::set_static(ID p_id, bool p_static) {
  52. Map<ID, Element>::Element *E = element_map.find(p_id);
  53. ERR_FAIL_COND(!E);
  54. E->get()._static = p_static;
  55. }
  56. void BroadPhaseBasic::remove(ID p_id) {
  57. Map<ID, Element>::Element *E = element_map.find(p_id);
  58. ERR_FAIL_COND(!E);
  59. List<PairKey> to_erase;
  60. //unpair must be done immediately on removal to avoid potential invalid pointers
  61. for (Map<PairKey, void *>::Element *F = pair_map.front(); F; F = F->next()) {
  62. if (F->key().a == p_id || F->key().b == p_id) {
  63. if (unpair_callback) {
  64. Element *elem_A = &element_map[F->key().a];
  65. Element *elem_B = &element_map[F->key().b];
  66. unpair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, F->get(), unpair_userdata);
  67. }
  68. to_erase.push_back(F->key());
  69. }
  70. }
  71. while (to_erase.size()) {
  72. pair_map.erase(to_erase.front()->get());
  73. to_erase.pop_front();
  74. }
  75. element_map.erase(E);
  76. }
  77. CollisionObjectSW *BroadPhaseBasic::get_object(ID p_id) const {
  78. const Map<ID, Element>::Element *E = element_map.find(p_id);
  79. ERR_FAIL_COND_V(!E, nullptr);
  80. return E->get().owner;
  81. }
  82. bool BroadPhaseBasic::is_static(ID p_id) const {
  83. const Map<ID, Element>::Element *E = element_map.find(p_id);
  84. ERR_FAIL_COND_V(!E, false);
  85. return E->get()._static;
  86. }
  87. int BroadPhaseBasic::get_subindex(ID p_id) const {
  88. const Map<ID, Element>::Element *E = element_map.find(p_id);
  89. ERR_FAIL_COND_V(!E, -1);
  90. return E->get().subindex;
  91. }
  92. int BroadPhaseBasic::cull_point(const Vector3 &p_point, CollisionObjectSW **p_results, int p_max_results, int *p_result_indices) {
  93. int rc = 0;
  94. for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
  95. const AABB aabb = E->get().aabb;
  96. if (aabb.has_point(p_point)) {
  97. p_results[rc] = E->get().owner;
  98. p_result_indices[rc] = E->get().subindex;
  99. rc++;
  100. if (rc >= p_max_results) {
  101. break;
  102. }
  103. }
  104. }
  105. return rc;
  106. }
  107. int BroadPhaseBasic::cull_segment(const Vector3 &p_from, const Vector3 &p_to, CollisionObjectSW **p_results, int p_max_results, int *p_result_indices) {
  108. int rc = 0;
  109. for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
  110. const AABB aabb = E->get().aabb;
  111. if (aabb.intersects_segment(p_from, p_to)) {
  112. p_results[rc] = E->get().owner;
  113. p_result_indices[rc] = E->get().subindex;
  114. rc++;
  115. if (rc >= p_max_results) {
  116. break;
  117. }
  118. }
  119. }
  120. return rc;
  121. }
  122. int BroadPhaseBasic::cull_aabb(const AABB &p_aabb, CollisionObjectSW **p_results, int p_max_results, int *p_result_indices) {
  123. int rc = 0;
  124. for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
  125. const AABB aabb = E->get().aabb;
  126. if (aabb.intersects(p_aabb)) {
  127. p_results[rc] = E->get().owner;
  128. p_result_indices[rc] = E->get().subindex;
  129. rc++;
  130. if (rc >= p_max_results) {
  131. break;
  132. }
  133. }
  134. }
  135. return rc;
  136. }
  137. void BroadPhaseBasic::set_pair_callback(PairCallback p_pair_callback, void *p_userdata) {
  138. pair_userdata = p_userdata;
  139. pair_callback = p_pair_callback;
  140. }
  141. void BroadPhaseBasic::set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata) {
  142. unpair_userdata = p_userdata;
  143. unpair_callback = p_unpair_callback;
  144. }
  145. void BroadPhaseBasic::update() {
  146. // recompute pairs
  147. for (Map<ID, Element>::Element *I = element_map.front(); I; I = I->next()) {
  148. for (Map<ID, Element>::Element *J = I->next(); J; J = J->next()) {
  149. Element *elem_A = &I->get();
  150. Element *elem_B = &J->get();
  151. if (elem_A->owner == elem_B->owner) {
  152. continue;
  153. }
  154. bool pair_ok = elem_A->aabb.intersects(elem_B->aabb) && (!elem_A->_static || !elem_B->_static);
  155. PairKey key(I->key(), J->key());
  156. Map<PairKey, void *>::Element *E = pair_map.find(key);
  157. if (!pair_ok && E) {
  158. if (unpair_callback) {
  159. unpair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, E->get(), unpair_userdata);
  160. }
  161. pair_map.erase(key);
  162. }
  163. if (pair_ok && !E) {
  164. void *data = nullptr;
  165. if (pair_callback) {
  166. data = pair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, nullptr, unpair_userdata);
  167. if (data) {
  168. pair_map.insert(key, data);
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. BroadPhaseSW *BroadPhaseBasic::_create() {
  176. return memnew(BroadPhaseBasic);
  177. }
  178. BroadPhaseBasic::BroadPhaseBasic() {
  179. current = 1;
  180. unpair_callback = nullptr;
  181. unpair_userdata = nullptr;
  182. pair_callback = nullptr;
  183. pair_userdata = nullptr;
  184. }