broad_phase_basic.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*************************************************************************/
  2. /* broad_phase_basic.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_basic.h"
  31. #include "core/list.h"
  32. #include "core/print_string.h"
  33. BroadPhaseSW::ID BroadPhaseBasic::create(CollisionObjectSW *p_object, int p_subindex) {
  34. ERR_FAIL_COND_V(p_object == NULL, 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::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 BroadPhaseBasic::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. CollisionObjectSW *BroadPhaseBasic::get_object(ID p_id) const {
  75. const Map<ID, Element>::Element *E = element_map.find(p_id);
  76. ERR_FAIL_COND_V(!E, NULL);
  77. return E->get().owner;
  78. }
  79. bool BroadPhaseBasic::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 BroadPhaseBasic::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 BroadPhaseBasic::cull_point(const Vector3 &p_point, CollisionObjectSW **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. return rc;
  102. }
  103. int BroadPhaseBasic::cull_segment(const Vector3 &p_from, const Vector3 &p_to, CollisionObjectSW **p_results, int p_max_results, int *p_result_indices) {
  104. int rc = 0;
  105. for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
  106. const AABB aabb = E->get().aabb;
  107. if (aabb.intersects_segment(p_from, p_to)) {
  108. p_results[rc] = E->get().owner;
  109. p_result_indices[rc] = E->get().subindex;
  110. rc++;
  111. if (rc >= p_max_results)
  112. break;
  113. }
  114. }
  115. return rc;
  116. }
  117. int BroadPhaseBasic::cull_aabb(const AABB &p_aabb, CollisionObjectSW **p_results, int p_max_results, int *p_result_indices) {
  118. int rc = 0;
  119. for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
  120. const AABB aabb = E->get().aabb;
  121. if (aabb.intersects(p_aabb)) {
  122. p_results[rc] = E->get().owner;
  123. p_result_indices[rc] = E->get().subindex;
  124. rc++;
  125. if (rc >= p_max_results)
  126. break;
  127. }
  128. }
  129. return rc;
  130. }
  131. void BroadPhaseBasic::set_pair_callback(PairCallback p_pair_callback, void *p_userdata) {
  132. pair_userdata = p_userdata;
  133. pair_callback = p_pair_callback;
  134. }
  135. void BroadPhaseBasic::set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata) {
  136. unpair_userdata = p_userdata;
  137. unpair_callback = p_unpair_callback;
  138. }
  139. void BroadPhaseBasic::update() {
  140. // recompute pairs
  141. for (Map<ID, Element>::Element *I = element_map.front(); I; I = I->next()) {
  142. for (Map<ID, Element>::Element *J = I->next(); J; J = J->next()) {
  143. Element *elem_A = &I->get();
  144. Element *elem_B = &J->get();
  145. if (elem_A->owner == elem_B->owner)
  146. continue;
  147. bool pair_ok = elem_A->aabb.intersects(elem_B->aabb) && (!elem_A->_static || !elem_B->_static);
  148. PairKey key(I->key(), J->key());
  149. Map<PairKey, void *>::Element *E = pair_map.find(key);
  150. if (!pair_ok && E) {
  151. if (unpair_callback)
  152. unpair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, E->get(), unpair_userdata);
  153. pair_map.erase(key);
  154. }
  155. if (pair_ok && !E) {
  156. void *data = NULL;
  157. if (pair_callback)
  158. data = pair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, unpair_userdata);
  159. pair_map.insert(key, data);
  160. }
  161. }
  162. }
  163. }
  164. BroadPhaseSW *BroadPhaseBasic::_create() {
  165. return memnew(BroadPhaseBasic);
  166. }
  167. BroadPhaseBasic::BroadPhaseBasic() {
  168. current = 1;
  169. unpair_callback = NULL;
  170. unpair_userdata = NULL;
  171. pair_callback = NULL;
  172. pair_userdata = NULL;
  173. }