title: Having Issue With Enemy AI - Java
body: <p>I am having an issue with enemy AI. The AI I am using right now is a dog. </p>
  
  <p>What the dog is supposed to do -</p>
  
  <p>The dog is supposed to follow the player if the player is within 200px of the dog. It's supposed to stay 100 pixels away from the character at all times. The dog will later shoot projectiles, which I can do easily</p>
  
  <p>My main issue is that the dog seems to just be attacking the player from it's origin, (0,0), or the top left of the player image.</p>
  
  <p>I tried adding 8 points around the player that the dog will constantly check on a loop which one he is closest to. He will move towards whichever one he is closest to, thus following the player but at a distance.</p>
  
  <p>This is how I tried accomplishing this:</p>
  
  <pre><code>int p1x,p1y,p2x,p2y,p3x,p3y,p4x,p4y,p5x,p5y,p6x,p6y,p7x,p7y,p8x,p8y,p1final,p2final,p3final,p4final,p5final,p6final,p7final,p8final;
      p1x = Game.px - 350;
      p1y = Game.py - 350;
      p2x = Game.px;
      p2y = Game.py - 350;
      p3x = Game.px + 350;
      p3y = Game.py - 350;
      p4x = Game.px + 350;
      p4y = Game.py;
      p5x = Game.px + 350;
      p5y = Game.py + 350;
      p6x = Game.px;
      p6y = Game.py + 350;
      p7x = Game.px - 350;
      p7y = Game.py + 350;
      p8x = Game.px - 350;
      p8y = Game.py;
      p1final = 0;
      p2final = 0;
      p3final = 0;
      p4final = 0;
      p5final = 0;
      p6final = 0;
      p7final = 0;
      p8final = 0;
          if(x &gt;= p1x){
              p1final = p1final + (x - p1x);
          }
          if(y &lt; p1y){
              p1final = p1final + (p1y - y);
          }
          if(x &lt; p1x){
              p1final = p1final + (p1x - x);
          }
          if(y &gt;= p1y){
              p1final = p1final + (y - p1y);
          }
          if(x &gt;= p2x){
              p2final = p2final + (x - p2x);
          }
          if(y &lt; p2y){
              p2final = p2final + (p2y - y);
          }
          if(x &lt; p2x){
              p2final = p2final + (p2x - x);
          }
          if(y &gt;= p2y){
              p2final = p2final + (y - p2y);
          }
          if(x &gt;= p3x){
              p3final = p3final + (x - p3x);
          }
          if(y &lt; p3y){
              p3final = p3final + (p3y - y);
          }
          if(x &lt; p3x){
              p3final = p3final + (p3x - x);
          }
          if(y &gt;= p3y){
              p3final = p3final + (y - p3y);
          }
          if(x &gt;= p4x){
              p4final = p4final + (x - p4x);
          }
          if(y &lt; p4y){
              p4final = p4final + (p4y - y);
          }
          if(x &lt; p4x){
              p4final = p4final + (p4x - x);
          }
          if(y &gt;= p4y){
              p4final = p4final + (y - p4y);
          }
          if(x &gt;= p5x){
              p5final = p5final + (x - p5x);
          }
          if(y &lt; p5y){
              p5final = p5final + (p5y - y);
          }
          if(x &lt; p5x){
              p5final = p5final + (p5x - x);
          }
          if(y &gt;= p5y){
              p5final = p5final + (y - p5y);
          }
          if(x &gt;= p6x){
              p6final = p6final + (x - p6x);
          }
          if(y &lt; p6y){
              p6final = p6final + (p6y - y);
          }
          if(x &lt; p6x){
              p6final = p6final + (p6x - x);
          }
          if(y &gt;= p6y){
              p6final = p6final + (y - p6y);
          }
          if(x &gt;= p7x){
              p7final = p7final + (x - p7x);
          }
          if(y &lt; p7y){
              p7final = p7final + (p7y - y);
          }
          if(x &lt; p7x){
              p7final = p7final + (p7x - x);
          }
          if(y &gt;= p7y){
              p7final = p7final + (y - p7y);
          }
          if(x &gt;= p8x){
              p8final = p8final + (x - p8x);
          }
          if(y &lt; p8y){
              p8final = p8final + (p8y - y);
          }
          if(x &lt; p8x){
              p8final = p8final + (p8x - x);
          }
          if(y &gt;= p8y){
              p8final = p8final + (y - p8y);
          }
      // Now checks which is the highest
      // 1
      if(p1final &gt; p2final &amp;&amp; p1final &gt; p3final &amp;&amp; p1final &gt; p4final &amp;&amp; p1final &gt; p5final &amp;&amp; p1final &gt; p6final &amp;&amp; p1final &gt; p7final &amp;&amp; p1final &gt; p8final){
          System.out.println("P1Final");
          if(x &gt; p1x){
              x-=7;
              Game.dogrepaint = true;
          }
          if(x &lt; p1x){
              x+=7;
              Game.dogrepaint = true;
          }
          if(y &gt; p1y){
              y-=7;
              Game.dogrepaint = true;
          }
          if(y &lt; p1y){
              y+=7;
              Game.dogrepaint = true;
          }
      }
      // 2
      if(p2final &gt; p1final &amp;&amp; p2final &gt; p3final &amp;&amp; p2final &gt; p4final &amp;&amp; p2final &gt; p5final &amp;&amp; p2final &gt; p6final &amp;&amp; p2final &gt; p7final &amp;&amp; p2final &gt; p8final){
          System.out.println("P2Final");
          if(x &gt; p2x){
              x-=7;
              Game.dogrepaint = true;
          }
          if(x &lt; p2x){
              x+=7;
              Game.dogrepaint = true;
          }
          if(y &gt; p2y){
              y-=7;
              Game.dogrepaint = true;
          }
          if(y &lt; p2y){
              y+=7;
              Game.dogrepaint = true;
          }
      }
      // 3
      if(p3final &gt; p1final &amp;&amp; p3final &gt; p2final &amp;&amp; p3final &gt; p4final &amp;&amp; p3final &gt; p5final &amp;&amp; p3final &gt; p6final &amp;&amp; p3final &gt; p7final &amp;&amp; p3final &gt; p8final){
          System.out.println("P3Final");
          if(x &gt; p3x){
              x-=7;
              Game.dogrepaint = true;
          }
          if(x &lt; p3x){
              x+=7;
              Game.dogrepaint = true;
          }
          if(y &gt; p3y){
              y-=7;
              Game.dogrepaint = true;
          }
          if(y &lt; p3y){
              y+=7;
              Game.dogrepaint = true;
          }
      }
      // 4
      if(p4final &gt; p1final &amp;&amp; p4final &gt; p2final &amp;&amp; p4final &gt; p3final &amp;&amp; p4final &gt; p5final &amp;&amp; p4final &gt; p6final &amp;&amp; p4final &gt; p7final &amp;&amp; p4final &gt; p8final){
          System.out.println("P4Final");
          if(x &gt; p4x){
              x-=7;
              Game.dogrepaint = true;
          }
          if(x &lt; p4x){
              x+=7;
              Game.dogrepaint = true;
          }
          if(y &gt; p4y){
              y-=7;
              Game.dogrepaint = true;
          }
          if(y &lt; p4y){
              y+=7;
              Game.dogrepaint = true;
          }
      }
      // 5
      if(p5final &gt; p1final &amp;&amp; p5final &gt; p2final &amp;&amp; p5final &gt; p3final &amp;&amp; p5final &gt; p4final &amp;&amp; p5final &gt; p6final &amp;&amp; p5final &gt; p7final &amp;&amp; p5final &gt; p8final){
          System.out.println("P5Final");
          if(x &gt; p5x){
              x-=7;
              Game.dogrepaint = true;
          }
          if(x &lt; p5x){
              x+=7;
              Game.dogrepaint = true;
          }
          if(y &gt; p5y){
              y-=7;
              Game.dogrepaint = true;
          }
          if(y &lt; p5y){
              y+=7;
              Game.dogrepaint = true;
          }
      }
      // 6
      if(p6final &gt; p1final &amp;&amp; p6final &gt; p2final &amp;&amp; p6final &gt; p3final &amp;&amp; p6final &gt; p4final &amp;&amp; p6final &gt; p5final &amp;&amp; p6final &gt; p7final &amp;&amp; p6final &gt; p8final){
          System.out.println("P6Final");
          if(x &gt; p6x){
              x-=7;
              Game.dogrepaint = true;
          }
          if(x &lt; p6x){
              x+=7;
              Game.dogrepaint = true;
          }
          if(y &gt; p6y){
              y-=7;
              Game.dogrepaint = true;
          }
          if(y &lt; p6y){
              y+=7;
              Game.dogrepaint = true;
          }
      }
      // 7
      if(p7final &gt; p1final &amp;&amp; p7final &gt; p2final &amp;&amp; p7final &gt; p3final &amp;&amp; p7final &gt; p4final &amp;&amp; p7final &gt; p5final &amp;&amp; p7final &gt; p6final &amp;&amp; p7final &gt; p8final){
          System.out.println("P7Final");
          if(x &gt; p7x){
              x-=7;
              Game.dogrepaint = true;
          }
          if(x &lt; p7x){
              x+=7;
              Game.dogrepaint = true;
          }
          if(y &gt; p7y){
              y-=7;
              Game.dogrepaint = true;
          }
          if(y &lt; p7y){
              y+=7;
              Game.dogrepaint = true;
          }
      }
      // 8
      if(p8final &gt; p1final &amp;&amp; p8final &gt; p2final &amp;&amp; p8final &gt; p3final &amp;&amp; p8final &gt; p4final &amp;&amp; p8final &gt; p5final &amp;&amp; p8final &gt; p6final &amp;&amp; p8final &gt; p7final){
          System.out.println("P8Final");
          if(x &gt; p8x){
              x-=7;
              Game.dogrepaint = true;
          }
          if(x &lt; p8x){
              x+=7;
              Game.dogrepaint = true;
          }
          if(y &gt; p8y){
              y-=7;
              Game.dogrepaint = true;
          }
          if(y &lt; p8y){
              x+=7;
              Game.dogrepaint = true;
          }
      }
  }
  </code></pre>
  
  <p>Since that is basically gibberish and just a long line of if statements, and it still isn't working, I'm trying to find a much simpler or actual working way of doing this.</p>
  
  <p>TLDR: The dog won't follow the player at a range of 100 pixels at all times.</p>