broad_phase_2d_hash_grid.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*************************************************************************/
  2. /* broad_phase_2d_hash_grid.h */
  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. #ifndef BROAD_PHASE_2D_HASH_GRID_H
  31. #define BROAD_PHASE_2D_HASH_GRID_H
  32. #include "broad_phase_2d_sw.h"
  33. #include "core/map.h"
  34. class BroadPhase2DHashGrid : public BroadPhase2DSW {
  35. struct PairData {
  36. bool colliding;
  37. int rc;
  38. void *ud;
  39. PairData() {
  40. colliding = false;
  41. rc = 1;
  42. ud = nullptr;
  43. }
  44. };
  45. struct Element {
  46. ID self;
  47. CollisionObject2DSW *owner;
  48. bool _static;
  49. Rect2 aabb;
  50. // Owner's collision_mask/layer, used to detect changes in layers.
  51. uint32_t collision_mask;
  52. uint32_t collision_layer;
  53. int subindex;
  54. uint64_t pass;
  55. Map<Element *, PairData *> paired;
  56. };
  57. struct RC {
  58. int ref;
  59. _FORCE_INLINE_ int inc() {
  60. ref++;
  61. return ref;
  62. }
  63. _FORCE_INLINE_ int dec() {
  64. ref--;
  65. return ref;
  66. }
  67. _FORCE_INLINE_ RC() {
  68. ref = 0;
  69. }
  70. };
  71. Map<ID, Element> element_map;
  72. Map<Element *, RC> large_elements;
  73. ID current;
  74. uint64_t pass;
  75. struct PairKey {
  76. union {
  77. struct {
  78. ID a;
  79. ID b;
  80. };
  81. uint64_t key;
  82. };
  83. _FORCE_INLINE_ bool operator<(const PairKey &p_key) const {
  84. return key < p_key.key;
  85. }
  86. PairKey() { key = 0; }
  87. PairKey(ID p_a, ID p_b) {
  88. if (p_a > p_b) {
  89. a = p_b;
  90. b = p_a;
  91. } else {
  92. a = p_a;
  93. b = p_b;
  94. }
  95. }
  96. };
  97. Map<PairKey, PairData> pair_map;
  98. int cell_size;
  99. int large_object_min_surface;
  100. PairCallback pair_callback;
  101. void *pair_userdata;
  102. UnpairCallback unpair_callback;
  103. void *unpair_userdata;
  104. static _FORCE_INLINE_ bool _test_collision_mask(uint32_t p_mask1, uint32_t p_layer1, uint32_t p_mask2, uint32_t p_layer2) {
  105. return p_mask1 & p_layer2 || p_mask2 & p_layer1;
  106. }
  107. void _enter_grid(Element *p_elem, const Rect2 &p_rect, bool p_static, bool p_force_enter);
  108. void _exit_grid(Element *p_elem, const Rect2 &p_rect, bool p_static, bool p_force_exit);
  109. template <bool use_aabb, bool use_segment>
  110. _FORCE_INLINE_ void _cull(const Point2i p_cell, const Rect2 &p_aabb, const Point2 &p_from, const Point2 &p_to, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices, int &index);
  111. struct PosKey {
  112. union {
  113. struct {
  114. int32_t x;
  115. int32_t y;
  116. };
  117. uint64_t key;
  118. };
  119. _FORCE_INLINE_ uint32_t hash() const {
  120. uint64_t k = key;
  121. k = (~k) + (k << 18); // k = (k << 18) - k - 1;
  122. k = k ^ (k >> 31);
  123. k = k * 21; // k = (k + (k << 2)) + (k << 4);
  124. k = k ^ (k >> 11);
  125. k = k + (k << 6);
  126. k = k ^ (k >> 22);
  127. return k;
  128. }
  129. bool operator==(const PosKey &p_key) const { return key == p_key.key; }
  130. _FORCE_INLINE_ bool operator<(const PosKey &p_key) const {
  131. return key < p_key.key;
  132. }
  133. };
  134. struct PosBin {
  135. PosKey key;
  136. Map<Element *, RC> object_set;
  137. Map<Element *, RC> static_object_set;
  138. PosBin *next;
  139. };
  140. uint32_t hash_table_size;
  141. PosBin **hash_table;
  142. void _pair_attempt(Element *p_elem, Element *p_with);
  143. void _unpair_attempt(Element *p_elem, Element *p_with);
  144. void _move_internal(Element *p_elem, const Rect2 &p_aabb);
  145. void _check_motion(Element *p_elem);
  146. public:
  147. virtual ID create(CollisionObject2DSW *p_object, int p_subindex = 0, const Rect2 &p_aabb = Rect2(), bool p_static = false);
  148. virtual void move(ID p_id, const Rect2 &p_aabb);
  149. virtual void recheck_pairs(ID p_id);
  150. virtual void set_static(ID p_id, bool p_static);
  151. virtual void remove(ID p_id);
  152. virtual CollisionObject2DSW *get_object(ID p_id) const;
  153. virtual bool is_static(ID p_id) const;
  154. virtual int get_subindex(ID p_id) const;
  155. virtual int cull_segment(const Vector2 &p_from, const Vector2 &p_to, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices = nullptr);
  156. virtual int cull_aabb(const Rect2 &p_aabb, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices = nullptr);
  157. virtual void set_pair_callback(PairCallback p_pair_callback, void *p_userdata);
  158. virtual void set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata);
  159. virtual void update();
  160. static BroadPhase2DSW *_create();
  161. BroadPhase2DHashGrid();
  162. ~BroadPhase2DHashGrid();
  163. };
  164. #endif // BROAD_PHASE_2D_HASH_GRID_H