broad_phase_2d_hash_grid.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 = NULL;
  43. }
  44. };
  45. struct Element {
  46. ID self;
  47. CollisionObject2DSW *owner;
  48. bool _static;
  49. Rect2 aabb;
  50. int subindex;
  51. uint64_t pass;
  52. Map<Element *, PairData *> paired;
  53. };
  54. struct RC {
  55. int ref;
  56. _FORCE_INLINE_ int inc() {
  57. ref++;
  58. return ref;
  59. }
  60. _FORCE_INLINE_ int dec() {
  61. ref--;
  62. return ref;
  63. }
  64. _FORCE_INLINE_ RC() {
  65. ref = 0;
  66. }
  67. };
  68. Map<ID, Element> element_map;
  69. Map<Element *, RC> large_elements;
  70. ID current;
  71. uint64_t pass;
  72. struct PairKey {
  73. union {
  74. struct {
  75. ID a;
  76. ID b;
  77. };
  78. uint64_t key;
  79. };
  80. _FORCE_INLINE_ bool operator<(const PairKey &p_key) const {
  81. return key < p_key.key;
  82. }
  83. PairKey() { key = 0; }
  84. PairKey(ID p_a, ID p_b) {
  85. if (p_a > p_b) {
  86. a = p_b;
  87. b = p_a;
  88. } else {
  89. a = p_a;
  90. b = p_b;
  91. }
  92. }
  93. };
  94. Map<PairKey, PairData> pair_map;
  95. int cell_size;
  96. int large_object_min_surface;
  97. PairCallback pair_callback;
  98. void *pair_userdata;
  99. UnpairCallback unpair_callback;
  100. void *unpair_userdata;
  101. void _enter_grid(Element *p_elem, const Rect2 &p_rect, bool p_static);
  102. void _exit_grid(Element *p_elem, const Rect2 &p_rect, bool p_static);
  103. template <bool use_aabb, bool use_segment>
  104. _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);
  105. struct PosKey {
  106. union {
  107. struct {
  108. int32_t x;
  109. int32_t y;
  110. };
  111. uint64_t key;
  112. };
  113. _FORCE_INLINE_ uint32_t hash() const {
  114. uint64_t k = key;
  115. k = (~k) + (k << 18); // k = (k << 18) - k - 1;
  116. k = k ^ (k >> 31);
  117. k = k * 21; // k = (k + (k << 2)) + (k << 4);
  118. k = k ^ (k >> 11);
  119. k = k + (k << 6);
  120. k = k ^ (k >> 22);
  121. return k;
  122. }
  123. bool operator==(const PosKey &p_key) const { return key == p_key.key; }
  124. _FORCE_INLINE_ bool operator<(const PosKey &p_key) const {
  125. return key < p_key.key;
  126. }
  127. };
  128. struct PosBin {
  129. PosKey key;
  130. Map<Element *, RC> object_set;
  131. Map<Element *, RC> static_object_set;
  132. PosBin *next;
  133. };
  134. uint32_t hash_table_size;
  135. PosBin **hash_table;
  136. void _pair_attempt(Element *p_elem, Element *p_with);
  137. void _unpair_attempt(Element *p_elem, Element *p_with);
  138. void _check_motion(Element *p_elem);
  139. public:
  140. virtual ID create(CollisionObject2DSW *p_object, int p_subindex = 0);
  141. virtual void move(ID p_id, const Rect2 &p_aabb);
  142. virtual void set_static(ID p_id, bool p_static);
  143. virtual void remove(ID p_id);
  144. virtual CollisionObject2DSW *get_object(ID p_id) const;
  145. virtual bool is_static(ID p_id) const;
  146. virtual int get_subindex(ID p_id) const;
  147. virtual int cull_segment(const Vector2 &p_from, const Vector2 &p_to, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices = NULL);
  148. virtual int cull_aabb(const Rect2 &p_aabb, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices = NULL);
  149. virtual void set_pair_callback(PairCallback p_pair_callback, void *p_userdata);
  150. virtual void set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata);
  151. virtual void update();
  152. static BroadPhase2DSW *_create();
  153. BroadPhase2DHashGrid();
  154. ~BroadPhase2DHashGrid();
  155. };
  156. #endif // BROAD_PHASE_2D_HASH_GRID_H