9
\$\begingroup\$

I'm currently working on a 2D Age of Empires style RTS game.

Here's my problem:

I'm currently using the A* algorithm (via Path finding package) to move my units.

When you select a "group" of units and set a destination, all units inside of the battalion will follow an A* path and go to the destination. Here's a video of what it looks like.

I want that the group of units stays solid; I want that the whole group stays united and follows the path without the individual units separating.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ What have you tried so far? You said what you want to achieve, but not what's the problem with your current implementation. \$\endgroup\$
    – liggiorgio
    Jun 23 at 7:53
  • \$\begingroup\$ Ive tried to make each unit move with a A* , but using this method the battalion is "broken" like in the video . \$\endgroup\$ Jun 23 at 8:21
  • 2
    \$\begingroup\$ What have you tried so far to fix the unwanted behaviour? \$\endgroup\$
    – liggiorgio
    Jun 23 at 8:48
  • 1
    \$\begingroup\$ Have you consulted past Q&A here about implementing unit formations in RTS games? \$\endgroup\$
    – DMGregory
    Jun 23 at 12:10

3 Answers 3

14
\$\begingroup\$

My approach would be:

  1. Designate one soldier of the formation "the leader". (this does not necessarily need to be the actual commanding officer of the formation. You can pick any soldier. You will probably get the best results when you pick the soldier closest to the center of the formation, but that might be worth experimenting with)
  2. When the formation moves, calculate a path only for "the leader"
  3. While "the leader" moves along the calculated path, have all other soldiers in the formation maintain their relative position to "the leader". This should not require additional route finding, only blindly moving into the relative direction.
  4. Should that be impossible, for example because an obstacle in the way requires to break formation, then the individual soldier should calculate an individual path to its designated position within the formation. In order to regain unit cohesion before arriving at the destination, those separated soldiers should move faster than usual. Or alternatively the leader should notice when they lose a soldier and then slow down until the lost soldier has caught up.

When your game features combat with individual soldiers being able to die at any time, then you also need to handle the case that "the leader" dies, potentially even while the formation is on the move. In that case you would have to select a new solder as the new leader and reevaluate the relative positions in the formation of all remaining soldiers.

\$\endgroup\$
1
  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$
    – Evorlor
    Jun 24 at 17:38
2
\$\begingroup\$

Maybe similar to Philipp's point but you could also make a separate object (maybe call it like navigator) and apply your pathfinding algorithm to that. Then you would make it so whenever your navigator moves, your soldiers each move by that same amount. Just instead of having to do a leader etc.

\$\endgroup\$
0
\$\begingroup\$

In the situation with no obstacles you could make the entire formation a thing you move, so a 3x4 formation can all move up, down, left, or right if the entire 3x4 formation is able to get to the new destination, and then use A* on the graph modified to adjust what is legal for the formation.

If you adopted this approach you would need to decide upon your business rules for how to handle the situation where the group can't get through something in formation. The other issue you would have to decide is if you allow the formation to break while going through something what is the expected behavior after they squeeze through whatever - do you expect them to form up again, meaning some of the units sit there and do nothing until the trailing members get through?

New contributor
zelinka is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .