Skip to content

Commit

Permalink
[Codechange] Removed redundant bounding box intersection checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ulteq committed Dec 22, 2015
1 parent 61c7dba commit 86a675b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
22 changes: 12 additions & 10 deletions source/main/physics/collision/PointColDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ void PointColDetector::update(Beam* truck) {

void PointColDetector::update(Beam* truck, Beam** trucks, const int numtrucks) {
int contacters_size = 0;
std::vector<Beam*> truckList(numtrucks);

This comment has been minimized.

Copy link
@Hiradur

Hiradur Dec 24, 2015

IMO the variable name truckList is not expressive enough, if I understood it correctly it's a vector of trucks where the BB intersects with the BB of the current truck, is that right?

This comment has been minimized.

Copy link
@ohlidalp

ohlidalp Dec 29, 2015

Variable truckList:
It's not expressive enough, it doesn't match our coding guidelines (should be collidable_trucks) and most importantly, it's sparse for no reason - trucks are inserted truckList[t] = @ and traversed if (truckList[t]) continue instead of truckList.push_back(@) and smooth traversal. Even if trucknum was needed somewhere, it can still be obtained from rig_t::trucknum


truck->collisionRelevant = false;
if (truck->state < SLEEPING) {
//Sweep & prune
for (int t = 0; t < numtrucks; t++) {
if (t != truck->trucknum && trucks[t] && trucks[t]->state < SLEEPING && BeamFactory::getSingleton().truckIntersectionAABB(t, truck->trucknum)) {
if (t != truck->trucknum && trucks[t] && trucks[t]->state < SLEEPING && truck->boundingBox.intersects(trucks[t]->boundingBox)) {
truckList[t] = trucks[t];
truck->collisionRelevant = true;
contacters_size += trucks[t]->free_contacter;
}
Expand All @@ -77,7 +79,7 @@ void PointColDetector::update(Beam* truck, Beam** trucks, const int numtrucks) {
//If the contacter number has changed, its time to update the kdtree structures
if (contacters_size != object_list_size) {
object_list_size = contacters_size;
update_structures_for_contacters(truck, trucks, numtrucks);
update_structures_for_contacters(truck, truckList);
}

kdtree[0].ref = NULL;
Expand All @@ -100,7 +102,7 @@ void PointColDetector::update_structures_for_contacters(Beam* truck) {

//Insert all contacters, into the list of points to consider when building the kdtree
if (truck && truck->state < SLEEPING) {
for (int i = 0;i < truck->free_contacter; ++i) {
for (int i = 0; i < truck->free_contacter; ++i) {
ref_list[refi].pidref = &pointid_list[refi];
pointid_list[refi].truckid = truck->trucknum;
pointid_list[refi].nodeid = truck->contacters[i].nodeid;
Expand All @@ -112,7 +114,7 @@ void PointColDetector::update_structures_for_contacters(Beam* truck) {
kdtree.resize(pow(2.f, exp_factor), kdelem);
}

void PointColDetector::update_structures_for_contacters(Beam* truck, Beam** trucks, const int numtrucks) {
void PointColDetector::update_structures_for_contacters(Beam* truck, const std::vector<Beam*> &truckList) {
kdnode_t kdelem = {0.0f, 0, 0.0f, NULL, 0.0f, 0};
hit_list.resize(object_list_size, NULL);
int exp_factor = std::max(0, (int) ceil(std::log2(object_list_size)) + 1);
Expand All @@ -126,16 +128,16 @@ void PointColDetector::update_structures_for_contacters(Beam* truck, Beam** truc
int refi = 0;

//Insert all contacters, into the list of points to consider when building the kdtree
for (int t = 0; t < numtrucks; t++) {
if (t == truck->trucknum || !trucks[t] || trucks[t]->state >= SLEEPING || !BeamFactory::getSingleton().truckIntersectionAABB(t, truck->trucknum)) {
for (int t = 0; t < truckList.size(); t++) {
if (!truckList[t])
{
continue;
}

for (int i = 0; i < trucks[t]->free_contacter; ++i) {
for (int i = 0; i < truckList[t]->free_contacter; ++i) {
ref_list[refi].pidref = &pointid_list[refi];
pointid_list[refi].truckid = t;
pointid_list[refi].nodeid = trucks[t]->contacters[i].nodeid;
ref_list[refi].point = &(trucks[t]->nodes[pointid_list[refi].nodeid].AbsPosition.x);
pointid_list[refi].nodeid = truckList[t]->contacters[i].nodeid;
ref_list[refi].point = &(truckList[t]->nodes[pointid_list[refi].nodeid].AbsPosition.x);
refi++;
}
}
Expand Down
2 changes: 1 addition & 1 deletion source/main/physics/collision/PointColDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class PointColDetector : public ZeroedMemoryAllocator
void build_kdtree_incr(int axis, int index);
void partintwo(const int start, const int median, const int end, const int axis, float &minex, float &maxex);
void update_structures_for_contacters(Beam* truck);
void update_structures_for_contacters(Beam* truck, Beam** trucks, const int numtrucks);
void update_structures_for_contacters(Beam* truck, const std::vector<Beam*> &truckList);
};

#endif // __PointColDetector_H_

0 comments on commit 86a675b

Please sign in to comment.