broad_phase_2d_basic.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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-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_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, NULL);
  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. return rc;
  83. }
  84. int BroadPhase2DBasic::cull_aabb(const Rect2 &p_aabb, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices) {
  85. int rc = 0;
  86. for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
  87. const Rect2 aabb = E->get().aabb;
  88. if (aabb.intersects(p_aabb)) {
  89. p_results[rc] = E->get().owner;
  90. p_result_indices[rc] = E->get().subindex;
  91. rc++;
  92. if (rc >= p_max_results)
  93. break;
  94. }
  95. }
  96. return rc;
  97. }
  98. void BroadPhase2DBasic::set_pair_callback(PairCallback p_pair_callback, void *p_userdata) {
  99. pair_userdata = p_userdata;
  100. pair_callback = p_pair_callback;
  101. }
  102. void BroadPhase2DBasic::set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata) {
  103. unpair_userdata = p_userdata;
  104. unpair_callback = p_unpair_callback;
  105. }
  106. void BroadPhase2DBasic::update() {
  107. // recompute pairs
  108. for (Map<ID, Element>::Element *I = element_map.front(); I; I = I->next()) {
  109. for (Map<ID, Element>::Element *J = I->next(); J; J = J->next()) {
  110. Element *elem_A = &I->get();
  111. Element *elem_B = &J->get();
  112. if (elem_A->owner == elem_B->owner)
  113. continue;
  114. bool pair_ok = elem_A->aabb.intersects(elem_B->aabb) && (!elem_A->_static || !elem_B->_static);
  115. PairKey key(I->key(), J->key());
  116. Map<PairKey, void *>::Element *E = pair_map.find(key);
  117. if (!pair_ok && E) {
  118. if (unpair_callback)
  119. unpair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, E->get(), unpair_userdata);
  120. pair_map.erase(key);
  121. }
  122. if (pair_ok && !E) {
  123. void *data = NULL;
  124. if (pair_callback)
  125. data = pair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, unpair_userdata);
  126. pair_map.insert(key, data);
  127. }
  128. }
  129. }
  130. }
  131. BroadPhase2DSW *BroadPhase2DBasic::_create() {
  132. return memnew(BroadPhase2DBasic);
  133. }
  134. BroadPhase2DBasic::BroadPhase2DBasic() {
  135. current = 1;
  136. unpair_callback = NULL;
  137. unpair_userdata = NULL;
  138. pair_callback = NULL;
  139. pair_userdata = NULL;
  140. }