-1

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));
    }
user2716722
  • 93
  • 11
  • Use Math.Atan2, it will return the right angle – Blau Aug 26 '13 at 07:21
  • This didn't work. The output was the same as before using Math.Atan. – user2716722 Aug 26 '13 at 12:43
  • What's the output you are getting and what's the output you expect? – davidsbro Aug 26 '13 at 13:48
  • Maybe your problem is here: `float remainingX = Math.Abs(Destination.X - Position.Y);` Shouldn't it be `Position.X`? – pinckerman Aug 26 '13 at 14:10
  • The mob is supposed to move from the first red dot on the left moving to the other red dots preceding left to right (from coordinates I preset) but instead the mob appears to try to circle around the point. I have a poor quality video (about 10 secs) if you would care to watch it at http://youtu.be/RN6HgkCDMMo – user2716722 Aug 26 '13 at 14:24

1 Answers1

0

This gives the right angle if position and destination are right:

angle = Math.Atan2(Destination.Y-Position.Y, Destination.X-Position.X) 
Blau
  • 5,742
  • 1
  • 18
  • 27