broad_phase_2d_bvh.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*************************************************************************/
  2. /* broad_phase_2d_bvh.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_bvh.h"
  31. #include "collision_object_2d_sw.h"
  32. #include "core/project_settings.h"
  33. BroadPhase2DSW::ID BroadPhase2DBVH::create(CollisionObject2DSW *p_object, int p_subindex, const Rect2 &p_aabb, bool p_static) {
  34. ID oid = bvh.create(p_object, true, p_aabb, p_subindex, !p_static, 1 << p_object->get_type(), p_static ? 0 : 0xFFFFF); // Pair everything, don't care?
  35. return oid + 1;
  36. }
  37. void BroadPhase2DBVH::move(ID p_id, const Rect2 &p_aabb) {
  38. bvh.move(p_id - 1, p_aabb);
  39. }
  40. void BroadPhase2DBVH::recheck_pairs(ID p_id) {
  41. bvh.recheck_pairs(p_id - 1);
  42. }
  43. void BroadPhase2DBVH::set_static(ID p_id, bool p_static) {
  44. CollisionObject2DSW *it = bvh.get(p_id - 1);
  45. bvh.set_pairable(p_id - 1, !p_static, 1 << it->get_type(), p_static ? 0 : 0xFFFFF, false); // Pair everything, don't care?
  46. }
  47. void BroadPhase2DBVH::remove(ID p_id) {
  48. bvh.erase(p_id - 1);
  49. }
  50. CollisionObject2DSW *BroadPhase2DBVH::get_object(ID p_id) const {
  51. CollisionObject2DSW *it = bvh.get(p_id - 1);
  52. ERR_FAIL_COND_V(!it, nullptr);
  53. return it;
  54. }
  55. bool BroadPhase2DBVH::is_static(ID p_id) const {
  56. return !bvh.is_pairable(p_id - 1);
  57. }
  58. int BroadPhase2DBVH::get_subindex(ID p_id) const {
  59. return bvh.get_subindex(p_id - 1);
  60. }
  61. int BroadPhase2DBVH::cull_segment(const Vector2 &p_from, const Vector2 &p_to, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices) {
  62. return bvh.cull_segment(p_from, p_to, p_results, p_max_results, p_result_indices);
  63. }
  64. int BroadPhase2DBVH::cull_aabb(const Rect2 &p_aabb, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices) {
  65. return bvh.cull_aabb(p_aabb, p_results, p_max_results, p_result_indices);
  66. }
  67. void *BroadPhase2DBVH::_pair_callback(void *p_self, uint32_t p_id_A, CollisionObject2DSW *p_object_A, int p_subindex_A, uint32_t p_id_B, CollisionObject2DSW *p_object_B, int p_subindex_B) {
  68. BroadPhase2DBVH *bpo = (BroadPhase2DBVH *)(p_self);
  69. if (!bpo->pair_callback) {
  70. return nullptr;
  71. }
  72. return bpo->pair_callback(p_object_A, p_subindex_A, p_object_B, p_subindex_B, nullptr, bpo->pair_userdata);
  73. }
  74. void BroadPhase2DBVH::_unpair_callback(void *p_self, uint32_t p_id_A, CollisionObject2DSW *p_object_A, int p_subindex_A, uint32_t p_id_B, CollisionObject2DSW *p_object_B, int p_subindex_B, void *p_pair_data) {
  75. BroadPhase2DBVH *bpo = (BroadPhase2DBVH *)(p_self);
  76. if (!bpo->unpair_callback) {
  77. return;
  78. }
  79. bpo->unpair_callback(p_object_A, p_subindex_A, p_object_B, p_subindex_B, p_pair_data, bpo->unpair_userdata);
  80. }
  81. void *BroadPhase2DBVH::_check_pair_callback(void *p_self, uint32_t p_id_A, CollisionObject2DSW *p_object_A, int p_subindex_A, uint32_t p_id_B, CollisionObject2DSW *p_object_B, int p_subindex_B, void *p_pair_data) {
  82. BroadPhase2DBVH *bpo = (BroadPhase2DBVH *)(p_self);
  83. if (!bpo->pair_callback) {
  84. return nullptr;
  85. }
  86. return bpo->pair_callback(p_object_A, p_subindex_A, p_object_B, p_subindex_B, p_pair_data, bpo->pair_userdata);
  87. }
  88. void BroadPhase2DBVH::set_pair_callback(PairCallback p_pair_callback, void *p_userdata) {
  89. pair_callback = p_pair_callback;
  90. pair_userdata = p_userdata;
  91. }
  92. void BroadPhase2DBVH::set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata) {
  93. unpair_callback = p_unpair_callback;
  94. unpair_userdata = p_userdata;
  95. }
  96. void BroadPhase2DBVH::update() {
  97. bvh.update();
  98. }
  99. BroadPhase2DSW *BroadPhase2DBVH::_create() {
  100. return memnew(BroadPhase2DBVH);
  101. }
  102. BroadPhase2DBVH::BroadPhase2DBVH() {
  103. bvh.params_set_thread_safe(GLOBAL_GET("rendering/threads/thread_safe_bvh"));
  104. bvh.params_set_pairing_expansion(GLOBAL_GET("physics/2d/bvh_collision_margin"));
  105. bvh.set_pair_callback(_pair_callback, this);
  106. bvh.set_unpair_callback(_unpair_callback, this);
  107. bvh.set_check_pair_callback(_check_pair_callback, this);
  108. pair_callback = nullptr;
  109. pair_userdata = nullptr;
  110. unpair_userdata = nullptr;
  111. }