Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualP0jahn

Posted 16 January 2013 - 10:25 AM

Thanks all, it is fully working now. Here is the complete code if anyone is interested:

 

 

public static float rotateTowardsPoint(float srcX, float srcY, float targetX, float targetY, float currRotation, float speed)
{
   float destinationRotation = (float) (Math.atan2(srcY - targetY, srcX - targetX) + Math.PI);
   currRotation = (float) Math.toRadians(currRotation);

   if(Math.abs((currRotation + 180 -  destinationRotation) % 360 - 180) < speed)
      currRotation = destinationRotation;
   else
   {
      if (destinationRotation > currRotation)
      {
          if (currRotation < destinationRotation - Math.PI)
             currRotation -= speed;
          else
             currRotation += speed;
      }
      else if (destinationRotation  < currRotation)
      {
          if (currRotation > destinationRotation + Math.PI)
             currRotation += speed;
          else
             currRotation -= speed;
      } 
      if (currRotation > Math.PI * 2.0f) currRotation = 0;
      if (currRotation < 0) currRotation = (float) (Math.PI * 2.0f);
   }
   return (float) Math.toDegrees(currRotation);
}

#1P0jahn

Posted 16 January 2013 - 10:18 AM

Thanks all, it is fully working now. Here is the complete code if anyone is interested:

 

public static float rotateTowardsPoint(float srcX, float srcY, float targetX, float targetY, float currRotation, float speed)
{
   float destinationRotation = (float) (Math.atan2(srcY - targetY, srcX - targetX) + Math.PI);
   currRotation = (float) Math.toRadians(currRotation);

   if(Math.abs((currRotation + 180 -  destinationRotation) % 360 - 180) < speed)
   currRotation = destinationRotation;
   else
   {
      if (destinationRotation > currRotation)
      {
          if (currRotation < destinationRotation - Math.PI)
             currRotation -= speed;
          else
             currRotation += speed;
      }
      else if (destinationRotation  < currRotation)
      {
          if (currRotation > destinationRotation + Math.PI)
           currRotation += speed;
          else
           currRotation -= speed;
      } 
      if (currRotation > Math.PI * 2.0f) currRotation = 0;
      if (currRotation < 0) currRotation = (float) (Math.PI * 2.0f);
   }
   return (float) Math.toDegrees(currRotation);
}

PARTNERS