broad_phase_basic.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*************************************************************************/
  2. /* broad_phase_basic.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "broad_phase_basic.h"
  30. #include "list.h"
  31. #include "print_string.h"
  32. BroadPhaseSW::ID BroadPhaseBasic::create(CollisionObjectSW *p_object_, int p_subindex) {
  33. if (p_object_==NULL) {
  34. ERR_FAIL_COND_V(p_object_==NULL,0);
  35. }
  36. current++;
  37. Element e;
  38. e.owner=p_object_;
  39. e._static=false;
  40. e.subindex=p_subindex;
  41. element_map[current]=e;
  42. return current;
  43. }
  44. void BroadPhaseBasic::move(ID p_id, const AABB& p_aabb) {
  45. Map<ID,Element>::Element *E=element_map.find(p_id);
  46. ERR_FAIL_COND(!E);
  47. E->get().aabb=p_aabb;
  48. }
  49. void BroadPhaseBasic::set_static(ID p_id, bool p_static) {
  50. Map<ID,Element>::Element *E=element_map.find(p_id);
  51. ERR_FAIL_COND(!E);
  52. E->get()._static=p_static;
  53. }
  54. void BroadPhaseBasic::remove(ID p_id) {
  55. Map<ID,Element>::Element *E=element_map.find(p_id);
  56. ERR_FAIL_COND(!E);
  57. List<PairKey> to_erase;
  58. //unpair must be done immediately on removal to avoid potential invalid pointers
  59. for (Map<PairKey,void*>::Element *F=pair_map.front();F;F=F->next()) {
  60. if (F->key().a==p_id || F->key().b==p_id) {
  61. if (unpair_callback) {
  62. Element *elem_A=&element_map[F->key().a];
  63. Element *elem_B=&element_map[F->key().b];
  64. unpair_callback(elem_A->owner,elem_A->subindex,elem_B->owner,elem_B->subindex,F->get(),unpair_userdata);
  65. }
  66. to_erase.push_back(F->key());
  67. }
  68. }
  69. while(to_erase.size()) {
  70. pair_map.erase(to_erase.front()->get());
  71. to_erase.pop_front();
  72. }
  73. element_map.erase(E);
  74. }
  75. CollisionObjectSW *BroadPhaseBasic::get_object(ID p_id) const {
  76. const Map<ID,Element>::Element *E=element_map.find(p_id);
  77. ERR_FAIL_COND_V(!E,NULL);
  78. return E->get().owner;
  79. }
  80. bool BroadPhaseBasic::is_static(ID p_id) const {
  81. const Map<ID,Element>::Element *E=element_map.find(p_id);
  82. ERR_FAIL_COND_V(!E,false);
  83. return E->get()._static;
  84. }
  85. int BroadPhaseBasic::get_subindex(ID p_id) const {
  86. const Map<ID,Element>::Element *E=element_map.find(p_id);
  87. ERR_FAIL_COND_V(!E,-1);
  88. return E->get().subindex;
  89. }
  90. int BroadPhaseBasic::cull_segment(const Vector3& p_from, const Vector3& p_to,CollisionObjectSW** p_results,int p_max_results,int *p_result_indices) {
  91. int rc=0;
  92. for (Map<ID,Element>::Element *E=element_map.front();E;E=E->next()) {
  93. const AABB aabb=E->get().aabb;
  94. if (aabb.intersects_segment(p_from,p_to)) {
  95. p_results[rc]=E->get().owner;
  96. p_result_indices[rc]=E->get().subindex;
  97. rc++;
  98. if (rc>=p_max_results)
  99. break;
  100. }
  101. }
  102. return rc;
  103. }
  104. int BroadPhaseBasic::cull_aabb(const AABB& p_aabb,CollisionObjectSW** 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(p_aabb)) {
  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. return rc;
  117. }
  118. void BroadPhaseBasic::set_pair_callback(PairCallback p_pair_callback,void *p_userdata) {
  119. pair_userdata=p_userdata;
  120. pair_callback=p_pair_callback;
  121. }
  122. void BroadPhaseBasic::set_unpair_callback(UnpairCallback p_pair_callback,void *p_userdata) {
  123. unpair_userdata=p_userdata;
  124. unpair_callback=p_pair_callback;
  125. }
  126. void BroadPhaseBasic::update() {
  127. // recompute pairs
  128. for(Map<ID,Element>::Element *I=element_map.front();I;I=I->next()) {
  129. for(Map<ID,Element>::Element *J=I->next();J;J=J->next()) {
  130. Element *elem_A=&I->get();
  131. Element *elem_B=&J->get();
  132. if (elem_A->owner == elem_B->owner)
  133. continue;
  134. bool pair_ok=elem_A->aabb.intersects( elem_B->aabb ) && (!elem_A->_static || !elem_B->_static );
  135. PairKey key(I->key(),J->key());
  136. Map<PairKey,void*>::Element *E=pair_map.find(key);
  137. if (!pair_ok && E) {
  138. if (unpair_callback)
  139. unpair_callback(elem_A->owner,elem_A->subindex,elem_B->owner,elem_B->subindex,E->get(),unpair_userdata);
  140. pair_map.erase(key);
  141. }
  142. if (pair_ok && !E) {
  143. void *data=NULL;
  144. if (pair_callback)
  145. data=pair_callback(elem_A->owner,elem_A->subindex,elem_B->owner,elem_B->subindex,unpair_userdata);
  146. pair_map.insert(key,data);
  147. }
  148. }
  149. }
  150. }
  151. BroadPhaseSW *BroadPhaseBasic::_create() {
  152. return memnew( BroadPhaseBasic );
  153. }
  154. BroadPhaseBasic::BroadPhaseBasic() {
  155. current=1;
  156. unpair_callback=NULL;
  157. unpair_userdata=NULL;
  158. pair_callback=NULL;
  159. pair_userdata=NULL;
  160. }