broad_phase_3d_basic.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*************************************************************************/
  2. /* broad_phase_3d_basic.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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_3d_basic.h"
  31. #include "core/string/print_string.h"
  32. #include "core/templates/list.h"
  33. BroadPhase3DSW::ID BroadPhase3DBasic::create(CollisionObject3DSW *p_object, int p_subindex) {
  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 BroadPhase3DBasic::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 BroadPhase3DBasic::set_static(ID p_id, bool p_static) {
  49. Map<ID, Element>::Element *E = element_map.find(p_id);
  50. ERR_FAIL_COND(!E);
  51. E->get()._static = p_static;
  52. }
  53. void BroadPhase3DBasic::remove(ID p_id) {
  54. Map<ID, Element>::Element *E = element_map.find(p_id);
  55. ERR_FAIL_COND(!E);
  56. List<PairKey> to_erase;
  57. //unpair must be done immediately on removal to avoid potential invalid pointers
  58. for (Map<PairKey, void *>::Element *F = pair_map.front(); F; F = F->next()) {
  59. if (F->key().a == p_id || F->key().b == p_id) {
  60. if (unpair_callback) {
  61. Element *elem_A = &element_map[F->key().a];
  62. Element *elem_B = &element_map[F->key().b];
  63. unpair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, F->get(), unpair_userdata);
  64. }
  65. to_erase.push_back(F->key());
  66. }
  67. }
  68. while (to_erase.size()) {
  69. pair_map.erase(to_erase.front()->get());
  70. to_erase.pop_front();
  71. }
  72. element_map.erase(E);
  73. }
  74. CollisionObject3DSW *BroadPhase3DBasic::get_object(ID p_id) const {
  75. const Map<ID, Element>::Element *E = element_map.find(p_id);
  76. ERR_FAIL_COND_V(!E, nullptr);
  77. return E->get().owner;
  78. }
  79. bool BroadPhase3DBasic::is_static(ID p_id) const {
  80. const Map<ID, Element>::Element *E = element_map.find(p_id);
  81. ERR_FAIL_COND_V(!E, false);
  82. return E->get()._static;
  83. }
  84. int BroadPhase3DBasic::get_subindex(ID p_id) const {
  85. const Map<ID, Element>::Element *E = element_map.find(p_id);
  86. ERR_FAIL_COND_V(!E, -1);
  87. return E->get().subindex;
  88. }
  89. int BroadPhase3DBasic::cull_point(const Vector3 &p_point, CollisionObject3DSW **p_results, int p_max_results, int *p_result_indices) {
  90. int rc = 0;
  91. for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
  92. const AABB aabb = E->get().aabb;
  93. if (aabb.has_point(p_point)) {
  94. p_results[rc] = E->get().owner;
  95. p_result_indices[rc] = E->get().subindex;
  96. rc++;
  97. if (rc >= p_max_results) {
  98. break;
  99. }
  100. }
  101. }
  102. return rc;
  103. }
  104. int BroadPhase3DBasic::cull_segment(const Vector3 &p_from, const Vector3 &p_to, CollisionObject3DSW **p_results, int p_max_results, int *p_result_indices) {
  105. int rc = 0;
  106. for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
  107. const AABB aabb = E->get().aabb;
  108. if (aabb.intersects_segment(p_from, p_to)) {
  109. p_results[rc] = E->get().owner;
  110. p_result_indices[rc] = E->get().subindex;
  111. rc++;
  112. if (rc >= p_max_results) {
  113. break;
  114. }
  115. }
  116. }
  117. return rc;
  118. }
  119. int BroadPhase3DBasic::cull_aabb(const AABB &p_aabb, CollisionObject3DSW **p_results, int p_max_results, int *p_result_indices) {
  120. int rc = 0;
  121. for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
  122. const AABB aabb = E->get().aabb;
  123. if (aabb.intersects(p_aabb)) {
  124. p_results[rc] = E->get().owner;
  125. p_result_indices[rc] = E->get().subindex;
  126. rc++;
  127. if (rc >= p_max_results) {
  128. break;
  129. }
  130. }
  131. }
  132. return rc;
  133. }
  134. void BroadPhase3DBasic::set_pair_callback(PairCallback p_pair_callback, void *p_userdata) {
  135. pair_userdata = p_userdata;
  136. pair_callback = p_pair_callback;
  137. }
  138. void BroadPhase3DBasic::set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata) {
  139. unpair_userdata = p_userdata;
  140. unpair_callback = p_unpair_callback;
  141. }
  142. void BroadPhase3DBasic::update() {
  143. // recompute pairs
  144. for (Map<ID, Element>::Element *I = element_map.front(); I; I = I->next()) {
  145. for (Map<ID, Element>::Element *J = I->next(); J; J = J->next()) {
  146. Element *elem_A = &I->get();
  147. Element *elem_B = &J->get();
  148. if (elem_A->owner == elem_B->owner) {
  149. continue;
  150. }
  151. bool pair_ok = elem_A->aabb.intersects(elem_B->aabb) && (!elem_A->_static || !elem_B->_static);
  152. PairKey key(I->key(), J->key());
  153. Map<PairKey, void *>::Element *E = pair_map.find(key);
  154. if (!pair_ok && E) {
  155. if (unpair_callback) {
  156. unpair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, E->get(), unpair_userdata);
  157. }
  158. pair_map.erase(key);
  159. }
  160. if (pair_ok && !E) {
  161. void *data = nullptr;
  162. if (pair_callback) {
  163. data = pair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, unpair_userdata);
  164. if (data) {
  165. pair_map.insert(key, data);
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. BroadPhase3DSW *BroadPhase3DBasic::_create() {
  173. return memnew(BroadPhase3DBasic);
  174. }
  175. BroadPhase3DBasic::BroadPhase3DBasic() {
  176. current = 1;
  177. unpair_callback = nullptr;
  178. unpair_userdata = nullptr;
  179. pair_callback = nullptr;
  180. pair_userdata = nullptr;
  181. }