broad_phase_2d_basic.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*************************************************************************/
  2. /* broad_phase_2d_basic.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_2d_basic.h"
  31. BroadPhase2DBasic::ID BroadPhase2DBasic::create(CollisionObject2DSW *p_object_, int p_subindex, const Rect2 &p_aabb, bool p_static) {
  32. current++;
  33. Element e;
  34. e.owner = p_object_;
  35. e._static = false;
  36. e.subindex = p_subindex;
  37. element_map[current] = e;
  38. return current;
  39. }
  40. void BroadPhase2DBasic::move(ID p_id, const Rect2 &p_aabb) {
  41. Map<ID, Element>::Element *E = element_map.find(p_id);
  42. ERR_FAIL_COND(!E);
  43. E->get().aabb = p_aabb;
  44. }
  45. void BroadPhase2DBasic::recheck_pairs(ID p_id) {
  46. // Not supported.
  47. }
  48. void BroadPhase2DBasic::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 BroadPhase2DBasic::remove(ID p_id) {
  54. Map<ID, Element>::Element *E = element_map.find(p_id);
  55. ERR_FAIL_COND(!E);
  56. element_map.erase(E);
  57. }
  58. CollisionObject2DSW *BroadPhase2DBasic::get_object(ID p_id) const {
  59. const Map<ID, Element>::Element *E = element_map.find(p_id);
  60. ERR_FAIL_COND_V(!E, nullptr);
  61. return E->get().owner;
  62. }
  63. bool BroadPhase2DBasic::is_static(ID p_id) const {
  64. const Map<ID, Element>::Element *E = element_map.find(p_id);
  65. ERR_FAIL_COND_V(!E, false);
  66. return E->get()._static;
  67. }
  68. int BroadPhase2DBasic::get_subindex(ID p_id) const {
  69. const Map<ID, Element>::Element *E = element_map.find(p_id);
  70. ERR_FAIL_COND_V(!E, -1);
  71. return E->get().subindex;
  72. }
  73. int BroadPhase2DBasic::cull_segment(const Vector2 &p_from, const Vector2 &p_to, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices) {
  74. int rc = 0;
  75. for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
  76. const Rect2 aabb = E->get().aabb;
  77. if (aabb.intersects_segment(p_from, p_to)) {
  78. p_results[rc] = E->get().owner;
  79. p_result_indices[rc] = E->get().subindex;
  80. rc++;
  81. if (rc >= p_max_results) {
  82. break;
  83. }
  84. }
  85. }
  86. return rc;
  87. }
  88. int BroadPhase2DBasic::cull_aabb(const Rect2 &p_aabb, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices) {
  89. int rc = 0;
  90. for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
  91. const Rect2 aabb = E->get().aabb;
  92. if (aabb.intersects(p_aabb)) {
  93. p_results[rc] = E->get().owner;
  94. p_result_indices[rc] = E->get().subindex;
  95. rc++;
  96. if (rc >= p_max_results) {
  97. break;
  98. }
  99. }
  100. }
  101. return rc;
  102. }
  103. void BroadPhase2DBasic::set_pair_callback(PairCallback p_pair_callback, void *p_userdata) {
  104. pair_userdata = p_userdata;
  105. pair_callback = p_pair_callback;
  106. }
  107. void BroadPhase2DBasic::set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata) {
  108. unpair_userdata = p_userdata;
  109. unpair_callback = p_unpair_callback;
  110. }
  111. void BroadPhase2DBasic::update() {
  112. // recompute pairs
  113. for (Map<ID, Element>::Element *I = element_map.front(); I; I = I->next()) {
  114. for (Map<ID, Element>::Element *J = I->next(); J; J = J->next()) {
  115. Element *elem_A = &I->get();
  116. Element *elem_B = &J->get();
  117. if (elem_A->owner == elem_B->owner) {
  118. continue;
  119. }
  120. bool pair_ok = elem_A->aabb.intersects(elem_B->aabb) && (!elem_A->_static || !elem_B->_static);
  121. PairKey key(I->key(), J->key());
  122. Map<PairKey, void *>::Element *E = pair_map.find(key);
  123. if (!pair_ok && E) {
  124. if (unpair_callback) {
  125. unpair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, E->get(), unpair_userdata);
  126. }
  127. pair_map.erase(key);
  128. }
  129. if (pair_ok && !E) {
  130. void *data = nullptr;
  131. if (pair_callback) {
  132. data = pair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, nullptr, unpair_userdata);
  133. if (data) {
  134. pair_map.insert(key, data);
  135. }
  136. }
  137. }
  138. }
  139. }
  140. }
  141. BroadPhase2DSW *BroadPhase2DBasic::_create() {
  142. return memnew(BroadPhase2DBasic);
  143. }
  144. BroadPhase2DBasic::BroadPhase2DBasic() {
  145. current = 1;
  146. unpair_callback = nullptr;
  147. unpair_userdata = nullptr;
  148. pair_callback = nullptr;
  149. pair_userdata = nullptr;
  150. }