summaryrefslogtreecommitdiff
path: root/servers/physics_2d/broad_phase_2d_hash_grid.cpp
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2020-05-14 13:23:58 +0200
committerRémi Verschelde <rverschelde@gmail.com>2020-05-14 16:54:55 +0200
commit0be6d925dc3c6413bce7a3ccb49631b8e4a6e67a (patch)
treea27e497da7104dd0a64f98a04fa3067668735e91 /servers/physics_2d/broad_phase_2d_hash_grid.cpp
parent710b34b70227becdc652b4ae027fe0ac47409642 (diff)
Style: clang-format: Disable KeepEmptyLinesAtTheStartOfBlocks
Which means that reduz' beloved style which we all became used to will now be changed automatically to remove the first empty line. This makes us lean closer to 1TBS (the one true brace style) instead of hybridating it with some Allman-inspired spacing. There's still the case of braces around single-statement blocks that needs to be addressed (but clang-format can't help with that, but clang-tidy may if we agree about it). Part of #33027.
Diffstat (limited to 'servers/physics_2d/broad_phase_2d_hash_grid.cpp')
-rw-r--r--servers/physics_2d/broad_phase_2d_hash_grid.cpp62
1 files changed, 0 insertions, 62 deletions
diff --git a/servers/physics_2d/broad_phase_2d_hash_grid.cpp b/servers/physics_2d/broad_phase_2d_hash_grid.cpp
index 2cb021258a..b78dcf2866 100644
--- a/servers/physics_2d/broad_phase_2d_hash_grid.cpp
+++ b/servers/physics_2d/broad_phase_2d_hash_grid.cpp
@@ -34,13 +34,11 @@
#define LARGE_ELEMENT_FI 1.01239812
void BroadPhase2DHashGrid::_pair_attempt(Element *p_elem, Element *p_with) {
-
Map<Element *, PairData *>::Element *E = p_elem->paired.find(p_with);
ERR_FAIL_COND(p_elem->_static && p_with->_static);
if (!E) {
-
PairData *pd = memnew(PairData);
p_elem->paired[p_with] = pd;
p_with->paired[p_elem] = pd;
@@ -50,7 +48,6 @@ void BroadPhase2DHashGrid::_pair_attempt(Element *p_elem, Element *p_with) {
}
void BroadPhase2DHashGrid::_unpair_attempt(Element *p_elem, Element *p_with) {
-
Map<Element *, PairData *>::Element *E = p_elem->paired.find(p_with);
ERR_FAIL_COND(!E); //this should really be paired..
@@ -58,7 +55,6 @@ void BroadPhase2DHashGrid::_unpair_attempt(Element *p_elem, Element *p_with) {
E->get()->rc--;
if (E->get()->rc == 0) {
-
if (E->get()->colliding) {
//uncollide
if (unpair_callback) {
@@ -73,20 +69,15 @@ void BroadPhase2DHashGrid::_unpair_attempt(Element *p_elem, Element *p_with) {
}
void BroadPhase2DHashGrid::_check_motion(Element *p_elem) {
-
for (Map<Element *, PairData *>::Element *E = p_elem->paired.front(); E; E = E->next()) {
-
bool pairing = p_elem->aabb.intersects(E->key()->aabb);
if (pairing != E->get()->colliding) {
-
if (pairing) {
-
if (pair_callback) {
E->get()->ud = pair_callback(p_elem->owner, p_elem->subindex, E->key()->owner, E->key()->subindex, pair_userdata);
}
} else {
-
if (unpair_callback) {
unpair_callback(p_elem->owner, p_elem->subindex, E->key()->owner, E->key()->subindex, E->get()->ud, unpair_userdata);
}
@@ -98,7 +89,6 @@ void BroadPhase2DHashGrid::_check_motion(Element *p_elem) {
}
void BroadPhase2DHashGrid::_enter_grid(Element *p_elem, const Rect2 &p_rect, bool p_static) {
-
Vector2 sz = (p_rect.size / cell_size * LARGE_ELEMENT_FI); //use magic number to avoid floating point issues
if (sz.width * sz.height > large_object_min_surface) {
//large object, do not use grid, must check against all elements
@@ -121,9 +111,7 @@ void BroadPhase2DHashGrid::_enter_grid(Element *p_elem, const Rect2 &p_rect, boo
Point2i to = ((p_rect.position + p_rect.size) / cell_size).floor();
for (int i = from.x; i <= to.x; i++) {
-
for (int j = from.y; j <= to.y; j++) {
-
PosKey pk;
pk.x = i;
pk.y = j;
@@ -132,7 +120,6 @@ void BroadPhase2DHashGrid::_enter_grid(Element *p_elem, const Rect2 &p_rect, boo
PosBin *pb = hash_table[idx];
while (pb) {
-
if (pb->key == pk) {
break;
}
@@ -156,24 +143,19 @@ void BroadPhase2DHashGrid::_enter_grid(Element *p_elem, const Rect2 &p_rect, boo
}
} else {
if (pb->object_set[p_elem].inc() == 1) {
-
entered = true;
}
}
if (entered) {
-
for (Map<Element *, RC>::Element *E = pb->object_set.front(); E; E = E->next()) {
-
if (E->key()->owner == p_elem->owner)
continue;
_pair_attempt(p_elem, E->key());
}
if (!p_static) {
-
for (Map<Element *, RC>::Element *E = pb->static_object_set.front(); E; E = E->next()) {
-
if (E->key()->owner == p_elem->owner)
continue;
_pair_attempt(p_elem, E->key());
@@ -186,7 +168,6 @@ void BroadPhase2DHashGrid::_enter_grid(Element *p_elem, const Rect2 &p_rect, boo
//pair separatedly with large elements
for (Map<Element *, RC>::Element *E = large_elements.front(); E; E = E->next()) {
-
if (E->key() == p_elem)
continue; // do not pair against itself
if (E->key()->owner == p_elem->owner)
@@ -199,10 +180,8 @@ void BroadPhase2DHashGrid::_enter_grid(Element *p_elem, const Rect2 &p_rect, boo
}
void BroadPhase2DHashGrid::_exit_grid(Element *p_elem, const Rect2 &p_rect, bool p_static) {
-
Vector2 sz = (p_rect.size / cell_size * LARGE_ELEMENT_FI);
if (sz.width * sz.height > large_object_min_surface) {
-
//unpair all elements, instead of checking all, just check what is already paired, so we at least save from checking static vs static
Map<Element *, PairData *>::Element *E = p_elem->paired.front();
while (E) {
@@ -221,9 +200,7 @@ void BroadPhase2DHashGrid::_exit_grid(Element *p_elem, const Rect2 &p_rect, bool
Point2i to = ((p_rect.position + p_rect.size) / cell_size).floor();
for (int i = from.x; i <= to.x; i++) {
-
for (int j = from.y; j <= to.y; j++) {
-
PosKey pk;
pk.x = i;
pk.y = j;
@@ -232,7 +209,6 @@ void BroadPhase2DHashGrid::_exit_grid(Element *p_elem, const Rect2 &p_rect, bool
PosBin *pb = hash_table[idx];
while (pb) {
-
if (pb->key == pk) {
break;
}
@@ -246,31 +222,25 @@ void BroadPhase2DHashGrid::_exit_grid(Element *p_elem, const Rect2 &p_rect, bool
if (p_static) {
if (pb->static_object_set[p_elem].dec() == 0) {
-
pb->static_object_set.erase(p_elem);
exited = true;
}
} else {
if (pb->object_set[p_elem].dec() == 0) {
-
pb->object_set.erase(p_elem);
exited = true;
}
}
if (exited) {
-
for (Map<Element *, RC>::Element *E = pb->object_set.front(); E; E = E->next()) {
-
if (E->key()->owner == p_elem->owner)
continue;
_unpair_attempt(p_elem, E->key());
}
if (!p_static) {
-
for (Map<Element *, RC>::Element *E = pb->static_object_set.front(); E; E = E->next()) {
-
if (E->key()->owner == p_elem->owner)
continue;
_unpair_attempt(p_elem, E->key());
@@ -279,15 +249,12 @@ void BroadPhase2DHashGrid::_exit_grid(Element *p_elem, const Rect2 &p_rect, bool
}
if (pb->object_set.empty() && pb->static_object_set.empty()) {
-
if (hash_table[idx] == pb) {
hash_table[idx] = pb->next;
} else {
-
PosBin *px = hash_table[idx];
while (px) {
-
if (px->next == pb) {
px->next = pb->next;
break;
@@ -318,7 +285,6 @@ void BroadPhase2DHashGrid::_exit_grid(Element *p_elem, const Rect2 &p_rect, bool
}
BroadPhase2DHashGrid::ID BroadPhase2DHashGrid::create(CollisionObject2DSW *p_object, int p_subindex) {
-
current++;
Element e;
@@ -333,7 +299,6 @@ BroadPhase2DHashGrid::ID BroadPhase2DHashGrid::create(CollisionObject2DSW *p_obj
}
void BroadPhase2DHashGrid::move(ID p_id, const Rect2 &p_aabb) {
-
Map<ID, Element>::Element *E = element_map.find(p_id);
ERR_FAIL_COND(!E);
@@ -343,12 +308,10 @@ void BroadPhase2DHashGrid::move(ID p_id, const Rect2 &p_aabb) {
return;
if (p_aabb != Rect2()) {
-
_enter_grid(&e, p_aabb, e._static);
}
if (e.aabb != Rect2()) {
-
_exit_grid(&e, e.aabb, e._static);
}
@@ -359,7 +322,6 @@ void BroadPhase2DHashGrid::move(ID p_id, const Rect2 &p_aabb) {
e.aabb = p_aabb;
}
void BroadPhase2DHashGrid::set_static(ID p_id, bool p_static) {
-
Map<ID, Element>::Element *E = element_map.find(p_id);
ERR_FAIL_COND(!E);
@@ -379,7 +341,6 @@ void BroadPhase2DHashGrid::set_static(ID p_id, bool p_static) {
}
}
void BroadPhase2DHashGrid::remove(ID p_id) {
-
Map<ID, Element>::Element *E = element_map.find(p_id);
ERR_FAIL_COND(!E);
@@ -392,19 +353,16 @@ void BroadPhase2DHashGrid::remove(ID p_id) {
}
CollisionObject2DSW *BroadPhase2DHashGrid::get_object(ID p_id) const {
-
const Map<ID, Element>::Element *E = element_map.find(p_id);
ERR_FAIL_COND_V(!E, nullptr);
return E->get().owner;
}
bool BroadPhase2DHashGrid::is_static(ID p_id) const {
-
const Map<ID, Element>::Element *E = element_map.find(p_id);
ERR_FAIL_COND_V(!E, false);
return E->get()._static;
}
int BroadPhase2DHashGrid::get_subindex(ID p_id) const {
-
const Map<ID, Element>::Element *E = element_map.find(p_id);
ERR_FAIL_COND_V(!E, -1);
return E->get().subindex;
@@ -412,7 +370,6 @@ int BroadPhase2DHashGrid::get_subindex(ID p_id) const {
template <bool use_aabb, bool use_segment>
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) {
-
PosKey pk;
pk.x = p_cell.x;
pk.y = p_cell.y;
@@ -421,7 +378,6 @@ void BroadPhase2DHashGrid::_cull(const Point2i p_cell, const Rect2 &p_aabb, cons
PosBin *pb = hash_table[idx];
while (pb) {
-
if (pb->key == pk) {
break;
}
@@ -433,7 +389,6 @@ void BroadPhase2DHashGrid::_cull(const Point2i p_cell, const Rect2 &p_aabb, cons
return;
for (Map<Element *, RC>::Element *E = pb->object_set.front(); E; E = E->next()) {
-
if (index >= p_max_results)
break;
if (E->key()->pass == pass)
@@ -453,7 +408,6 @@ void BroadPhase2DHashGrid::_cull(const Point2i p_cell, const Rect2 &p_aabb, cons
}
for (Map<Element *, RC>::Element *E = pb->static_object_set.front(); E; E = E->next()) {
-
if (index >= p_max_results)
break;
if (E->key()->pass == pass)
@@ -474,7 +428,6 @@ void BroadPhase2DHashGrid::_cull(const Point2i p_cell, const Rect2 &p_aabb, cons
}
int BroadPhase2DHashGrid::cull_segment(const Vector2 &p_from, const Vector2 &p_to, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices) {
-
pass++;
Vector2 dir = (p_to - p_from);
@@ -515,13 +468,10 @@ int BroadPhase2DHashGrid::cull_segment(const Vector2 &p_from, const Vector2 &p_t
bool reached_y = false;
while (true) {
-
if (max.x < max.y) {
-
max.x += delta.x;
pos.x += step.x;
} else {
-
max.y += delta.y;
pos.y += step.y;
}
@@ -530,7 +480,6 @@ int BroadPhase2DHashGrid::cull_segment(const Vector2 &p_from, const Vector2 &p_t
if (pos.x >= end.x)
reached_x = true;
} else if (pos.x <= end.x) {
-
reached_x = true;
}
@@ -538,7 +487,6 @@ int BroadPhase2DHashGrid::cull_segment(const Vector2 &p_from, const Vector2 &p_t
if (pos.y >= end.y)
reached_y = true;
} else if (pos.y <= end.y) {
-
reached_y = true;
}
@@ -549,7 +497,6 @@ int BroadPhase2DHashGrid::cull_segment(const Vector2 &p_from, const Vector2 &p_t
}
for (Map<Element *, RC>::Element *E = large_elements.front(); E; E = E->next()) {
-
if (cullcount >= p_max_results)
break;
if (E->key()->pass == pass)
@@ -574,7 +521,6 @@ int BroadPhase2DHashGrid::cull_segment(const Vector2 &p_from, const Vector2 &p_t
}
int BroadPhase2DHashGrid::cull_aabb(const Rect2 &p_aabb, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices) {
-
pass++;
Point2i from = (p_aabb.position / cell_size).floor();
@@ -582,15 +528,12 @@ int BroadPhase2DHashGrid::cull_aabb(const Rect2 &p_aabb, CollisionObject2DSW **p
int cullcount = 0;
for (int i = from.x; i <= to.x; i++) {
-
for (int j = from.y; j <= to.y; j++) {
-
_cull<true, false>(Point2i(i, j), p_aabb, Point2(), Point2(), p_results, p_max_results, p_result_indices, cullcount);
}
}
for (Map<Element *, RC>::Element *E = large_elements.front(); E; E = E->next()) {
-
if (cullcount >= p_max_results)
break;
if (E->key()->pass == pass)
@@ -614,12 +557,10 @@ int BroadPhase2DHashGrid::cull_aabb(const Rect2 &p_aabb, CollisionObject2DSW **p
}
void BroadPhase2DHashGrid::set_pair_callback(PairCallback p_pair_callback, void *p_userdata) {
-
pair_callback = p_pair_callback;
pair_userdata = p_userdata;
}
void BroadPhase2DHashGrid::set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata) {
-
unpair_callback = p_unpair_callback;
unpair_userdata = p_userdata;
}
@@ -628,12 +569,10 @@ void BroadPhase2DHashGrid::update() {
}
BroadPhase2DSW *BroadPhase2DHashGrid::_create() {
-
return memnew(BroadPhase2DHashGrid);
}
BroadPhase2DHashGrid::BroadPhase2DHashGrid() {
-
hash_table_size = GLOBAL_DEF("physics/2d/bp_hash_table_size", 4096);
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"));
hash_table_size = Math::larger_prime(hash_table_size);
@@ -653,7 +592,6 @@ BroadPhase2DHashGrid::BroadPhase2DHashGrid() {
}
BroadPhase2DHashGrid::~BroadPhase2DHashGrid() {
-
for (uint32_t i = 0; i < hash_table_size; i++) {
while (hash_table[i]) {
PosBin *pb = hash_table[i];