title: Collision Detection in Slick 2D for Java
body: <p>I'm new to Java Game Development, as well as Slick 2D, and I'm making a platforming game like Mario or Sonic. Anyways, I'm having trouble with collision detection. I have the code set up, but my player keeps going through the map. I am using Tiled Map editor to make the maps. Here is the code for movement and collision detection</p>
  
  <p>Variables</p>
  
  <pre><code>   private float marioX = 12;
     private float marioY = 590;
     boolean jumping = false;        
     float verticalSpeed = 0.0f;     
     private Polygon playerPoly;
     public BlockMap map2;
  
  
  Input input = gc.getInput();
  
  if (input.isKeyDown(Input.KEY_LEFT)){
      mario = marioLeft;
      marioX-=5;
      playerPoly.setX(marioX);
      if (entityCollisionWith()){
          marioX+=5;
          playerPoly.setX(marioX);
      }
  
  }   
  if (input.isKeyDown(Input.KEY_RIGHT)){
      mario = marioRight;
      marioX+=5;
      playerPoly.setX(marioX);
      if (entityCollisionWith()){
          marioX-=5;
          playerPoly.setX(marioX);
      }
  }
  
  
  if (input.isKeyDown(Input.KEY_SPACE) &amp;&amp; !jumping) {
      mario = marioJumpRight;
      verticalSpeed = -.4f * delta;
      jumping = true;
  }
  if (jumping){
      verticalSpeed += .01f * delta;
  }
  
  marioY += verticalSpeed;
  playerPoly.setY(marioY);
  
  if (entityCollisionWith()){
      jumping = false;
      verticalSpeed = 0;
  }
  entityCollisionWith()
  
  public boolean entityCollisionWith() throws SlickException {
  for (int i = 0; i &lt; BlockMap.entities.size(); i++) {
      Blocks entity1 = (Blocks) BlockMap.entities.get(i);
      if (playerPoly.intersects(entity1.poly)) {
          return true;
      }       
  }       
  return false;
  }
  </code></pre>
  
  <p>BlockMap class</p>
  
  <pre><code>import java.util.ArrayList;
  import org.newdawn.slick.SlickException;
  import org.newdawn.slick.tiled.TiledMap;
  
  public class BlockMap {
  public static TiledMap tmap;
  public static int mapWidth;
  public static int mapHeight;
  private int square[] = {1,1,15,1,15,15,1,15}; //square shaped tile
  public static ArrayList&lt;Object&gt; entities;
  
  public BlockMap(String ref) throws SlickException {
  entities = new ArrayList&lt;Object&gt;();
  tmap = new TiledMap(ref, "res");
  mapWidth = tmap.getWidth() * tmap.getTileWidth();
  mapHeight = tmap.getHeight() * tmap.getTileHeight();
  
  for (int x = 0; x &lt; tmap.getWidth(); x++) {
      for (int y = 0; y &lt; tmap.getHeight(); y++) {
          int tileID = tmap.getTileId(x, y, 0);
          if (tileID == 1) {
              entities.add(
                                  new Blocks(x * 16, y * 16, square, "square")
                                  );
          }
      }
     }
   }
  }
  </code></pre>
  
  <p>Blocks Class</p>
  
  <pre><code>import org.newdawn.slick.Graphics;
  import org.newdawn.slick.geom.Polygon;
  
  public class Blocks  {
  public Polygon poly;
  public Blocks(int x, int y, int test[],String type) {
  poly = new Polygon(new float[]{
          x+test[0], y+test[1],
          x+test[2], y+test[3],
          x+test[4], y+test[5],
          x+test[6], y+test[7],
   });   
   }
  
  public void update(int delta) {
  }
  
  public void draw(Graphics g) {
     g.draw(poly);
   }
  }
  </code></pre>