diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2017-03-05 16:44:50 +0100 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2017-03-05 16:44:50 +0100 |
commit | 5dbf1809c6e3e905b94b8764e99491e608122261 (patch) | |
tree | 5e5a5360db15d86d59ec8c6e4f7eb511388c5a9a /servers/physics_2d/broad_phase_2d_hash_grid.cpp | |
parent | 45438e9918d421b244bfd7776a30e67dc7f2d3e3 (diff) |
A Whole New World (clang-format edition)
I can show you the code
Pretty, with proper whitespace
Tell me, coder, now when did
You last write readable code?
I can open your eyes
Make you see your bad indent
Force you to respect the style
The core devs agreed upon
A whole new world
A new fantastic code format
A de facto standard
With some sugar
Enforced with clang-format
A whole new world
A dazzling style we all dreamed of
And when we read it through
It's crystal clear
That now we're in a whole new world of code
Diffstat (limited to 'servers/physics_2d/broad_phase_2d_hash_grid.cpp')
-rw-r--r-- | servers/physics_2d/broad_phase_2d_hash_grid.cpp | 479 |
1 files changed, 209 insertions, 270 deletions
diff --git a/servers/physics_2d/broad_phase_2d_hash_grid.cpp b/servers/physics_2d/broad_phase_2d_hash_grid.cpp index 06eead64cd..74c01e1220 100644 --- a/servers/physics_2d/broad_phase_2d_hash_grid.cpp +++ b/servers/physics_2d/broad_phase_2d_hash_grid.cpp @@ -31,110 +31,100 @@ #define LARGE_ELEMENT_FI 1.01239812 -void BroadPhase2DHashGrid::_pair_attempt(Element *p_elem, Element* p_with) { +void BroadPhase2DHashGrid::_pair_attempt(Element *p_elem, Element *p_with) { - Map<Element*,PairData*>::Element *E=p_elem->paired.find(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; + PairData *pd = memnew(PairData); + p_elem->paired[p_with] = pd; + p_with->paired[p_elem] = pd; } else { E->get()->rc++; } - } -void BroadPhase2DHashGrid::_unpair_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); + Map<Element *, PairData *>::Element *E = p_elem->paired.find(p_with); ERR_FAIL_COND(!E); //this should really be paired.. E->get()->rc--; - if (E->get()->rc==0) { + if (E->get()->rc == 0) { if (E->get()->colliding) { //uncollide if (unpair_callback) { - unpair_callback(p_elem->owner,p_elem->subindex,p_with->owner,p_with->subindex,E->get()->ud,unpair_userdata); + unpair_callback(p_elem->owner, p_elem->subindex, p_with->owner, p_with->subindex, E->get()->ud, unpair_userdata); } - - } memdelete(E->get()); p_elem->paired.erase(E); p_with->paired.erase(p_elem); } - - } void BroadPhase2DHashGrid::_check_motion(Element *p_elem) { - for (Map<Element*,PairData*>::Element *E=p_elem->paired.front();E;E=E->next()) { + for (Map<Element *, PairData *>::Element *E = p_elem->paired.front(); E; E = E->next()) { - bool pairing = p_elem->aabb.intersects( E->key()->aabb ); + bool pairing = p_elem->aabb.intersects(E->key()->aabb); - if (pairing!=E->get()->colliding) { + 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); + 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); + unpair_callback(p_elem->owner, p_elem->subindex, E->key()->owner, E->key()->subindex, E->get()->ud, unpair_userdata); } - } - E->get()->colliding=pairing; + E->get()->colliding = pairing; } } } -void BroadPhase2DHashGrid::_enter_grid( Element* p_elem, const Rect2& p_rect,bool p_static) { - - +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) { + 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 - for (Map<ID,Element>::Element *E=element_map.front();E;E=E->next()) { - if (E->key()==p_elem->self) + for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) { + if (E->key() == p_elem->self) continue; // do not pair against itself if (E->get().owner == p_elem->owner) continue; if (E->get()._static && p_static) continue; - _pair_attempt(p_elem,&E->get()); + _pair_attempt(p_elem, &E->get()); } - large_elements[p_elem].inc(); return; } - Point2i from = (p_rect.pos/cell_size).floor(); - Point2i to = ((p_rect.pos+p_rect.size)/cell_size).floor(); - - for(int i=from.x;i<=to.x;i++) { + Point2i from = (p_rect.pos / cell_size).floor(); + Point2i to = ((p_rect.pos + p_rect.size) / cell_size).floor(); + for (int i = from.x; i <= to.x; i++) { - for(int j=from.y;j<=to.y;j++) { + for (int j = from.y; j <= to.y; j++) { PosKey pk; - pk.x=i; - pk.y=j; + pk.x = i; + pk.y = j; uint32_t idx = pk.hash() % hash_table_size; PosBin *pb = hash_table[idx]; @@ -145,102 +135,94 @@ void BroadPhase2DHashGrid::_enter_grid( Element* p_elem, const Rect2& p_rect,boo break; } - pb=pb->next; + pb = pb->next; } - - bool entered=false; + bool entered = false; if (!pb) { //does not exist, create! - pb = memnew( PosBin ); - pb->key=pk; - pb->next=hash_table[idx]; - hash_table[idx]=pb; + pb = memnew(PosBin); + pb->key = pk; + pb->next = hash_table[idx]; + hash_table[idx] = pb; } - - if (p_static) { - if (pb->static_object_set[p_elem].inc()==1) { - entered=true; + if (pb->static_object_set[p_elem].inc() == 1) { + entered = true; } } else { - if (pb->object_set[p_elem].inc()==1) { + if (pb->object_set[p_elem].inc() == 1) { - entered=true; + entered = true; } } if (entered) { - for(Map<Element*,RC>::Element *E=pb->object_set.front();E;E=E->next()) { + for (Map<Element *, RC>::Element *E = pb->object_set.front(); E; E = E->next()) { - if (E->key()->owner==p_elem->owner) + if (E->key()->owner == p_elem->owner) continue; - _pair_attempt(p_elem,E->key()); + _pair_attempt(p_elem, E->key()); } if (!p_static) { - for(Map<Element*,RC>::Element *E=pb->static_object_set.front();E;E=E->next()) { + for (Map<Element *, RC>::Element *E = pb->static_object_set.front(); E; E = E->next()) { - if (E->key()->owner==p_elem->owner) + if (E->key()->owner == p_elem->owner) continue; - _pair_attempt(p_elem,E->key()); + _pair_attempt(p_elem, E->key()); } } } - } - } //pair separatedly with large elements - for (Map<Element*,RC>::Element *E=large_elements.front();E;E=E->next()) { + for (Map<Element *, RC>::Element *E = large_elements.front(); E; E = E->next()) { - if (E->key()==p_elem) + if (E->key() == p_elem) continue; // do not pair against itself if (E->key()->owner == p_elem->owner) continue; if (E->key()->_static && p_static) continue; - _pair_attempt(E->key(),p_elem); + _pair_attempt(E->key(), p_elem); } - } +void BroadPhase2DHashGrid::_exit_grid(Element *p_elem, const Rect2 &p_rect, bool p_static) { -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) { + 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 - for (Map<Element*,PairData*>::Element *E=p_elem->paired.front();E;E=E->next()) { + for (Map<Element *, PairData *>::Element *E = p_elem->paired.front(); E; E = E->next()) { - _unpair_attempt(p_elem,E->key()); + _unpair_attempt(p_elem, E->key()); } - if (large_elements[p_elem].dec()==0) { + if (large_elements[p_elem].dec() == 0) { large_elements.erase(p_elem); } return; } + Point2i from = (p_rect.pos / cell_size).floor(); + Point2i to = ((p_rect.pos + p_rect.size) / cell_size).floor(); - Point2i from = (p_rect.pos/cell_size).floor(); - Point2i to = ((p_rect.pos+p_rect.size)/cell_size).floor(); - - for(int i=from.x;i<=to.x;i++) { + for (int i = from.x; i <= to.x; i++) { - for(int j=from.y;j<=to.y;j++) { + for (int j = from.y; j <= to.y; j++) { PosKey pk; - pk.x=i; - pk.y=j; + pk.x = i; + pk.y = j; uint32_t idx = pk.hash() % hash_table_size; PosBin *pb = hash_table[idx]; @@ -251,82 +233,75 @@ void BroadPhase2DHashGrid::_exit_grid( Element* p_elem, const Rect2& p_rect,bool break; } - pb=pb->next; + pb = pb->next; } ERR_CONTINUE(!pb); //should exist!! - bool exited=false; - + bool exited = false; if (p_static) { - if (pb->static_object_set[p_elem].dec()==0) { + if (pb->static_object_set[p_elem].dec() == 0) { pb->static_object_set.erase(p_elem); - exited=true; - + exited = true; } } else { - if (pb->object_set[p_elem].dec()==0) { + if (pb->object_set[p_elem].dec() == 0) { pb->object_set.erase(p_elem); - exited=true; - + exited = true; } } if (exited) { - for(Map<Element*,RC>::Element *E=pb->object_set.front();E;E=E->next()) { + for (Map<Element *, RC>::Element *E = pb->object_set.front(); E; E = E->next()) { - if (E->key()->owner==p_elem->owner) + if (E->key()->owner == p_elem->owner) continue; - _unpair_attempt(p_elem,E->key()); - + _unpair_attempt(p_elem, E->key()); } if (!p_static) { - for(Map<Element*,RC>::Element *E=pb->static_object_set.front();E;E=E->next()) { + for (Map<Element *, RC>::Element *E = pb->static_object_set.front(); E; E = E->next()) { - if (E->key()->owner==p_elem->owner) + if (E->key()->owner == p_elem->owner) continue; - _unpair_attempt(p_elem,E->key()); + _unpair_attempt(p_elem, E->key()); } } } if (pb->object_set.empty() && pb->static_object_set.empty()) { - if (hash_table[idx]==pb) { - hash_table[idx]=pb->next; + 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; + if (px->next == pb) { + px->next = pb->next; break; } - px=px->next; + px = px->next; } ERR_CONTINUE(!px); } memdelete(pb); - } } - } - - for (Map<Element*,RC>::Element *E=large_elements.front();E;E=E->next()) { - if (E->key()==p_elem) + 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) continue; @@ -334,121 +309,109 @@ void BroadPhase2DHashGrid::_exit_grid( Element* p_elem, const Rect2& p_rect,bool continue; //unpair from large elements - _unpair_attempt(p_elem,E->key()); + _unpair_attempt(p_elem, E->key()); } - - } - BroadPhase2DHashGrid::ID BroadPhase2DHashGrid::create(CollisionObject2DSW *p_object, int p_subindex) { current++; Element e; - e.owner=p_object; - e._static=false; - e.subindex=p_subindex; - e.self=current; - e.pass=0; + e.owner = p_object; + e._static = false; + e.subindex = p_subindex; + e.self = current; + e.pass = 0; - element_map[current]=e; + element_map[current] = e; return current; - } -void BroadPhase2DHashGrid::move(ID p_id, const Rect2& p_aabb) { - +void BroadPhase2DHashGrid::move(ID p_id, const Rect2 &p_aabb) { - Map<ID,Element>::Element *E=element_map.find(p_id); + Map<ID, Element>::Element *E = element_map.find(p_id); ERR_FAIL_COND(!E); - Element &e=E->get(); + Element &e = E->get(); - if (p_aabb==e.aabb) + if (p_aabb == e.aabb) return; + if (p_aabb != Rect2()) { - if (p_aabb!=Rect2()) { - - _enter_grid(&e,p_aabb,e._static); + _enter_grid(&e, p_aabb, e._static); } - if (e.aabb!=Rect2()) { + if (e.aabb != Rect2()) { - _exit_grid(&e,e.aabb,e._static); + _exit_grid(&e, e.aabb, e._static); } - e.aabb=p_aabb; + e.aabb = p_aabb; _check_motion(&e); - e.aabb=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); + Map<ID, Element>::Element *E = element_map.find(p_id); ERR_FAIL_COND(!E); - Element &e=E->get(); + Element &e = E->get(); - if (e._static==p_static) + if (e._static == p_static) return; - if (e.aabb!=Rect2()) - _exit_grid(&e,e.aabb,e._static); + if (e.aabb != Rect2()) + _exit_grid(&e, e.aabb, e._static); - e._static=p_static; + e._static = p_static; - if (e.aabb!=Rect2()) { - _enter_grid(&e,e.aabb,e._static); + if (e.aabb != Rect2()) { + _enter_grid(&e, e.aabb, e._static); _check_motion(&e); } - } void BroadPhase2DHashGrid::remove(ID p_id) { - Map<ID,Element>::Element *E=element_map.find(p_id); + Map<ID, Element>::Element *E = element_map.find(p_id); ERR_FAIL_COND(!E); - Element &e=E->get(); + Element &e = E->get(); - if (e.aabb!=Rect2()) - _exit_grid(&e,e.aabb,e._static); + if (e.aabb != Rect2()) + _exit_grid(&e, e.aabb, e._static); element_map.erase(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,NULL); + const Map<ID, Element>::Element *E = element_map.find(p_id); + ERR_FAIL_COND_V(!E, NULL); 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); + 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); + const Map<ID, Element>::Element *E = element_map.find(p_id); + ERR_FAIL_COND_V(!E, -1); return E->get().subindex; } -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) { - +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; + pk.x = p_cell.x; + pk.y = p_cell.y; uint32_t idx = pk.hash() % hash_table_size; PosBin *pb = hash_table[idx]; @@ -459,190 +422,177 @@ void BroadPhase2DHashGrid::_cull(const Point2i p_cell,const Rect2& p_aabb,const break; } - pb=pb->next; + pb = pb->next; } if (!pb) return; + for (Map<Element *, RC>::Element *E = pb->object_set.front(); E; E = E->next()) { - - for(Map<Element*,RC>::Element *E=pb->object_set.front();E;E=E->next()) { - - - if (index>=p_max_results) + if (index >= p_max_results) break; - if (E->key()->pass==pass) + if (E->key()->pass == pass) continue; - E->key()->pass=pass; + E->key()->pass = pass; if (use_aabb && !p_aabb.intersects(E->key()->aabb)) continue; - if (use_segment && !E->key()->aabb.intersects_segment(p_from,p_to)) + if (use_segment && !E->key()->aabb.intersects_segment(p_from, p_to)) continue; - p_results[index]=E->key()->owner; - p_result_indices[index]=E->key()->subindex; + p_results[index] = E->key()->owner; + p_result_indices[index] = E->key()->subindex; index++; - - } - for(Map<Element*,RC>::Element *E=pb->static_object_set.front();E;E=E->next()) { - + for (Map<Element *, RC>::Element *E = pb->static_object_set.front(); E; E = E->next()) { - if (index>=p_max_results) + if (index >= p_max_results) break; - if (E->key()->pass==pass) + if (E->key()->pass == pass) continue; if (use_aabb && !p_aabb.intersects(E->key()->aabb)) { continue; } - if (use_segment && !E->key()->aabb.intersects_segment(p_from,p_to)) + if (use_segment && !E->key()->aabb.intersects_segment(p_from, p_to)) continue; - E->key()->pass=pass; - p_results[index]=E->key()->owner; - p_result_indices[index]=E->key()->subindex; + E->key()->pass = pass; + p_results[index] = E->key()->owner; + p_result_indices[index] = E->key()->subindex; index++; - } } -int BroadPhase2DHashGrid::cull_segment(const Vector2& p_from, const Vector2& p_to,CollisionObject2DSW** p_results,int p_max_results,int *p_result_indices) { +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); - if (dir==Vector2()) + Vector2 dir = (p_to - p_from); + if (dir == Vector2()) return 0; //avoid divisions by zero dir.normalize(); - if (dir.x==0.0) - dir.x=0.000001; - if (dir.y==0.0) - dir.y=0.000001; + if (dir.x == 0.0) + dir.x = 0.000001; + if (dir.y == 0.0) + dir.y = 0.000001; Vector2 delta = dir.abs(); - delta.x=cell_size/delta.x; - delta.y=cell_size/delta.y; + delta.x = cell_size / delta.x; + delta.y = cell_size / delta.y; - Point2i pos = (p_from/cell_size).floor(); - Point2i end = (p_to/cell_size).floor(); + Point2i pos = (p_from / cell_size).floor(); + Point2i end = (p_to / cell_size).floor(); - Point2i step = Vector2( SGN(dir.x), SGN(dir.y) ); + Point2i step = Vector2(SGN(dir.x), SGN(dir.y)); Vector2 max; - if (dir.x<0) - max.x= (Math::floor((double)pos.x)*cell_size - p_from.x) / dir.x; + if (dir.x < 0) + max.x = (Math::floor((double)pos.x) * cell_size - p_from.x) / dir.x; else - max.x= (Math::floor((double)pos.x + 1)*cell_size - p_from.x) / dir.x; + max.x = (Math::floor((double)pos.x + 1) * cell_size - p_from.x) / dir.x; - if (dir.y<0) - max.y= (Math::floor((double)pos.y)*cell_size - p_from.y) / dir.y; + if (dir.y < 0) + max.y = (Math::floor((double)pos.y) * cell_size - p_from.y) / dir.y; else - max.y= (Math::floor((double)pos.y + 1)*cell_size - p_from.y) / dir.y; + max.y = (Math::floor((double)pos.y + 1) * cell_size - p_from.y) / dir.y; - int cullcount=0; - _cull<false,true>(pos,Rect2(),p_from,p_to,p_results,p_max_results,p_result_indices,cullcount); + int cullcount = 0; + _cull<false, true>(pos, Rect2(), p_from, p_to, p_results, p_max_results, p_result_indices, cullcount); - bool reached_x=false; - bool reached_y=false; + bool reached_x = false; + bool reached_y = false; - while(true) { + while (true) { if (max.x < max.y) { - max.x+=delta.x; - pos.x+=step.x; + max.x += delta.x; + pos.x += step.x; } else { - max.y+=delta.y; - pos.y+=step.y; - + max.y += delta.y; + pos.y += step.y; } - if (step.x>0) { - if (pos.x>=end.x) - reached_x=true; - } else if (pos.x<=end.x) { + if (step.x > 0) { + if (pos.x >= end.x) + reached_x = true; + } else if (pos.x <= end.x) { - reached_x=true; + reached_x = true; } - if (step.y>0) { - if (pos.y>=end.y) - reached_y=true; - } else if (pos.y<=end.y) { + if (step.y > 0) { + if (pos.y >= end.y) + reached_y = true; + } else if (pos.y <= end.y) { - reached_y=true; + reached_y = true; } - _cull<false,true>(pos,Rect2(),p_from,p_to,p_results,p_max_results,p_result_indices,cullcount); + _cull<false, true>(pos, Rect2(), p_from, p_to, p_results, p_max_results, p_result_indices, cullcount); if (reached_x && reached_y) break; - } - for (Map<Element*,RC>::Element *E=large_elements.front();E;E=E->next()) { + for (Map<Element *, RC>::Element *E = large_elements.front(); E; E = E->next()) { - if (cullcount>=p_max_results) + if (cullcount >= p_max_results) break; - if (E->key()->pass==pass) + if (E->key()->pass == pass) continue; - E->key()->pass=pass; + E->key()->pass = pass; /* if (use_aabb && !p_aabb.intersects(E->key()->aabb)) continue; */ - if (!E->key()->aabb.intersects_segment(p_from,p_to)) + if (!E->key()->aabb.intersects_segment(p_from, p_to)) continue; - p_results[cullcount]=E->key()->owner; - p_result_indices[cullcount]=E->key()->subindex; + p_results[cullcount] = E->key()->owner; + p_result_indices[cullcount] = E->key()->subindex; cullcount++; - - } return cullcount; } - -int BroadPhase2DHashGrid::cull_aabb(const Rect2& p_aabb,CollisionObject2DSW** p_results,int p_max_results,int *p_result_indices) { +int BroadPhase2DHashGrid::cull_aabb(const Rect2 &p_aabb, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices) { pass++; - Point2i from = (p_aabb.pos/cell_size).floor(); - Point2i to = ((p_aabb.pos+p_aabb.size)/cell_size).floor(); - int cullcount=0; + Point2i from = (p_aabb.pos / cell_size).floor(); + Point2i to = ((p_aabb.pos + p_aabb.size) / cell_size).floor(); + int cullcount = 0; - for(int i=from.x;i<=to.x;i++) { + for (int i = from.x; i <= to.x; i++) { - for(int j=from.y;j<=to.y;j++) { + 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); + _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()) { + for (Map<Element *, RC>::Element *E = large_elements.front(); E; E = E->next()) { - if (cullcount>=p_max_results) + if (cullcount >= p_max_results) break; - if (E->key()->pass==pass) + if (E->key()->pass == pass) continue; - E->key()->pass=pass; + E->key()->pass = pass; if (!p_aabb.intersects(E->key()->aabb)) continue; @@ -652,72 +602,61 @@ int BroadPhase2DHashGrid::cull_aabb(const Rect2& p_aabb,CollisionObject2DSW** p_ continue; */ - p_results[cullcount]=E->key()->owner; - p_result_indices[cullcount]=E->key()->subindex; + p_results[cullcount] = E->key()->owner; + p_result_indices[cullcount] = E->key()->subindex; cullcount++; - - } return cullcount; } -void BroadPhase2DHashGrid::set_pair_callback(PairCallback p_pair_callback,void *p_userdata) { - - pair_callback=p_pair_callback; - pair_userdata=p_userdata; +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; +void BroadPhase2DHashGrid::set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata) { + unpair_callback = p_unpair_callback; + unpair_userdata = p_userdata; } void BroadPhase2DHashGrid::update() { - - } BroadPhase2DSW *BroadPhase2DHashGrid::_create() { - return memnew( BroadPhase2DHashGrid ); + return memnew(BroadPhase2DHashGrid); } - BroadPhase2DHashGrid::BroadPhase2DHashGrid() { - hash_table_size = GLOBAL_DEF("physics/2d/bp_hash_table_size",4096); + hash_table_size = GLOBAL_DEF("physics/2d/bp_hash_table_size", 4096); hash_table_size = Math::larger_prime(hash_table_size); - hash_table = memnew_arr( PosBin*, hash_table_size); + hash_table = memnew_arr(PosBin *, hash_table_size); - cell_size = GLOBAL_DEF("physics/2d/cell_size",128); - large_object_min_surface = GLOBAL_DEF("physics/2d/large_object_surface_treshold_in_cells",512); + cell_size = GLOBAL_DEF("physics/2d/cell_size", 128); + large_object_min_surface = GLOBAL_DEF("physics/2d/large_object_surface_treshold_in_cells", 512); - for(int i=0;i<hash_table_size;i++) - hash_table[i]=NULL; - pass=1; + for (int i = 0; i < hash_table_size; i++) + hash_table[i] = NULL; + pass = 1; - current=0; + current = 0; } BroadPhase2DHashGrid::~BroadPhase2DHashGrid() { - for(int i=0;i<hash_table_size;i++) { - while(hash_table[i]) { - PosBin *pb=hash_table[i]; - hash_table[i]=pb->next; + for (int i = 0; i < hash_table_size; i++) { + while (hash_table[i]) { + PosBin *pb = hash_table[i]; + hash_table[i] = pb->next; memdelete(pb); } } - memdelete_arr( hash_table ); - - + memdelete_arr(hash_table); } - - /* 3D version of voxel traversal: public IEnumerable<Point3D> GetCellsOnRay(Ray ray, int maxDepth) |