Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to get something like the below image. As you can see, there's a highway and inside of it, there'll be some objects moving (millisecond). I guess the street behavior is like a pipeline. When the highway loads an object, it appears at the beginning and it'll be moving through the highway until it arrives in the other extreme of the highway.

My main problem is, how can I move several objects only inside of the highway?

screen capture from bloons td4 level 1

share|improve this question

3 Answers 3

That depends: how do you want them to move?

The unnatural option

Define a preset path with a series of vertices and have the balloons follow the path strictly.

A more natural option

Make the balloons boids and implement crowd path following behaviour, which will have them follow the path (and not get too far from it) whilst avoiding collision with each other. Both those pages contain Java applet examples.

The author of those pages notes he cannot distribute the source code of the examples, however OpenSteer offers C++ implementations of those algorithms.

(I'm not aware of any C# boids libraries, or any decent tutorials - the best I've done is just follow code examples)

share|improve this answer
    
+1 for a good answer. I think that a simple path-following is the only thing that's needed to simulate the depicted behavior. Steering behaviors will come in handy, when the path suddenly gets wider or obstacles appear where the "boids" will queue up. –  bummzack Aug 21 '11 at 9:56
1  
+1 for the boids –  pek Aug 21 '11 at 13:32
    
I think the first option it would be easier. Is it possible you can show me an example? –  oscar.fimbres Aug 21 '11 at 15:39
    
@oscar.fimbres: That should be quite simple. Just create a list of waypoint coordinates (eg. an array), take the first coordinate as target and move your agent into that direction. Once the agent reaches the waypoint, take the next one as target. If the agent moves with constant velocity, you can also easily map any overshoot to the next target. –  bummzack Aug 21 '11 at 19:53
    
Yeah, I can imagine.. I'm checking the site: enchantedage.com/node/78 Although, I can get move my sprite in that path –  oscar.fimbres Aug 21 '11 at 21:20

You need a list of points and a list of balloons

class Path 
{
   List<Vector2> Points;
   float[] Lengths;
   Vector2[] Directions;

   void Build()
   {
       Lengths = new float[Points.Count-1];
       Directions = new float[Points.Count-1];
       for (int i=0; i<Points.Count-1;i++)
       {
            Directions[i] = Points[i+1] - Points[i];
            Lengths[i] = Directions[i].Length();
            Directions[i].Normalize();
       }  
   }
}

class Balloon
{
     Vector2 Position;
     float StagePos;
     int StageIndex;
     Path Path;
     float Speed;

     void Update(float Seconds)
     {
         if (StageIndex!=Path.Points.Count-1)
         {
             StagePos += Speed * Seconds;
             while (StagePos>Path.Lengths[StageIndex])
             {
                 StagePos -= Path.Lengths[StageIndex]; 
                 StageIndex++;              
                 if (StageIndex == Path.Points.Count-1) 
                 {
                     Position = Path.Points[StageIndex];
                     return;
                 }
             }
             Position = Path.Points[StageIndex] + Directions[StageIndex] * StagePos;
         }
     }    
}
share|improve this answer
2  
Sprites are images. That's a path following object of some sort. The name's up to you, but don't call it a sprite –  Jonathan Hobbs Aug 23 '11 at 11:53
    
@Jonathan: Does it really matter what he calls it when he provides a concrete example of how to solve the problem? –  Patrik Aug 23 '11 at 17:48
    
@Patrik It matters when you pick a name that suggests a certain type of behaviour. e.g. Sprite, Singleton, Joint, Spring - you've seen these classes before and you expect other classes of this name to do something similar. It doesn't matter right here because we can immediately tell the name Sprite has nothing to do with its behaviour, but it will matter when someone takes this code and adds a bit more to their sprite class. This isn't hyperbole: we had a question recently about code with a sprite class that wasn't a sprite class, and most responders were confused for a while. –  Jonathan Hobbs Aug 23 '11 at 23:41
    
@Jonathan There's no accounting for taste. This code shows how to move sprites folloqing a path, so i think is not too bad for an example. :) –  Blau Aug 23 '11 at 23:42
1  
@Blau This isn't a matter of taste. –  Jonathan Hobbs Aug 23 '11 at 23:46

If you have a road made up of tiles with a single path ("pipeline" as you called it), then you don't need boids, avoidance, AI, or really anything super special. Just have each balloon move in the natural direction of the road tile. For example, given a starting tile with only one non-sand tile to touch, you can figure out which direction to move -- it is the direction that is "not here, not where I was, and not sand". Since your road has only two possible directions of flow, once the initial direction and tile are established, the algorithm will solve the problem.

The balloons will appear to follow each other simply because there isn't anywhere else to go. If they all move at a constant speed, then no collisions are possible. Even if they don't move at a constant speed, the "not here, not where I was, and not sand" can be have "and make sure there is enough space for me" appended.

You could generalize a little and use the tile's image as a method of extracting direction. For example, an L shaped road stripe means either "if you're going south, then turn east" or "if you're going west, turn north".

This system breaks down when you decision making to do, i.e. more than a single path that can be taken, but from your screenshot and description of the problem, that doesn't seem to be an issue. If it is a requirement for your application, then definitely invest in a more AI-centric approach.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.