XNA Creators Club Online
Page 1 of 1 (24 items)
Sort Posts: Previous Next

Ai \ Making enemies fly on a set path.

Last post 11-12-2008 10:26 PM by Revolution1200. 23 replies.
  • 09-05-2008 2:43 AM

    Ai \ Making enemies fly on a set path.

    I need some help. I'm trying to make a game like raiden (http://www.freeworldgroup.com/onlinegames/gameindex/raiden.htm) and I'm trying to code in the enemies. What i want is an algorithm that allows me to say "this enemy unit type moves along this path when spawned on a random interval at the top of the screen", but i have no clue how to even attempt this. I wouldn't mind figuring it out for myself, but i don't even know how to start. If anyone wants to help please do so, it would be greatly appreciated.
  • 09-05-2008 4:17 AM In reply to

    Re: Ai \ Making enemies fly on a set path.

    First, you need to define a path. Easiest is to hard-code it as a vector of points, and just offset it by wherever you start following the path.

    Second, you must make the enemy move on that path. Easiest is to keep an index into the array, and move towards the point at that index. When you are close enough, increment the index. When the index is the length of the array, you're done with the path.

    using System; 
     
    namespace flypath 
      /// <summary> 
      /// Create an instance of class Follow, passing in the initial position of the object  
      /// and its speed in pixels per second. Also pass in an array of Vector2 that describe  
      /// the relative movement of the object. Call Move() each frame, passing in the number  
      /// of seconds since the last frame (typically 0.166667f for 60 Hz) and get back the  
      /// new position of the object. If Move() returns false, it means that the path has  
      /// been completed. 
      /// </summary> 
      public class Follow 
      { 
        Vector2[ path_; 
        Vector2 offset_; 
        float speed_; 
        float t_; 
        float len_; 
        int segment_; 
     
        public static Vector2[ SomePath = new Vector2[ { 
          new Vector2(-100, -100), // start out going down/left 
          new Vector2(0, -200), // then go down-right until you're right below where you started 
          new Vector2(0, -300), // then go straight down 
          new Vector2(100, -100), // then go right/up 
          new Vector2(0, 0), // then return to "home" 
        }; 
     
        public Follow(Vector2[ path, Vector2 spawnPoint, float speed) 
        { 
          path_ = path; 
          offset_ = spawnPoint; 
          speed_ = speed; 
          t_ = 0; 
          //  Make sure the internal state machine knows we're at the first segment start point 
          EnterSegment(0); 
        } 
     
        void EnterSegment(int seg) 
        { 
          //  the state machine has reached a new segment in the path to follow 
          segment_ = seg; 
          if (segment_ < path_.Length - 1) 
          {  // calculate length to go 
            //  calculate how long the path is, at our configured velocity 
            len_ = (path_[segment_ + 1] - path_[segment_]).Length(); 
            t_ = 0; // I have not started moving here yet, so I'm at time 0 of this segment 
          } 
        } 
     
        /// <summary> 
        /// Move the object along the path, given some amount of time has elapsed. 
        /// </summary> 
        /// <param name="dt">The number of seconds that has elapsed since the last call.</param> 
        /// <param name="pos">Returns the new position of the object.</param> 
        /// <returns>false if the path has finished</returns> 
        public bool Move(float dt, out Vector2 pos) 
        { 
          //  add to the current time along the path 
          t_ += dt; 
          //  if I reached the end of this segment, move to a new segment 
          if (t_ >= len_) 
          { 
            EnterSegment(segment_ + 1); 
          } 
          //  calculate my position along the current segment (or, if complete, just return the start point) 
          pos = (segment_ >= path_.Length - 1) ? offset_ : offset_ + path_[segment_] + (path_[segment_ + 1] - path_[segment_]) * (t_ / len_); 
          //  return true as long as I'm still following the path 
          return segment_ < path_.Length; 
        } 
      } 
     
      public class Program 
      { 
        static void Main(string[ args) 
        { 
          Vector2 initPos = new Vector2(300, 600); 
          Follow f = new Follow(Follow.SomePath, initPos, 20); 
          Console.WriteLine("First pos: {0}", initPos); 
          while (f.Move(1, out initPos)) 
          { 
            Console.WriteLine("New pos: {0}", initPos); 
          } 
          Console.WriteLine("End pos: {0}", initPos); 
          return
        } 
      } 
     
      //  In this test program, I define my own Vector2, so that I don't have to  
      //  link with the entire XNA framework. 
      public struct Vector2 { 
        public Vector2(float x, float y) { X = x; Y = y; } 
        public float X; 
        public float Y; 
        public float Length() { return (float)Math.Sqrt(X*X + Y*Y); } 
        public override string ToString() 
        { 
          return String.Format("{0:0.0},{1:0.0}", X, Y); 
        } 
        public static Vector2 operator +(Vector2 a, Vector2 o) { return new Vector2(a.X + o.X, a.Y + o.Y); } 
        public static Vector2 operator -(Vector2 a, Vector2 o) { return new Vector2(a.X - o.X, a.Y - o.Y); } 
        public static Vector2 operator *(Vector2 a, float s) { return new Vector2(a.X * s, a.Y * s); } 
      } 
     
     

    Usage:
    Create a Follow for each sprite, passing in the spawn position and speed in pixels per second.
    Each frame, call Move(), passing in the amount of time that has passed (ElapsedGameTime).
    Out will come the new position. The function will return false if the path is complete.

    Jon Watte, Direct3D MVP kW X-port 3ds Max .X exporter kW Animation source code
  • 09-05-2008 7:41 AM In reply to

    Re: Ai \ Making enemies fly on a set path.

    Awesome, but one question. How do i put that into my code? Like, where would i put everything...
  • 09-06-2008 7:10 AM In reply to

    Re: Ai \ Making enemies fly on a set path.

    Dude, there is like, 22 errors with that!
  • 09-06-2008 7:57 AM In reply to

    Re: Ai \ Making enemies fly on a set path.

    Close the brackets for the array definitions. Every [ needs a ] to go with it but for some reason the forums don't like seeing them side by side. Usually if someone posts code and you see a [ all by itself there's a good chance a ] should be nearby.
  • 09-06-2008 5:32 PM In reply to

    Re: Ai \ Making enemies fly on a set path.

    Well, the forum code editor sucks like (some expression that would get me banned).

    Add the brackets correctly, and it will be better. However, the code is a suggestion of what it would look like that I typed straight into the forum, so it will probably have some minor errors. The idea with pseudocode or illustrations is that someone who understands a programming language can easily implement it as real code. If you're having trouble with C# as a language, then you're trying to fight two battles at the same time: 1) understanding the language and 2) understanding how to build your game. Often, it's better to tackle one thing at a time.


    Jon Watte, Direct3D MVP kW X-port 3ds Max .X exporter kW Animation source code
  • 09-07-2008 12:10 AM In reply to

    Re: Ai \ Making enemies fly on a set path.

    Yeah i get that a lot. But I'm not trying to build a super game, I'm just trying to make a simple one... the a.i will be the hardest part which is why i needs a little help. O'k I'll try to implement the code by myself. Any tips?
  • 09-07-2008 5:44 PM In reply to

    Re: Ai \ Making enemies fly on a set path.

    Tips: Read the code, and try to understand what it's trying to do. There are some comments, at least :-)

    Also, the code will probably work with minor adjustments, so it should be a good starting point.


    Jon Watte, Direct3D MVP kW X-port 3ds Max .X exporter kW Animation source code
  • 09-08-2008 12:22 AM In reply to

    Re: Ai \ Making enemies fly on a set path.

    Yeah, I've begun implementing it, had to change some small things and I've been looking up whatever i don't know. All in all i think i might actually be able to do it :D I'll post here if i run into any problems.
  • 09-08-2008 8:27 AM In reply to

    Re: Ai \ Making enemies fly on a set path.

    I've got an error. On the line Return Segment_ Expected. Invalid Token ; Maybe the position of the line is wrong. ok i wrote 3 other errors in but the stupid post wont display them WTF!!! Anyways what i done to fix it was move it into the public bool Im trying to figure out how to tell my Enemies to follow the path i set... any help?
  • 09-08-2008 8:01 PM In reply to

    Re: Ai \ Making enemies fly on a set path.