# Algorithm to make groups of units

In Age of Mythology and some other strategy games, when you select multiple units and order them to move to some place, they make a "group" when they reach the desired location:

I have a Vector with several sprites, which are the selected units, the variables tarX and tarY are the target x and y. I just want an example, so you can just set the x and y position and I can adapt it to my code. Also, I would like to ask that the algorithm calls "isWalkable" for the x and y position, to determine if it's a valid position for each unit.

What you're looking for is basically coordinated movement. The units pathfind to their destination and when they reach it, they form a "formation".

This is a topic decently covered in the AI Game Programming Wisdom (Book 1, Book 2, Book 3 and Book 4) books. But, I'll refer you to the following articles:

Coordinated Movement - A link explaining the fundamental principles of coordinated unit movement in RTS games

and

Implementing Coordinated Movement - An article continuing from the first, that talks about how to implement this type of movement in a game.

It's written in C++ but it shouldn't be too difficult to port to any other language, as the underlying principles remain the same.

Hope that helps.

Ray

• Okay, I'm gonna read it. I am using A* path finding. – user4248 Dec 27 '10 at 21:00