Hello all I’ve been messing around with Quadtrees lately, and I have came across this guys implementation which has some sort of weird effect
I followed this tut
However please correct me if im wrong but the way Im using it makes entities not detect collision when moving from one node to another
Here is an image… of a sim I just made (All entities bounce of each other) You should see clearly what I mean
I made some changes to the code which fixes this issue but I find it rather odd why no one else has mentioned this… I feel like I have made a mistake somewhere
Here was my fix
public void insert(Entity e){
if(nodes[0] != null){
List<Integer> indexes = getIndex(e.getGlobalPosition().x, e.getGlobalPosition().y, e.getWidth(), e.getHeight());
if(indexes != null){
for(Integer i : indexes)
nodes[i].insert(e);
return;
}
}
// Fits in this quad
objects.add(e);
if(objects.size() > MAX_OBJECTS && level < MAX_LEVELS){
if(nodes[0] == null){
split();
}
for(int i = 0;i < objects.size();i++){
Entity o = objects.get(i);
List<Integer> indexes = getIndex(o.getGlobalPosition().x, o.getGlobalPosition().y, o.getWidth(), o.getHeight());
if(indexes != null){
for(Integer iq : indexes)
nodes[iq].insert(o);
}
}
}
}
It basically uses the dimensions of the entity to see if it collides in any other node, then adds them all to an array for checking… the only issue with this is there is a higher chance of doing the collision check on the same entity twice, but its faster then checking every entity
The problem that you check an collision between two objects twice is maybe because you iterate over all nodes… and don’t skip collision with nodes already handled by before from “the other object’s view”. But I didn’t take a look at the code.
but this accalerates your insertion/update for moving objects and you have plenty of them, it could be worth the price. I would tend to say that it is rather not correct, because I can’t think of a performance benefit over other solutions, such as a loose tree.