broad_phase_2d_hash_grid.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /**************************************************************************/
  2. /* broad_phase_2d_hash_grid.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_hash_grid.h"
  31. #include "collision_object_2d_sw.h"
  32. #include "core/project_settings.h"
  33. #define LARGE_ELEMENT_FI 1.01239812
  34. void BroadPhase2DHashGrid::_pair_attempt(Element *p_elem, Element *p_with) {
  35. if (p_elem->owner == p_with->owner) {
  36. return;
  37. }
  38. if (!_test_collision_mask(p_elem->collision_mask, p_elem->collision_layer, p_with->collision_mask, p_with->collision_layer)) {
  39. return;
  40. }
  41. Map<Element *, PairData *>::Element *E = p_elem->paired.find(p_with);
  42. ERR_FAIL_COND(p_elem->_static && p_with->_static);
  43. if (!E) {
  44. PairData *pd = memnew(PairData);
  45. p_elem->paired[p_with] = pd;
  46. p_with->paired[p_elem] = pd;
  47. } else {
  48. E->get()->rc++;
  49. }
  50. }
  51. void BroadPhase2DHashGrid::_unpair_attempt(Element *p_elem, Element *p_with) {
  52. if (p_elem->owner == p_with->owner) {
  53. return;
  54. }
  55. if (!_test_collision_mask(p_elem->collision_mask, p_elem->collision_layer, p_with->collision_mask, p_with->collision_layer)) {
  56. return;
  57. }
  58. Map<Element *, PairData *>::Element *E = p_elem->paired.find(p_with);
  59. ERR_FAIL_COND(!E); //this should really be paired..
  60. E->get()->rc--;
  61. if (E->get()->rc == 0) {
  62. if (E->get()->colliding) {
  63. //uncollide
  64. if (unpair_callback) {
  65. unpair_callback(p_elem->owner, p_elem->subindex, p_with->owner, p_with->subindex, E->get()->ud, unpair_userdata);
  66. }
  67. }
  68. memdelete(E->get());
  69. p_elem->paired.erase(E);
  70. p_with->paired.erase(p_elem);
  71. }
  72. }
  73. void BroadPhase2DHashGrid::_check_motion(Element *p_elem) {
  74. for (Map<Element *, PairData *>::Element *E = p_elem->paired.front(); E; E = E->next()) {
  75. bool physical_collision = p_elem->aabb.intersects(E->key()->aabb);
  76. bool logical_collision = p_elem->owner->test_collision_mask(E->key()->owner);
  77. if (physical_collision && logical_collision) {
  78. if (!E->get()->colliding && pair_callback) {
  79. E->get()->ud = pair_callback(p_elem->owner, p_elem->subindex, E->key()->owner, E->key()->subindex, nullptr, pair_userdata);
  80. }
  81. E->get()->colliding = true;
  82. } else { // No collision
  83. if (E->get()->colliding && unpair_callback) {
  84. unpair_callback(p_elem->owner, p_elem->subindex, E->key()->owner, E->key()->subindex, E->get()->ud, unpair_userdata);
  85. E->get()->ud = nullptr;
  86. }
  87. E->get()->colliding = false;
  88. }
  89. }
  90. }
  91. void BroadPhase2DHashGrid::_enter_grid(Element *p_elem, const Rect2 &p_rect, bool p_static, bool p_force_enter) {
  92. Vector2 sz = (p_rect.size / cell_size * LARGE_ELEMENT_FI); //use magic number to avoid floating point issues
  93. if (sz.width * sz.height > large_object_min_surface) {
  94. //large object, do not use grid, must check against all elements
  95. for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
  96. if (E->key() == p_elem->self) {
  97. continue; // do not pair against itself
  98. }
  99. if (E->get()._static && p_static) {
  100. continue;
  101. }
  102. _pair_attempt(p_elem, &E->get());
  103. }
  104. large_elements[p_elem].inc();
  105. return;
  106. }
  107. Point2i from = (p_rect.position / cell_size).floor();
  108. Point2i to = ((p_rect.position + p_rect.size) / cell_size).floor();
  109. for (int i = from.x; i <= to.x; i++) {
  110. for (int j = from.y; j <= to.y; j++) {
  111. PosKey pk;
  112. pk.x = i;
  113. pk.y = j;
  114. uint32_t idx = pk.hash() % hash_table_size;
  115. PosBin *pb = hash_table[idx];
  116. while (pb) {
  117. if (pb->key == pk) {
  118. break;
  119. }
  120. pb = pb->next;
  121. }
  122. bool entered = p_force_enter;
  123. if (!pb) {
  124. //does not exist, create!
  125. pb = memnew(PosBin);
  126. pb->key = pk;
  127. pb->next = hash_table[idx];
  128. hash_table[idx] = pb;
  129. }
  130. if (p_static) {
  131. if (pb->static_object_set[p_elem].inc() == 1) {
  132. entered = true;
  133. }
  134. } else {
  135. if (pb->object_set[p_elem].inc() == 1) {
  136. entered = true;
  137. }
  138. }
  139. if (entered) {
  140. for (Map<Element *, RC>::Element *E = pb->object_set.front(); E; E = E->next()) {
  141. _pair_attempt(p_elem, E->key());
  142. }
  143. if (!p_static) {
  144. for (Map<Element *, RC>::Element *E = pb->static_object_set.front(); E; E = E->next()) {
  145. _pair_attempt(p_elem, E->key());
  146. }
  147. }
  148. }
  149. }
  150. }
  151. //pair separatedly with large elements
  152. for (Map<Element *, RC>::Element *E = large_elements.front(); E; E = E->next()) {
  153. if (E->key() == p_elem) {
  154. continue; // do not pair against itself
  155. }
  156. if (E->key()->_static && p_static) {
  157. continue;
  158. }
  159. _pair_attempt(E->key(), p_elem);
  160. }
  161. }
  162. void BroadPhase2DHashGrid::_exit_grid(Element *p_elem, const Rect2 &p_rect, bool p_static, bool p_force_exit) {
  163. Vector2 sz = (p_rect.size / cell_size * LARGE_ELEMENT_FI);
  164. if (sz.width * sz.height > large_object_min_surface) {
  165. //unpair all elements, instead of checking all, just check what is already paired, so we at least save from checking static vs static
  166. Map<Element *, PairData *>::Element *E = p_elem->paired.front();
  167. while (E) {
  168. Map<Element *, PairData *>::Element *next = E->next();
  169. _unpair_attempt(p_elem, E->key());
  170. E = next;
  171. }
  172. if (large_elements[p_elem].dec() == 0) {
  173. large_elements.erase(p_elem);
  174. }
  175. return;
  176. }
  177. Point2i from = (p_rect.position / cell_size).floor();
  178. Point2i to = ((p_rect.position + p_rect.size) / cell_size).floor();
  179. for (int i = from.x; i <= to.x; i++) {
  180. for (int j = from.y; j <= to.y; j++) {
  181. PosKey pk;
  182. pk.x = i;
  183. pk.y = j;
  184. uint32_t idx = pk.hash() % hash_table_size;
  185. PosBin *pb = hash_table[idx];
  186. while (pb) {
  187. if (pb->key == pk) {
  188. break;
  189. }
  190. pb = pb->next;
  191. }
  192. ERR_CONTINUE(!pb); //should exist!!
  193. bool exited = p_force_exit;
  194. if (p_static) {
  195. if (pb->static_object_set[p_elem].dec() == 0) {
  196. pb->static_object_set.erase(p_elem);
  197. exited = true;
  198. }
  199. } else {
  200. if (pb->object_set[p_elem].dec() == 0) {
  201. pb->object_set.erase(p_elem);
  202. exited = true;
  203. }
  204. }
  205. if (exited) {
  206. for (Map<Element *, RC>::Element *E = pb->object_set.front(); E; E = E->next()) {
  207. _unpair_attempt(p_elem, E->key());
  208. }
  209. if (!p_static) {
  210. for (Map<Element *, RC>::Element *E = pb->static_object_set.front(); E; E = E->next()) {
  211. _unpair_attempt(p_elem, E->key());
  212. }
  213. }
  214. }
  215. if (pb->object_set.empty() && pb->static_object_set.empty()) {
  216. if (hash_table[idx] == pb) {
  217. hash_table[idx] = pb->next;
  218. } else {
  219. PosBin *px = hash_table[idx];
  220. while (px) {
  221. if (px->next == pb) {
  222. px->next = pb->next;
  223. break;
  224. }
  225. px = px->next;
  226. }
  227. ERR_CONTINUE(!px);
  228. }
  229. memdelete(pb);
  230. }
  231. }
  232. }
  233. for (Map<Element *, RC>::Element *E = large_elements.front(); E; E = E->next()) {
  234. if (E->key() == p_elem) {
  235. continue; // do not pair against itself
  236. }
  237. if (E->key()->_static && p_static) {
  238. continue;
  239. }
  240. //unpair from large elements
  241. _unpair_attempt(p_elem, E->key());
  242. }
  243. }
  244. BroadPhase2DHashGrid::ID BroadPhase2DHashGrid::create(CollisionObject2DSW *p_object, int p_subindex, const Rect2 &p_aabb, bool p_static) {
  245. current++;
  246. Element e;
  247. e.owner = p_object;
  248. e._static = false;
  249. e.collision_mask = p_object->get_collision_mask();
  250. e.collision_layer = p_object->get_collision_layer();
  251. e.subindex = p_subindex;
  252. e.self = current;
  253. e.pass = 0;
  254. element_map[current] = e;
  255. return current;
  256. }
  257. void BroadPhase2DHashGrid::move(ID p_id, const Rect2 &p_aabb) {
  258. Map<ID, Element>::Element *E = element_map.find(p_id);
  259. ERR_FAIL_COND(!E);
  260. Element &e = E->get();
  261. bool layer_changed = e.collision_mask != e.owner->get_collision_mask() || e.collision_layer != e.owner->get_collision_layer();
  262. if (p_aabb != e.aabb || layer_changed) {
  263. uint32_t old_mask = e.collision_mask;
  264. uint32_t old_layer = e.collision_layer;
  265. if (p_aabb != Rect2()) {
  266. e.collision_mask = e.owner->get_collision_mask();
  267. e.collision_layer = e.owner->get_collision_layer();
  268. _enter_grid(&e, p_aabb, e._static, layer_changed);
  269. }
  270. if (e.aabb != Rect2()) {
  271. // Need _exit_grid to remove from cells based on the old layer values.
  272. e.collision_mask = old_mask;
  273. e.collision_layer = old_layer;
  274. _exit_grid(&e, e.aabb, e._static, layer_changed);
  275. e.collision_mask = e.owner->get_collision_mask();
  276. e.collision_layer = e.owner->get_collision_layer();
  277. }
  278. e.aabb = p_aabb;
  279. }
  280. _check_motion(&e);
  281. }
  282. void BroadPhase2DHashGrid::recheck_pairs(ID p_id) {
  283. Map<ID, Element>::Element *E = element_map.find(p_id);
  284. ERR_FAIL_COND(!E);
  285. Element &e = E->get();
  286. move(p_id, e.aabb);
  287. }
  288. void BroadPhase2DHashGrid::set_static(ID p_id, bool p_static) {
  289. Map<ID, Element>::Element *E = element_map.find(p_id);
  290. ERR_FAIL_COND(!E);
  291. Element &e = E->get();
  292. if (e._static == p_static) {
  293. return;
  294. }
  295. if (e.aabb != Rect2()) {
  296. _exit_grid(&e, e.aabb, e._static, false);
  297. }
  298. e._static = p_static;
  299. if (e.aabb != Rect2()) {
  300. _enter_grid(&e, e.aabb, e._static, false);
  301. _check_motion(&e);
  302. }
  303. }
  304. void BroadPhase2DHashGrid::remove(ID p_id) {
  305. Map<ID, Element>::Element *E = element_map.find(p_id);
  306. ERR_FAIL_COND(!E);
  307. Element &e = E->get();
  308. if (e.aabb != Rect2()) {
  309. _exit_grid(&e, e.aabb, e._static, false);
  310. }
  311. element_map.erase(p_id);
  312. }
  313. CollisionObject2DSW *BroadPhase2DHashGrid::get_object(ID p_id) const {
  314. const Map<ID, Element>::Element *E = element_map.find(p_id);
  315. ERR_FAIL_COND_V(!E, nullptr);
  316. return E->get().owner;
  317. }
  318. bool BroadPhase2DHashGrid::is_static(ID p_id) const {
  319. const Map<ID, Element>::Element *E = element_map.find(p_id);
  320. ERR_FAIL_COND_V(!E, false);
  321. return E->get()._static;
  322. }
  323. int BroadPhase2DHashGrid::get_subindex(ID p_id) const {
  324. const Map<ID, Element>::Element *E = element_map.find(p_id);
  325. ERR_FAIL_COND_V(!E, -1);
  326. return E->get().subindex;
  327. }
  328. template <bool use_aabb, bool use_segment>
  329. void BroadPhase2DHashGrid::_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) {
  330. PosKey pk;
  331. pk.x = p_cell.x;
  332. pk.y = p_cell.y;
  333. uint32_t idx = pk.hash() % hash_table_size;
  334. PosBin *pb = hash_table[idx];
  335. while (pb) {
  336. if (pb->key == pk) {
  337. break;
  338. }
  339. pb = pb->next;
  340. }
  341. if (!pb) {
  342. return;
  343. }
  344. for (Map<Element *, RC>::Element *E = pb->object_set.front(); E; E = E->next()) {
  345. if (index >= p_max_results) {
  346. break;
  347. }
  348. if (E->key()->pass == pass) {
  349. continue;
  350. }
  351. E->key()->pass = pass;
  352. if (use_aabb && !p_aabb.intersects(E->key()->aabb)) {
  353. continue;
  354. }
  355. if (use_segment && !E->key()->aabb.intersects_segment(p_from, p_to)) {
  356. continue;
  357. }
  358. p_results[index] = E->key()->owner;
  359. p_result_indices[index] = E->key()->subindex;
  360. index++;
  361. }
  362. for (Map<Element *, RC>::Element *E = pb->static_object_set.front(); E; E = E->next()) {
  363. if (index >= p_max_results) {
  364. break;
  365. }
  366. if (E->key()->pass == pass) {
  367. continue;
  368. }
  369. if (use_aabb && !p_aabb.intersects(E->key()->aabb)) {
  370. continue;
  371. }
  372. if (use_segment && !E->key()->aabb.intersects_segment(p_from, p_to)) {
  373. continue;
  374. }
  375. E->key()->pass = pass;
  376. p_results[index] = E->key()->owner;
  377. p_result_indices[index] = E->key()->subindex;
  378. index++;
  379. }
  380. }
  381. int BroadPhase2DHashGrid::cull_segment(const Vector2 &p_from, const Vector2 &p_to, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices) {
  382. pass++;
  383. Vector2 dir = (p_to - p_from);
  384. if (dir == Vector2()) {
  385. return 0;
  386. }
  387. //avoid divisions by zero
  388. dir.normalize();
  389. if (dir.x == 0.0) {
  390. dir.x = 0.000001;
  391. }
  392. if (dir.y == 0.0) {
  393. dir.y = 0.000001;
  394. }
  395. Vector2 delta = dir.abs();
  396. delta.x = cell_size / delta.x;
  397. delta.y = cell_size / delta.y;
  398. Point2i pos = (p_from / cell_size).floor();
  399. Point2i end = (p_to / cell_size).floor();
  400. Point2i step = Vector2(SGN(dir.x), SGN(dir.y));
  401. Vector2 max;
  402. if (dir.x < 0) {
  403. max.x = (Math::floor((double)pos.x) * cell_size - p_from.x) / dir.x;
  404. } else {
  405. max.x = (Math::floor((double)pos.x + 1) * cell_size - p_from.x) / dir.x;
  406. }
  407. if (dir.y < 0) {
  408. max.y = (Math::floor((double)pos.y) * cell_size - p_from.y) / dir.y;
  409. } else {
  410. max.y = (Math::floor((double)pos.y + 1) * cell_size - p_from.y) / dir.y;
  411. }
  412. int cullcount = 0;
  413. _cull<false, true>(pos, Rect2(), p_from, p_to, p_results, p_max_results, p_result_indices, cullcount);
  414. bool reached_x = false;
  415. bool reached_y = false;
  416. while (true) {
  417. if (max.x < max.y) {
  418. max.x += delta.x;
  419. pos.x += step.x;
  420. } else {
  421. max.y += delta.y;
  422. pos.y += step.y;
  423. }
  424. if (step.x > 0) {
  425. if (pos.x >= end.x) {
  426. reached_x = true;
  427. }
  428. } else if (pos.x <= end.x) {
  429. reached_x = true;
  430. }
  431. if (step.y > 0) {
  432. if (pos.y >= end.y) {
  433. reached_y = true;
  434. }
  435. } else if (pos.y <= end.y) {
  436. reached_y = true;
  437. }
  438. _cull<false, true>(pos, Rect2(), p_from, p_to, p_results, p_max_results, p_result_indices, cullcount);
  439. if (reached_x && reached_y) {
  440. break;
  441. }
  442. }
  443. for (Map<Element *, RC>::Element *E = large_elements.front(); E; E = E->next()) {
  444. if (cullcount >= p_max_results) {
  445. break;
  446. }
  447. if (E->key()->pass == pass) {
  448. continue;
  449. }
  450. E->key()->pass = pass;
  451. /*
  452. if (use_aabb && !p_aabb.intersects(E->key()->aabb))
  453. continue;
  454. */
  455. if (!E->key()->aabb.intersects_segment(p_from, p_to)) {
  456. continue;
  457. }
  458. p_results[cullcount] = E->key()->owner;
  459. p_result_indices[cullcount] = E->key()->subindex;
  460. cullcount++;
  461. }
  462. return cullcount;
  463. }
  464. int BroadPhase2DHashGrid::cull_aabb(const Rect2 &p_aabb, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices) {
  465. pass++;
  466. Point2i from = (p_aabb.position / cell_size).floor();
  467. Point2i to = ((p_aabb.position + p_aabb.size) / cell_size).floor();
  468. int cullcount = 0;
  469. for (int i = from.x; i <= to.x; i++) {
  470. for (int j = from.y; j <= to.y; j++) {
  471. _cull<true, false>(Point2i(i, j), p_aabb, Point2(), Point2(), p_results, p_max_results, p_result_indices, cullcount);
  472. }
  473. }
  474. for (Map<Element *, RC>::Element *E = large_elements.front(); E; E = E->next()) {
  475. if (cullcount >= p_max_results) {
  476. break;
  477. }
  478. if (E->key()->pass == pass) {
  479. continue;
  480. }
  481. E->key()->pass = pass;
  482. if (!p_aabb.intersects(E->key()->aabb)) {
  483. continue;
  484. }
  485. /*
  486. if (!E->key()->aabb.intersects_segment(p_from,p_to))
  487. continue;
  488. */
  489. p_results[cullcount] = E->key()->owner;
  490. p_result_indices[cullcount] = E->key()->subindex;
  491. cullcount++;
  492. }
  493. return cullcount;
  494. }
  495. void BroadPhase2DHashGrid::set_pair_callback(PairCallback p_pair_callback, void *p_userdata) {
  496. pair_callback = p_pair_callback;
  497. pair_userdata = p_userdata;
  498. }
  499. void BroadPhase2DHashGrid::set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata) {
  500. unpair_callback = p_unpair_callback;
  501. unpair_userdata = p_userdata;
  502. }
  503. void BroadPhase2DHashGrid::update() {
  504. }
  505. BroadPhase2DSW *BroadPhase2DHashGrid::_create() {
  506. return memnew(BroadPhase2DHashGrid);
  507. }
  508. BroadPhase2DHashGrid::BroadPhase2DHashGrid() {
  509. hash_table_size = GLOBAL_GET("physics/2d/bp_hash_table_size");
  510. ProjectSettings::get_singleton()->set_custom_property_info("physics/2d/bp_hash_table_size", PropertyInfo(Variant::INT, "physics/2d/bp_hash_table_size", PROPERTY_HINT_RANGE, "0,8192,1,or_greater"));
  511. hash_table_size = Math::larger_prime(hash_table_size);
  512. hash_table = memnew_arr(PosBin *, hash_table_size);
  513. cell_size = GLOBAL_GET("physics/2d/cell_size");
  514. ProjectSettings::get_singleton()->set_custom_property_info("physics/2d/cell_size", PropertyInfo(Variant::INT, "physics/2d/cell_size", PROPERTY_HINT_RANGE, "0,512,1,or_greater"));
  515. large_object_min_surface = GLOBAL_GET("physics/2d/large_object_surface_threshold_in_cells");
  516. ProjectSettings::get_singleton()->set_custom_property_info("physics/2d/large_object_surface_threshold_in_cells", PropertyInfo(Variant::INT, "physics/2d/large_object_surface_threshold_in_cells", PROPERTY_HINT_RANGE, "0,1024,1,or_greater"));
  517. for (uint32_t i = 0; i < hash_table_size; i++) {
  518. hash_table[i] = nullptr;
  519. }
  520. pass = 1;
  521. current = 0;
  522. }
  523. BroadPhase2DHashGrid::~BroadPhase2DHashGrid() {
  524. for (uint32_t i = 0; i < hash_table_size; i++) {
  525. while (hash_table[i]) {
  526. PosBin *pb = hash_table[i];
  527. hash_table[i] = pb->next;
  528. memdelete(pb);
  529. }
  530. }
  531. memdelete_arr(hash_table);
  532. }