title: how to stop enemies overlapping java
body: <p>I have been trying to figure out how to stop my enemies from overlapping one another for the past week. I am using a linked list of enemies. I loop through the list and set an <code>oldX</code>and <code>oldY</code> position, which is the position I want the zombie to move back to if they overlap. My problem is that in the beginning the collision detection works but eventually they end up both sticking together and not moving, the below image shows the state they end up in. They just stick together and don't move. If anyone can help me with this or explain how to manage this sort of collision detection that would be great as I can't find anything that clearly explains how it is done! </p>
  
  <p><a href="https://i.stack.imgur.com/WMqq3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WMqq3.png" alt="enter image description here"></a></p>
  
  <p>This is my basic AI method for moving the zombies, it tests to see if there is no collision with a wall, then checks if that zombie overlaps with any other. Then moves the zombie based on the players position.</p>
  
  <pre><code>public void moveZombie(){
          for(Zombie zombie : c.z)
          {
              oldX = zombie.getX();
              oldY = zombie.getY();
  
              if(player.getX() &gt; zombie.getX()){
                  if(wallCollision(zombie.getX()+1, zombie.getY()) == false){
                      if(zombiesCollision(zombie) == false){
                          zombie.setX(zombie.getX() + 1);
                      }
  
                  }
              }
  
              if(player.getX() &lt; zombie.getX()){
                  if(wallCollision(zombie.getX()-1, zombie.getY()) == false){
                      if(zombiesCollision(zombie) == false){
                          zombie.setX(zombie.getX() - 1);
                      }
  
  
                  }
              }
  
              if(player.getY() &gt; zombie.getY()){
                  if(wallCollision(zombie.getX(), zombie.getY()+1) == false){
                      if(zombiesCollision(zombie) == false){
                          zombie.setY(zombie.getY() + 1);
                      }
  
                  }
              }
  
              if(player.getY() &lt; zombie.getY()){
                  if(wallCollision(zombie.getX(), zombie.getY()-1) == false){
                      if(zombiesCollision(zombie) == false){
                          zombie.setY(zombie.getY() - 1);
                      }
                  }
              }
          }
  
  }
  </code></pre>
  
  <p>This is my Boolean method which tests if the zombies collide. It is called from my <code>move()</code> method above, and takes in the current zombie from the loop above as a parameter. Then it checks if they intersect. If they do the x and y position of the zombie is set to the <code>oldX</code> and <code>oldY</code> position and returns true. If there was a intersection I tried <code>zombie.x -= 32;</code> and <code>zombie.y -=32</code>. which works but it is very glitchy and they often get stuck in the walls, I want to know how i can move the zombie back to its old position basically.</p>
  
  <pre><code>public boolean zombiesCollision(Zombie zombie){
  
              for(Zombie zombie2 : c.z){
                      if(zombie != zombie2){
                          if(zombie2.getZombieBounds().intersects(zombie.getZombieBounds())){
                              zombie.x = oldX;
                              zombie.y = oldY;
                              return true;
                          }
                      }
              }
  
          return false;
  }
  </code></pre>