My current entity engine uses a quadtree that loops through all the Entities (an Array) and then checks for collisions using a libgdx Rectangle instance that every entity has.
My QuadTree (Its pretty long so i figured id just link to it)
In the Entity class i have a placeFree method:
public boolean placeFree(float px, float py){
//the pre-emptive collision Rectangle
preCollBox.set(px,py,hitBox.getWidth(),hitBox.getHeight());
return screen.placeFree(preCollBox);
}
The screen.placeFree method:
Qtree = new Quadtree(0 , new Rectangle(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight()));
public boolean placeFree(Rectangle rect){
boolean result = false;
Qtree.clear();
for (int i = 0; i < collideEnt.size; i++) {
Qtree.insert(collideEnt.get(i));
}
for(int i = 0; i < collideEnt.size;i++){
returnEntity.clear();
Qtree.retrieve(returnEntity, rect);
for (int x = 0; x < returnEntity.size; x++) {
Entity ec = returnEntity.get(x);
result = !(ec.hitBox.overlaps(rect));
}
}
return result;
}
To check for pre-emptive collisions i do this:
if(Gdx.input.isKeyPressed(Keys.D) && placeFree(x+10,y)){
x+=10;
}
Basically placeFree() actually places a copy of the current hitbox at a new position and checks if any collisions happen there. What i want to do is to check if the area to the left of my player is empty of collisions before moving left.
My current problem is if I add 3 instances of "Solid" (A subclass of entity) it only works on the first entity added. Why does this happen and how do i fix this?
This is what I mean (red box is normal hitBox,green is pre-emptive hitbox).
In this pic you can see that the pre-emptive hitBox just passes through:
But in this one it can't because i added that block first (Btw i used placeFree(x,y-100) to show the green box more clearly):