broad_phase_octree.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*************************************************************************/
  2. /* broad_phase_octree.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_octree.h"
  31. #include "collision_object_sw.h"
  32. BroadPhaseSW::ID BroadPhaseOctree::create(CollisionObjectSW *p_object, int p_subindex, const AABB &p_aabb, bool p_static) {
  33. ID oid = octree.create(p_object, AABB(), p_subindex, false, 1 << p_object->get_type(), 0);
  34. return oid;
  35. }
  36. void BroadPhaseOctree::move(ID p_id, const AABB &p_aabb) {
  37. octree.move(p_id, p_aabb);
  38. }
  39. void BroadPhaseOctree::recheck_pairs(ID p_id) {
  40. AABB aabb = octree.get_aabb(p_id);
  41. octree.move(p_id, aabb);
  42. }
  43. void BroadPhaseOctree::set_static(ID p_id, bool p_static) {
  44. CollisionObjectSW *it = octree.get(p_id);
  45. octree.set_pairable(p_id, !p_static, 1 << it->get_type(), p_static ? 0 : 0xFFFFF); //pair everything, don't care 1?
  46. }
  47. void BroadPhaseOctree::remove(ID p_id) {
  48. octree.erase(p_id);
  49. }
  50. CollisionObjectSW *BroadPhaseOctree::get_object(ID p_id) const {
  51. CollisionObjectSW *it = octree.get(p_id);
  52. ERR_FAIL_COND_V(!it, nullptr);
  53. return it;
  54. }
  55. bool BroadPhaseOctree::is_static(ID p_id) const {
  56. return !octree.is_pairable(p_id);
  57. }
  58. int BroadPhaseOctree::get_subindex(ID p_id) const {
  59. return octree.get_subindex(p_id);
  60. }
  61. int BroadPhaseOctree::cull_point(const Vector3 &p_point, CollisionObjectSW **p_results, int p_max_results, int *p_result_indices) {
  62. return octree.cull_point(p_point, p_results, p_max_results, p_result_indices);
  63. }
  64. int BroadPhaseOctree::cull_segment(const Vector3 &p_from, const Vector3 &p_to, CollisionObjectSW **p_results, int p_max_results, int *p_result_indices) {
  65. return octree.cull_segment(p_from, p_to, p_results, p_max_results, p_result_indices);
  66. }
  67. int BroadPhaseOctree::cull_aabb(const AABB &p_aabb, CollisionObjectSW **p_results, int p_max_results, int *p_result_indices) {
  68. return octree.cull_aabb(p_aabb, p_results, p_max_results, p_result_indices);
  69. }
  70. void *BroadPhaseOctree::_pair_callback(void *self, OctreeElementID p_A, CollisionObjectSW *p_object_A, int subindex_A, OctreeElementID p_B, CollisionObjectSW *p_object_B, int subindex_B) {
  71. BroadPhaseOctree *bpo = (BroadPhaseOctree *)(self);
  72. if (!bpo->pair_callback) {
  73. return nullptr;
  74. }
  75. return bpo->pair_callback(p_object_A, subindex_A, p_object_B, subindex_B, nullptr, bpo->pair_userdata);
  76. }
  77. void BroadPhaseOctree::_unpair_callback(void *self, OctreeElementID p_A, CollisionObjectSW *p_object_A, int subindex_A, OctreeElementID p_B, CollisionObjectSW *p_object_B, int subindex_B, void *pairdata) {
  78. BroadPhaseOctree *bpo = (BroadPhaseOctree *)(self);
  79. if (!bpo->unpair_callback) {
  80. return;
  81. }
  82. bpo->unpair_callback(p_object_A, subindex_A, p_object_B, subindex_B, pairdata, bpo->unpair_userdata);
  83. }
  84. void BroadPhaseOctree::set_pair_callback(PairCallback p_pair_callback, void *p_userdata) {
  85. pair_callback = p_pair_callback;
  86. pair_userdata = p_userdata;
  87. }
  88. void BroadPhaseOctree::set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata) {
  89. unpair_callback = p_unpair_callback;
  90. unpair_userdata = p_userdata;
  91. }
  92. void BroadPhaseOctree::update() {
  93. // does.. not?
  94. }
  95. BroadPhaseSW *BroadPhaseOctree::_create() {
  96. return memnew(BroadPhaseOctree);
  97. }
  98. BroadPhaseOctree::BroadPhaseOctree() {
  99. octree.set_pair_callback(_pair_callback, this);
  100. octree.set_unpair_callback(_unpair_callback, this);
  101. pair_callback = nullptr;
  102. pair_userdata = nullptr;
  103. unpair_userdata = nullptr;
  104. }