title: Rectangle Wall collision in Java
body: <p>I really could use some help in order to find a working solution for my game.
  My game is almost done, but the walls in my game are still not working as they should. </p>
  
  <p>I have tried to find a solution on the internet for this problem, but i still haven't found a simple way to stop a rectangle just before it will collide with a wall (another rectangle).</p>
  
  <p>Right now i have implemented a collision detection between the player rectangle and the wall rectangle and then stopped it to move, but then it gets stuck inside a wall when it hits. </p>
  
  <p>Want it to stop just before, so it still can move. The code i have done this with so far is here:</p>
  
  <p><strong>Pacman Class</strong></p>
  
  <pre><code>public class Pacman {
  
  private String pacmanup = "pacmanup.png";
  private String pacmandown = "pacmandown.png";
  private String pacmanleft = "pacmanleft.png";
  private String pacmanright = "pacmanright.png";
  
  private int dx;
  private int dy;
  private int x;
  private int y;
  private int width;
  private int height;
  private boolean visible;
  
  private Image imageup;
  private Image imagedown;
  private Image imageleft;
  private Image imageright;
  
  public Pacman() {
  
      ImageIcon i1 = new ImageIcon(this.getClass().getResource(pacmanup));
      imageup = i1.getImage();
  
      ImageIcon i2 = new ImageIcon(this.getClass().getResource(pacmandown));
      imagedown = i2.getImage();
  
      ImageIcon i3 = new ImageIcon(this.getClass().getResource(pacmanleft));
      imageleft = i3.getImage();
  
      ImageIcon i4 = new ImageIcon(this.getClass().getResource(pacmanright));
      imageright = i4.getImage();
  
      width = imageup.getWidth(null);
      height = imageup.getHeight(null);
      visible = true;
      x = 270;
      y = 189;
  
  }
  
  public int getDx() {
      return dx;
  }
  
  public void setDx(int dx) {
      this.dx = dx;
  }
  
  public int getDy() {
      return dy;
  }
  
  public void setDy(int dy) {
      this.dy = dy;
  }
  
  public int getX() {
      return x;
  }
  
  public int getY() {
      return y;
  }
  
  public void setX(int x) {
      this.x = x;
  }
  
  public void setY(int y) {
      this.y = y;
  }
  
  public Image getImageup() {
      return imageup;
  }
  
  public Image getImagedown() {
      return imagedown;
  }
  
  public Image getImageleft() {
      return imageleft;
  }
  
  public Image getImageright() {
      return imageright;
  }
  
  public void setVisible(boolean visible) {
      this.visible = visible;
  }
  
  public boolean isVisible() {
      return visible;
  }
  
  public Rectangle getBounds() {
      return new Rectangle(x, y, width, height);
  }
  
  public void move() {
  
      x += dx;
      y += dy;
  }
  
  public void keyPressed(KeyEvent e) {
  
      int key = e.getKeyCode();
  
      if (key == KeyEvent.VK_LEFT) {
          dx = -2;
          dy = 0;
      }
  
      if (key == KeyEvent.VK_RIGHT) {
          dx = 2;
          dy = 0;
      }
  
      if (key == KeyEvent.VK_UP) {
          dx = 0;
          dy = -2;
      }
  
      if (key == KeyEvent.VK_DOWN) {
          dx = 0;
          dy = 2; 
      }
  }
  </code></pre>
  
  <p>Here i have created a Rectangle getBounds method which i use to create an rectangle of the pacman and place an image over it.</p>
  
  <p><strong>Barrier class / Wall class</strong></p>
  
  <pre><code>public class Barrier {
  
  private String barrier = "barrier.png";
  
  private int x;
  private int y;
  private int width;
  private int height;
  private boolean visible;
  private Image image;
  
  public Barrier(int x, int y) {
      ImageIcon ii = new ImageIcon(this.getClass().getResource(barrier));
      image = ii.getImage();      
      width = image.getWidth(null);
      height = image.getHeight(null);
      visible = true;
      this.x = x;
      this.y = y;
  }
  
  public int getX() {
      return x;
  }
  
  public int getY() {
      return y;
  }
  
  public boolean isVisible() {
      return visible;
  }
  
  public void setVisible(Boolean visible) {
      this.visible = visible;
  }
  
  public Image getImage() {
      return image;
  }
  
  public Rectangle getBounds() {
      return new Rectangle(x, y, width, height);
  }
  </code></pre>
  
  <p>This class also have the Rectangle getBounds class which i use to detect collision. </p>
  
  <p>The last code i show is how i do the collision detection so far:</p>
  
  <p><strong>Code inside Board class</strong></p>
  
  <pre><code>Rectangle r3 = pacman.getBounds();
  
   for (int j = 0; j&lt;barriers.size(); j++) {
          Barrier b = (Barrier) barriers.get(j);
          Rectangle r4 = b.getBounds();
  
          if (r3.intersects(r4)) {
              System.out.println("Wall hit");
              pacman.setDx(0);
              pacman.setDy(0);
          }
      }
  </code></pre>
  
  <p>Well, what i do, if there is a collision between r3 and r4, i gonna set Dx and Dy to 0.. what i want to find another solution so it detect for collision but i wont get stuck inside a wall, but i don't know how to :/</p>
  
  <p>Hope someone will help.</p>