godot_broad_phase_2d_bvh.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*************************************************************************/
  2. /* godot_broad_phase_2d_bvh.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 "godot_broad_phase_2d_bvh.h"
  31. #include "godot_collision_object_2d.h"
  32. GodotBroadPhase2D::ID GodotBroadPhase2DBVH::create(GodotCollisionObject2D *p_object, int p_subindex, const Rect2 &p_aabb, bool p_static) {
  33. 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?
  34. return oid + 1;
  35. }
  36. void GodotBroadPhase2DBVH::move(ID p_id, const Rect2 &p_aabb) {
  37. bvh.move(p_id - 1, p_aabb);
  38. }
  39. void GodotBroadPhase2DBVH::set_static(ID p_id, bool p_static) {
  40. GodotCollisionObject2D *it = bvh.get(p_id - 1);
  41. bvh.set_pairable(p_id - 1, !p_static, 1 << it->get_type(), p_static ? 0 : 0xFFFFF, false); // Pair everything, don't care?
  42. }
  43. void GodotBroadPhase2DBVH::remove(ID p_id) {
  44. bvh.erase(p_id - 1);
  45. }
  46. GodotCollisionObject2D *GodotBroadPhase2DBVH::get_object(ID p_id) const {
  47. GodotCollisionObject2D *it = bvh.get(p_id - 1);
  48. ERR_FAIL_COND_V(!it, nullptr);
  49. return it;
  50. }
  51. bool GodotBroadPhase2DBVH::is_static(ID p_id) const {
  52. return !bvh.is_pairable(p_id - 1);
  53. }
  54. int GodotBroadPhase2DBVH::get_subindex(ID p_id) const {
  55. return bvh.get_subindex(p_id - 1);
  56. }
  57. int GodotBroadPhase2DBVH::cull_segment(const Vector2 &p_from, const Vector2 &p_to, GodotCollisionObject2D **p_results, int p_max_results, int *p_result_indices) {
  58. return bvh.cull_segment(p_from, p_to, p_results, p_max_results, p_result_indices);
  59. }
  60. int GodotBroadPhase2DBVH::cull_aabb(const Rect2 &p_aabb, GodotCollisionObject2D **p_results, int p_max_results, int *p_result_indices) {
  61. return bvh.cull_aabb(p_aabb, p_results, p_max_results, p_result_indices);
  62. }
  63. void *GodotBroadPhase2DBVH::_pair_callback(void *self, uint32_t p_A, GodotCollisionObject2D *p_object_A, int subindex_A, uint32_t p_B, GodotCollisionObject2D *p_object_B, int subindex_B) {
  64. GodotBroadPhase2DBVH *bpo = (GodotBroadPhase2DBVH *)(self);
  65. if (!bpo->pair_callback) {
  66. return nullptr;
  67. }
  68. return bpo->pair_callback(p_object_A, subindex_A, p_object_B, subindex_B, bpo->pair_userdata);
  69. }
  70. void GodotBroadPhase2DBVH::_unpair_callback(void *self, uint32_t p_A, GodotCollisionObject2D *p_object_A, int subindex_A, uint32_t p_B, GodotCollisionObject2D *p_object_B, int subindex_B, void *pairdata) {
  71. GodotBroadPhase2DBVH *bpo = (GodotBroadPhase2DBVH *)(self);
  72. if (!bpo->unpair_callback) {
  73. return;
  74. }
  75. bpo->unpair_callback(p_object_A, subindex_A, p_object_B, subindex_B, pairdata, bpo->unpair_userdata);
  76. }
  77. void GodotBroadPhase2DBVH::set_pair_callback(PairCallback p_pair_callback, void *p_userdata) {
  78. pair_callback = p_pair_callback;
  79. pair_userdata = p_userdata;
  80. }
  81. void GodotBroadPhase2DBVH::set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata) {
  82. unpair_callback = p_unpair_callback;
  83. unpair_userdata = p_userdata;
  84. }
  85. void GodotBroadPhase2DBVH::update() {
  86. bvh.update();
  87. }
  88. GodotBroadPhase2D *GodotBroadPhase2DBVH::_create() {
  89. return memnew(GodotBroadPhase2DBVH);
  90. }
  91. GodotBroadPhase2DBVH::GodotBroadPhase2DBVH() {
  92. bvh.set_pair_callback(_pair_callback, this);
  93. bvh.set_unpair_callback(_unpair_callback, this);
  94. }