0

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: pic

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):

pic2

Aman
  • 86
  • 1
  • 6
  • It is not clear what your problem is. What does "it only works on the first entity" mean? What is "it"? Best to contrast what you expect to happen vs. what you actually see happening. – P.T. Mar 16 '13 at 04:55
  • Sorry, what is supposed to happen is my player entity should stop moving when a solid block comes within 10 pixels (thats what the placefree is for in the input check) – Aman Mar 16 '13 at 06:21
  • Are you familiar with Box2D? why you not use it? it will answer all your needs. – Aliaaa Mar 16 '13 at 09:13
  • Sure i am, but i'm currently developing an entity framework (more of a learning experience). It will have a physics entity that can deal with Box2d though – Aman Mar 16 '13 at 09:50

0 Answers0