broad_phase_2d_basic.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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-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_2d_basic.h"
  31. BroadPhase2DBasic::ID BroadPhase2DBasic::create(CollisionObject2DSW *p_object_, int p_subindex) {
  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::set_static(ID p_id, bool p_static) {
  46. Map<ID, Element>::Element *E = element_map.find(p_id);
  47. ERR_FAIL_COND(!E);
  48. E->get()._static = p_static;
  49. }
  50. void BroadPhase2DBasic::remove(ID p_id) {
  51. Map<ID, Element>::Element *E = element_map.find(p_id);
  52. ERR_FAIL_COND(!E);
  53. element_map.erase(E);
  54. }
  55. CollisionObject2DSW *BroadPhase2DBasic::get_object(ID p_id) const {
  56. const Map<ID, Element>::Element *E = element_map.find(p_id);
  57. ERR_FAIL_COND_V(!E, nullptr);
  58. return E->get().owner;
  59. }
  60. bool BroadPhase2DBasic::is_static(ID p_id) const {
  61. const Map<ID, Element>::Element *E = element_map.find(p_id);
  62. ERR_FAIL_COND_V(!E, false);
  63. return E->get()._static;
  64. }
  65. int BroadPhase2DBasic::get_subindex(ID p_id) const {
  66. const Map<ID, Element>::Element *E = element_map.find(p_id);
  67. ERR_FAIL_COND_V(!E, -1);
  68. return E->get().subindex;
  69. }
  70. int BroadPhase2DBasic::cull_segment(const Vector2 &p_from, const Vector2 &p_to, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices) {
  71. int rc = 0;
  72. for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
  73. const Rect2 aabb = E->get().aabb;
  74. if (aabb.intersects_segment(p_from, p_to)) {
  75. p_results[rc] = E->get().owner;
  76. p_result_indices[rc] = E->get().subindex;
  77. rc++;
  78. if (rc >= p_max_results) {
  79. break;
  80. }
  81. }
  82. }
  83. return rc;
  84. }
  85. int BroadPhase2DBasic::cull_aabb(const Rect2 &p_aabb, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices) {
  86. int rc = 0;
  87. for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
  88. const Rect2 aabb = E->get().aabb;
  89. if (aabb.intersects(p_aabb)) {
  90. p_results[rc] = E->get().owner;
  91. p_result_indices[rc] = E->get().subindex;
  92. rc++;
  93. if (rc >= p_max_results) {
  94. break;
  95. }
  96. }
  97. }
  98. return rc;
  99. }
  100. void BroadPhase2DBasic::set_pair_callback(PairCallback p_pair_callback, void *p_userdata) {
  101. pair_userdata = p_userdata;
  102. pair_callback = p_pair_callback;
  103. }
  104. void BroadPhase2DBasic::set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata) {
  105. unpair_userdata = p_userdata;
  106. unpair_callback = p_unpair_callback;
  107. }
  108. void BroadPhase2DBasic::update() {
  109. // recompute pairs
  110. for (Map<ID, Element>::Element *I = element_map.front(); I; I = I->next()) {
  111. for (Map<ID, Element>::Element *J = I->next(); J; J = J->next()) {
  112. Element *elem_A = &I->get();
  113. Element *elem_B = &J->get();
  114. if (elem_A->owner == elem_B->owner) {
  115. continue;
  116. }
  117. bool pair_ok = elem_A->aabb.intersects(elem_B->aabb) && (!elem_A->_static || !elem_B->_static);
  118. PairKey key(I->key(), J->key());
  119. Map<PairKey, void *>::Element *E = pair_map.find(key);
  120. if (!pair_ok && E) {
  121. if (unpair_callback) {
  122. unpair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, E->get(), unpair_userdata);
  123. }
  124. pair_map.erase(key);
  125. }
  126. if (pair_ok && !E) {
  127. void *data = nullptr;
  128. if (pair_callback) {
  129. data = pair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, unpair_userdata);
  130. if (data) {
  131. pair_map.insert(key, data);
  132. }
  133. }
  134. }
  135. }
  136. }
  137. }
  138. BroadPhase2DSW *BroadPhase2DBasic::_create() {
  139. return memnew(BroadPhase2DBasic);
  140. }
  141. BroadPhase2DBasic::BroadPhase2DBasic() {
  142. current = 1;
  143. unpair_callback = nullptr;
  144. unpair_userdata = nullptr;
  145. pair_callback = nullptr;
  146. pair_userdata = nullptr;
  147. }