title: Java: Help determining a collision between two rectangles
body: <p>I'm creating a game, pacman specifically as part of my coursework and I'm having problems when it comes to collision-detection using Rectangles. </p>
  
  <p>The problem I'm experiencing is that even though on screen it is clear to see that the characters aren't colliding checking the intersection is practically always returning true. The output below explains what I mean:</p>
  
  <blockquote>
    <p>Pacman details: x 17.0 y 16.0 
    Inky details: x 22.0 y 13.0
    Collision after calling intersects(): true
    Pacman details: x 18.0 y 16.0 
    Inky details: x 23.0 y 13.0
    Collision after calling intersects(): true</p>
  </blockquote>
  
  <p>I have the Rectangles set up as follows:  </p>
  
  <pre><code>public Rectangle pacmanBounds(){
      return new Rectangle(pacRow, pacColumn, 22, 22);
  }
  public Rectangle ghostBounds(){
      return new Rectangle(ghostRow, ghostColumn, 22, 22);
  }
  </code></pre>
  
  <p>The height and width have been hardcoded for testing purposes, but these are the actual image size.</p>
  
  <p>I'm checking for collision as follows everytime the JPanel is repainted:</p>
  
  <pre><code>public boolean checkCollision(){
      Rectangle pacmanBounds = pacman.pacmanBounds();
      //currently commented out for testing
      /*if(pacmanBounds.intersects(inky.ghostBounds()) || pacmanBounds.intersects(blinky.ghostBounds())
              || pacmanBounds.intersects(pinky.ghostBounds()) || pacmanBounds.intersects(clyde.ghostBounds())){
          System.out.println("Oh no!");
          return true;
      }*/
  
      System.out.println("Pacman details: x " + pacmanBounds.getX() + " y " + pacmanBounds.getY() + " ");
      System.out.println("Inky details: x " + inky.ghostBounds().getX() + " y " + inky.ghostBounds().getY());
      System.out.println("Collision after calling intersects(): " + pacmanBounds.intersects(inky.ghostBounds()));
      return false;
  }
  </code></pre>
  
  <p>At this point in time I've pretty much ran out of ideas on how to solve this problem so any advice you guys can give would be greatly appreciated!</p>