title: LibGDX - Collision detection between Player and a Platform (or tile) WITHOUT using TiledMap
body: <p>In my game the character is constantly moving from left to right, so there will never be a situation where he hits the right side of a platform. I had a very brief talk with somebody and he suggested me to model my platform as 4 rectangles so that I would only need 3 cases in my algorithm (i.e. <strong>Player</strong> can only collide with the rectangles shaded green in the image below). Here's a visual description of what I mean: </p>
  
  <p><img src="https://i.stack.imgur.com/EDlYs.png" alt="enter image description here"></p>
  
  <p>I gave it a try but the collision detection is still messed up. It doesn't go through the side of the platform (this is good news since we want this to happen) but you can't glide on top of it or bounce under it. Can anyone see where I may have gone wrong?</p>
  
  <pre><code>public void checkCollisions(ArrayList&lt;GameObject&gt; list) {
  
      for (int i = 0; i &lt; list.size(); i++) {
          for (int j = i + 1; j &lt; list.size(); j++) {
              GameObject go1 = list.get(i);
              GameObject go2 = list.get(j);
              boolean playerPlatformCollision = go1.getType()
                      .equalsIgnoreCase("player") &amp;&amp; go2.getType().equalsIgnoreCase("platform");
              if (playerPlatformCollision) {
                Rectangle playerRect = go1.getRect();
                Rectangle platformRect = go2.getRect();
                  if (playerRect.overlaps(platformRect)) {
  
                      float platformRectX, platformRectY, platformRectWidth, platformRectHeight;
                      platformRectX = go2.getRect().getX(); // x position of the platform
                      platformRectY = go2.getRect().getY(); // y position of the platform
                      platformRectWidth = go2.getRect().getWidth(); // width of platform
                      platformRectHeight = go2.getRect().getHeight(); // height of platform
  
                      Rectangle caseA = new Rectangle(platformRectX,
                              platformRectY, platformRectX + 1,
                              platformRectHeight);
                      Rectangle caseB = new Rectangle(platformRectX + 1,
                              (platformRectY + platformRectHeight) - 1,
                              platformRectWidth - 1, platformRectHeight);
                      Rectangle caseC = new Rectangle(platformRectX + 1,
                              platformRectY, platformRectWidth, 1);
  
                      if (playerRect.overlaps(caseA)) {
                          go1.setxSpeed(0);
                      } else if (playerRect.overlaps(caseB)) {
                          go1.setySpeed(0);
                      } else if (playerRect.overlaps(caseC)) {
                          go1.setySpeed(-3f);
                      }
                  }
              }
          }
      }
  } 
  </code></pre>
  
  <p>Please let me know if you are not clear with any parts of the code. </p>