title: Work out whether to turn clockwise or anticlockwise from two angles
body: <p>Im making a game in XNA.
  I have enemies and player.
  The enemies should turn gradually towards the player. They should work out whether they need to turn clockwise or anticlockwise, whichever is shorter.</p>
  
  <p>I got the angle the enemy is currently facing and the angle it should be facing (the angle of the line between the enemy and the player) as radians by using Atan2.</p>
  
  <p>I get some weird behavior though. Lets say in the scenario below. the enemy might turn all the way around in the wrong direction.</p>
  
  <p><img src="https://i.stack.imgur.com/uKAUg.png" alt="enter image description here"></p>
  
  <p>My code (below) keeps getting longer and I'm still having issues. This code is part of the enemy classes Update method.
  This must be a common problem to overcome in games. Is there some way of dealing with this?</p>
  
  <pre><code>            //this bit is just in case enemy has rotated more than 360 degrees (gets set back to 0)
              if (Math.Abs(_blocklist[0]._floor.Revolutions) &gt;= 2)
              {
                  _blocklist[0]._floor.Rotation = 0.0f;
              }
  
              //enemy rotation in radians          
              float blockroat = _blocklist[0]._floor.Rotation;
              // vector to player - vector to enemy
              _vectToPlayer = playerpos - _blocklist[0].Centre
              angletoplayer = (float)(Math.Atan2(_vectToPlayer.Y, _vectToPlayer.X));
              diff = blockroat - angletoplayer;
  
              if (diff &lt; -Math.PI)
              {
                  diff += (float) Math.PI;
                  diff = -diff;
              }
              else if (diff &gt; Math.PI)
              {
                  diff -= (float)Math.PI;
                  diff = -diff;
              }
  
              // if enemy angle if off by a certain amount
              if (Math.Abs(diff) &gt;_maxturn)
              {
                  if (diff &lt; 0)
                  {
                      //turn clockwise
                      _blocklist[0]._floor.Rotation += _maxturn;
                  }
                  else
                  {
                       //turn anti clockwise
                      _blocklist[0]._floor.Rotation -= _maxturn;
                  }
              }
  </code></pre>
  
  <hr>
  
  <h2>UPDATE</h2>
  
  <p>I ended up using method 2 like this.. Works perfectly. Also it is a lot neater than my previous code</p>
  
  <pre><code>            //enemy rotation in radians from farseer (red line)
              float brot = _blocklist[0]._floor.Rotation + ((float)Math.PI/2);
              //vector from enemy to player (blue line)
              Vector2 _vectToPlayer = playerpos - _blocklist[0].Centre;
              //cross product of 2d vectors
              cross = (_vectToPlayer.X * (float)Math.Sin(brot)) - ((float)Math.Cos(brot) * _vectToPlayer.Y);
  
              //tolerance for how closely enemy must point towards player
              if (Math.Abs(cross) &gt; 5)
              {
                  if (cross &gt; 0)
                  {
                      //turn anticlockwise
                      _blocklist[0]._floor.Rotation -= _npcstats.maxturnspeed;
                  }
                  else
                  {
                      //turn clockwise
                      _blocklist[0]._floor.Rotation += _npcstats.maxturnspeed;
                  } 
              }
  </code></pre>
  
  <p>I think that my previous code was more or less doing exactly the suggested method 1.
  But I could not get it to work.. I put this down to the vagaries of farseers coordinate system + how it interacted with my own.</p>