title: Pre-emptive collisions with LibGdx
body: <p>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. </p>
  
  <p><a href="https://gist.github.com/MATheCoder/5174704" rel="nofollow noreferrer">My QuadTree</a> (Its pretty long so i figured id just link to it)</p>
  
  <p>In the Entity class i have a placeFree method:</p>
  
  <pre><code>public boolean placeFree(float px, float py){
  
      //the pre-emptive collision Rectangle
      preCollBox.set(px,py,hitBox.getWidth(),hitBox.getHeight());
  
      return screen.placeFree(preCollBox);
  
  }
  </code></pre>
  
  <p>The screen.placeFree method:</p>
  
  <pre><code>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 &lt; collideEnt.size; i++) {
            Qtree.insert(collideEnt.get(i));
      }
  
      for(int i  = 0; i &lt; collideEnt.size;i++){
          returnEntity.clear();
          Qtree.retrieve(returnEntity, rect);
          for (int x = 0; x &lt; returnEntity.size; x++) {
              Entity ec = returnEntity.get(x);
                  result = !(ec.hitBox.overlaps(rect));
           }
      }
  
      return result;
  
  }
  </code></pre>
  
  <p>To check for pre-emptive collisions i do this:</p>
  
  <pre><code>if(Gdx.input.isKeyPressed(Keys.D) &amp;&amp; placeFree(x+10,y)){
          x+=10;
      }
  </code></pre>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>This is what I mean (red box is normal hitBox,green is pre-emptive hitbox).</p>
  
  <p>In this pic you can see that the pre-emptive hitBox just passes through:
  <img src="https://i.stack.imgur.com/N4L2g.png" alt="pic"></p>
  
  <p>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):</p>
  
  <p><img src="https://i.stack.imgur.com/hQZV4.png" alt="pic2"></p>