I have been trying to make a tower defense game game where the mob is given a destination to move towards at a given velocity, and the way it is set up now something quirky is happening with the updateAngle() method that I am not sure about.
// Destination is represented by Coordinates[index]
public void Update(GameTime time)
{
Position.Y -= (float)(time.ElapsedGameTime.TotalSeconds * velocity * Math.Cos(angle));
Position.X += (float)(time.ElapsedGameTime.TotalSeconds * velocity * Math.Sin(angle));
float remainingX = Math.Abs(Destination.X - Position.Y);
float remainingY = Math.Abs(Destination.Y - Position.Y);
if (remainingX < 2 && remainingY < 2)
{
index++;
if (index == Coordinates.Count - 1)
{
End = true;
}
else
{
updateAngle();
}
}
}
private void updateAngle()
{
angle = (float)Math.Atan((Position.Y - Destination.Y) / (Position.X - Destination.X));
}