I have a spaceship that has a location, destination and a rotation. When it has a new destination it moves forward all the while rotating clockwise until it is facing its destination.
Code:
public void Move()
{
Vector requiredDirection = destination - origin;
requiredDirection.Normalize();
Vector directionNow = new Vector((float)Math.Cos(rotation), (float)Math.Sin(rotation));
float x = Math.Abs(requiredDirection.X - directionNow.X);
float y = Math.Abs(requiredDirection.Y - directionNow.Y);
if ((x > rotationSpeed) || (y > rotationSpeed))
{
rotation += rotationSpeed;
}
shipPosition += directionNow * speed;
}
My problem is that the ship will only rotate in the one direction until it is facing its target, I need it to rotate in the direction that would be the shortest route.
I’m really at a loss as to where to begin, this is my first real attempt at Vectors.
The angle from
directionNowtorequiredDirectionis given byMath.Atan2(requiredDirection.Y,requiredDirection.X) - Math.Atan2(directionNow.Y,directionNow.X). That will be positive to rotate counterclockwise, negative to rotate clockwise.