I currently have an Asteroids-style character that is controlled by the mouse.
However, I'm having an issue when my ship's movement would have it turn across the "west" line.
This is my current code for rotating:
if (destAngle - rotation <= 0)
{
rotation -= movingRotationSpeed;
}
else
{
rotation += movingRotationSpeed;
}
However, an issue arises when the mouse crosses over the "west" line (with respect to the sprite). The destAngle changes from -3.140 to +3.140 (radians/PI). If I'm going clockwise at the time, the ship will then do a massive anti-clockwise movement (and vice verse).
What computation should I be doing to determine if the ship should move clockwise or anti-clockwise given I have its current angle and the destination angle?
Here is my entire code segment for that method:
MouseState mouseState = Mouse.GetState();
Vector2 mouseLoc = new Vector2(mouseState.X, mouseState.Y);
Vector2 direction = position - mouseLoc;
destAngle = (float)(Math.Atan2(-direction.Y, -direction.X));
if (mouseState.LeftButton == ButtonState.Pressed)
{
Console.WriteLine(rotation + " + " + destAngle);
if (destAngle - rotation <= 0)
{
rotation -= movingRotationSpeed;
}
else
{
rotation += movingRotationSpeed;
}
if (position.X == mouseState.X && position.Y == mouseState.Y)
{ }
else
{
//The ship always moves "forward".
position.X += speed * (float)(Math.Cos(rotation));
position.Y += speed * (float)(Math.Sin(rotation));
}
}