title: Pacman Collisions in Java
body: <p>I am making a pacman arcade game on Java but my collisions have problems.
  If you see my image
  <a href="https://i.stack.imgur.com/ZzQ3U.jpg" rel="nofollow noreferrer">here</a> my pacman sprite stands still (no x or y movement) when visiting a corner or when trying to turn back the direction it came (ie goes left but does not go right again). I understand this is because I set my xMovement to 0 and yMovement to 0 if a collision is detected (see second code block under collide()).</p>
  <p>How would I allow the pacman sprite to move the other way (ie it comes from the left and I want it to go right) or traverse a corner if my collision() does not allow this?</p>
  <p>Below is my draw method in the App.java class which draws the player based on the user's input. The keys wasd correspond with up, left, down, right respectively. this.direction is only used so the player does not waste a move nor go through a wall when a collision happens. *Note, I put <code>(this.player.x_ord + 2) % 16 == 0</code> because my pacman image is not a 16x16 image unlike my walls which are.</p>
  <pre><code>public void draw() { // This is an infinite loop
      mapDraw(); // this draws my map constantly so I do not have multiple pacman sprites drawn 
      this.player.tick(); // see second code below for details
      if (keyPressed){ // Used wasd rather than arrow keys
          if (key == 's' &amp;&amp; this.direction != 's' &amp;&amp; (this.player.x_ord + 2) % 16 == 0 &amp;&amp; 
          (this.player.y_ord + 4) % 16 == 0){ // We dont want Player to turn ever on a non 16 divisible area
              this.player.p = this.loadImage(&quot;src/main/resources/playerDown.png&quot;);
              this.player.yMovement = this.player.speed;
              this.player.xMovement = 0;
              this.direction = 's';
          }
          else if (key == 'w' &amp;&amp; this.direction != 'w' &amp;&amp; (this.player.x_ord + 2) % 16 == 0 &amp;&amp; 
          (this.player.y_ord + 4) % 16 == 0){
              this.player.p = this.loadImage(&quot;src/main/resources/playerUp.png&quot;);
              this.player.yMovement = -this.player.speed;
              this.player.xMovement = 0; // I do not want my pacman to move diagonally so thats why
              this.direction = 'w';
  
          }
          else if (key == 'd' &amp;&amp; this.direction != 'd' &amp;&amp; (this.player.x_ord + 2) % 16 == 0 &amp;&amp; 
          (this.player.y_ord + 4) % 16 == 0){
              this.player.p = this.loadImage(&quot;src/main/resources/playerRight.png&quot;);
              this.player.xMovement = this.player.speed;
              this.player.yMovement = 0;
              this.direction = 'd';
          }
          else if (key == 'a' &amp;&amp; this.direction != 'a' &amp;&amp; (this.player.x_ord + 2) % 16 == 0 &amp;&amp; 
          (this.player.y_ord + 4) % 16 == 0){
              this.player.p = this.loadImage(&quot;src/main/resources/playerLeft.png&quot;);
              this.player.xMovement = -this.player.speed;
              this.player.yMovement = 0;
              this.direction = 'a';
          }
      }
      this.player.draw(this); //see second code below for details
  }
  </code></pre>
  <p>Below is my player.java class
  *Note again that <code>this.x_ord + 2 == w.x_pos + 16 &amp;&amp; this.y_ord + 4 == w.y_pos</code> is offset because of my pacman sprite is larger than my 16x16 walls.</p>
  <pre><code>   public void tick(){
      // //logic   
      this.detectCollision();
      if (this.isLiving){
          this.y_ord += this.yMovement;
          this.x_ord += this.xMovement;
      }
  }
  
  public void draw(PApplet a){ //just draw the sprite
      if (this.isLiving){
          a.image(this.p, this.x_ord, this.y_ord);
      }
      
  }
  
  public void collide(boolean isX){ // Is it an x or y collision
      if (isX == true){ // If it moves left or right into a wall
          this.xMovement = 0;
      }
      else if (isX == false){ // If it moves up or down into a wall
          this.xMovement = 0;
      }
  }
  
  public void detectCollision(){
      for (Walls w: this.wallLocations){ // A list of wall locations from a .txt file
          if (this.x_ord + 2 == w.x_pos + 16 &amp;&amp; this.y_ord + 4 == w.y_pos){ // Detect left movement into right wall piece
              collide(true);
          }
  
          if (this.x_ord + 2 + 16 == w.x_pos &amp;&amp; this.y_ord + 4 == w.y_pos){ // Detect right movement into left wall piece
              collide(true);
          }
  
          if (this.y_ord + 4 == w.y_pos + 16 &amp;&amp; this.x_ord + 2 == w.x_pos){ // Detect up movement into bottom wall piece
              collide(false);
          }
  
          if (this.y_ord + 4 + 16 == w.y_pos &amp;&amp; this.x_ord + 2 == w.x_pos){ // Detect down movement into top wall piece
              collide(false);
          }
      }
  </code></pre>
  <p>Any help to my problem is much appreciated.</p>