title: Java Platform Game Collision
body: <p>I'm trying to make the player not be able to go through the platform from the left and right, and make the player stop on top of the platform. All I have for collision so far is left and right but the right isn't working and the left is bugging. I can't figure out how to do the basic collision of a platform game, can anyone give me a working solution?</p>
  
  <p>Player class code:</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.Rectangle;
  import java.awt.event.KeyEvent;
  import java.awt.event.KeyListener;
  
  
  public class Player implements KeyListener
  {
  int x, y, width, height;
  
  boolean jump, left, right;
  
  PlayerThread playerThread;
  
  int maxHeight = 40;
  double heightC = 0;
  boolean onPlatform = false;
  boolean landed = true;
  int prevY;
  int prevX;
  Rectangle playRect = new Rectangle();
  boolean leftCollision = false;
  boolean rightCollision = false;
  Platform p;
  
  public Player()
  {
      x = 500;
      y = 350;
      width = 40;
      height = 50;
      playerThread = new PlayerThread();
      playerThread.start();
  }
  
  public void paint(Graphics g)
  {
      String str = String.valueOf(x);
      String str2 = String.valueOf(y);
  
      g.setColor(Color.RED);
      playRect.setBounds(x, y, width, height);
      g.fillRect(x, y, width, height);
  
      g.drawString("X: " + str + ", Y: " + str2, 100, 100);
  }
  
  public void update(Platform p)
  {
      this.p = p;
      CheckForCollision(p);
  
  }
  
  public void CheckForCollision(Platform p)
  {
      int pX = p.getX();
      int pY = p.getY();
      int pWidth = p.getWidth();
      int pHeight = p.getHeight();
  
      //COLLISION WITH PLATFORM CODE
      if (playRect.intersects(p.plat) &amp;&amp; left == true &amp;&amp; !jump &amp;&amp; !right &amp;&amp; landed)
      {
          System.out.println("LEFT");
          x = prevX;
          leftCollision = true;
      }
      else
      {
          leftCollision = false;
      }
  
      if (playRect.intersects(p.plat) &amp;&amp; right == true &amp;&amp; !jump &amp;&amp; !right &amp;&amp; landed)
      {
          System.out.println("RIGHT");
          x = prevX;
          rightCollision = true;
      }
      else
      {
          rightCollision = false;
      }
  }
  
  public class PlayerThread extends Thread implements Runnable
  {
      public void run()
      {
          while (true)
          {
              if (left &amp;&amp; !leftCollision)
              {
  
                  prevX = x;
                  x -= 2;
  
              }
  
              if (right &amp;&amp; !rightCollision)
              {
                  prevX = x;
                  x += 2;
              }
  
              if (jump)
              {
                  if (heightC &gt;= maxHeight)
                  {
                      System.out.println(heightC);
                      jump = false;
                      heightC = 0;
                  }
                  else
                  {
                      heightC += 1.5;
                      prevY = y;
                      y -= 5;
                      landed = false;
                  }
              }
  
              //GRAVITY CODE
              if (!jump &amp;&amp; !landed)
              {
                  if (y &lt; 400 - height)
                  {
                      prevY = y;
                      y += 5;
                      //landed = false;
                  }
              }
  
              if (y &gt;= 400 - height &amp;&amp; !landed)
              {
                  y = 400 - height;
                  landed = true;
              }
  
              try
              {
                  sleep(17);
              } 
              catch (InterruptedException e)
              {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      }
  }
  
  @Override
  public void keyPressed(KeyEvent e)
  {
      switch (e.getKeyCode())
      {
      case KeyEvent.VK_LEFT:
  
          left = true;
          break;
      case KeyEvent.VK_RIGHT:
          right = true;
          break;
      case KeyEvent.VK_SPACE:
          if (landed)
          {
          jump = true;
          }
          break;
      }
  }
  
  @Override
  public void keyReleased(KeyEvent e)
  {
      switch (e.getKeyCode())
      {
      case KeyEvent.VK_LEFT:
          left = false;
          break;
      case KeyEvent.VK_RIGHT:
          right = false;
          break;
      case KeyEvent.VK_SPACE:
          break;
      }
  }
  
  
  
  public int getX()
  {
      return x;
  }
  
  public void setX(int x)
  {
      this.x = x;
  }
  
  public int getY()
  {
      return y;
  }
  
  public void setY(int y)
  {
      this.y = y;
  }
  
  public int getWidth()
  {
      return width;
  }
  
  public void setWidth(int width)
  {
      this.width = width;
  }
  
  public int getHeight()
  {
      return height;
  }
  
  public void setHeight(int height)
  {
      this.height = height;
  }
  
  @Override
  public void keyTyped(KeyEvent e)
  {
      // TODO Auto-generated method stub
  
  }
  }
  </code></pre>