title: Rotating image in Andengine
body: <p>Let's suppose there are bugs coming into the screen from outside. To do this, we need to have 4 coordinates (StartX, StartY, TargetX, TargetY). How to rotate the bugs to the appropriate direction? I have been messing with this for hours and have been unable to figure out this part. So the bug comes into the screen but it is not facing the right direction. (The four coordinates are random numbers).</p>
  
  <p>I am counting the gradient of the line as I learned in school:</p>
  
  <pre class="lang-java prettyprint-override"><code>int deltaY = TargetY - StartY;
  int deltaX = TargetX - StartX;
  float gradient = (float)deltaY / (float)deltaX;
  </code></pre>
  
  <p>Then the angle to rotate the bug:</p>
  
  <pre class="lang-java prettyprint-override"><code>float angle = (float) (Math.atan(gradient) * 180 / PI);
  </code></pre>
  
  <p>Then here is the rotation and move modifier:</p>
  
  <pre class="lang-java prettyprint-override"><code>registerEntityModifier(new RotationModifier(0.3f, 0, angle) {
      @Override
      protected void onModifierFinished(IEntity pItem) {
          super.onModifierFinished(pItem);
  
          registerEntityModifier(new MoveModifier(0.8f, StartX, TargetX, StartY, TargetY) {
              @Override
              protected void onModifierFinished(IEntity pItem) {
                  super.onModifierFinished(pItem);
              }
          });
      }
  });
  </code></pre>
  
  <p>The solution is more complex since on a line of a certain gradient the bug can come from two directions so it can face two directions.
  I know that a gradient of 1/2 means 26.56 degrees and in the AndEngine coordinate system it means -26.56 or -206.56 degrees depending on which side the bug comes from.</p>