Flanking and Coordinated Movement - Best Practices?

Hi!

In our game, during combat, our agents are divided into 3 squads. While one squad provides cover, 2 others advance towards the enemy. I’d like to arrive at a solution, where the 2 moving squads take different paths, essentially, while 1 approaches the enemy dead on (fastest path) and attacks, I’d like to have the second squad flank around the enemy’s position and arrive at his other side.

Currently this is somewhat working, using some custom way point around the maps to find an alternative path.

However, at some cases, the flanking squad’s path takes them directly through the enemy’s position. In other cases, the flanking squad will crowd out the other 2 squads, creating bottlenecks.

We are using a recast graph with a custom movement script based on RichAI.

My main question is what would be best practice to have an agent’s path completely avoid a part of the map, (where the enemy is, or other squad members)? is there a way to add this to the seeker’s constraints, without updating the graph? obviously as the enemy is moving, and during combat this can be quite fast, I cannot afford to update the graph itself.

Additionally, is there a good way to find a “longer” or alternate way to a target, perhaps using MultiTargetPath? I guess I’m not clear on how this works, but I do have some way points I can use to feed into it, so if I can tell an agent “find the way to here, but go through these points first” that will be a great solution.

Hi

To find alternative paths you really have to use some kind of penalty.
In order to do that while not updating the graph the best way would be to create a new path type and override the GetTraversalCost method (you will have to mark it as virtual though). This method could for example check the distance to some enemies. It is called pretty often so it will have to be relatively fast and it may be called in a different thread, so it cannot use any part of the Unity API (such as transform.position).

Somewhat related I can also recommend the chapter “Company of Heroes Squad Formations Explained” in the book “AI Game Programming Wisdom 4” (which is also available online on various pages).

Some follow up questions.

Currently I am using Rich Paths.

After I get a path, what would be the process involved in perhaps making a modifier that will re-route certain parts of the path to avoid the player for instance?

After I get a path, how can I check each segment, so that I may determine which parts should be avoided? (I’m unclear how to translate the path/rich path I get into a bunch of straight lines I can check if they intersect a certain circle, which would be the radius of the player)

Is it possible to somehow mark the player as an obstacle (side note - it is already an RVO obstacle, not sure if that will help) without updating tags/penalty? Will using a nav mesh cut be too expensive? (i’m assuming so)

A RichPath is not represented by a series of straight lines, it is just a bunch of nodes, the straight lines will be calculated on the fly when necessary.
It is probably easier to add a funnel modifier to the AI object (the RichAI component will just ignore it) and then read the straight lines from the vectorPath field of the path object which will have been simplified by the funnel modifier.

You can use a navmesh cut on the player, usually it is not that expensive, especially if you set the TileHandlerHelper to not check for updates very often.

Note that updating tags/penalty is probably cheaper than using a navmesh cut (but it depends on the navmesh).

So I decided to go with creating a new path type since I figured it will be the most dynamic and effective way.

The idea was very simple.

Derive from ABPath store an Int3 with the point that I wish to avoid and the radius of avoidance when constructing the path.

Next, override GetTraversalCost(), and check if the node falls within the radius of avoidance, if so, add a high penalty.

So technically this worked out fine. The problem I have now is that no node ever passes the test. meaning the distance between a node (on the XZ plane) and the world position to avoid is always above the radius defined. I have a feeling that the nodes of the graph are just not close enough to make a difference.

Again, this is a recast graph, with 0.05 cell size, 0.1 cell height, 128 tile size, and a min area size of 1.

Can you shed some light as to what might be the issue?

Here is the code I’m using in GetTraversalCost():
IN_RADIUS_PENALTY is 100000

float dx = (PlayerPosition.x - node.position.x);
float dz = (PlayerPosition.z - node.position.z);
float distSqr = dx * dx + dz * dz;
if (distSqr <= m_radiusSqrd)
{
//within avoidance radius, add a big penalty
return GetTagPenalty((int)node.Tag) + node.Penalty + IN_RADIUS_PENALTY;
}
else
return base.GetTraversalCost(node);

Hi

Node positions are stored as Int3 coordinates, not as Vector3s. To get the correct results you will want to convert the node coordinates to Vector3 coordinates before you use them to calculate the distance to the player.

Vector3 nodePos = (Vector3)node.position;
float dx = PlayerPosition.x - nodePos.x;
...

I’m doing the opposite, converting the player’s position to an Int3, so upon construction I do:

public static AvoidPlayerRadiusPath Construct(Vector3 start, Vector3 end, Vector3 playerPosition , float playerAvoidRadius = 2f ,OnPathDelegate callback = null) { var p = PathPool.GetPath(); p.PlayerPosition = (Int3)playerPosition; p.PlayerAvoidRadius = playerAvoidRadius; p.Setup(start, end, callback); return p; }

Does the conversion not work that way?

Ah. Ok. Yeah that will also work. However Int3 coordinates are scaled up by a factor 1000 (Int3.Precision) since they only use whole numbers, not decimals, so you will have to multiply the range with 1000 in that case (or the squared range by 1000000).

Also. I would recommend using a long instead of an int to store the squared length to ensure the calculations do not overflow.

Ah! that must be the issue. will test and report back :slight_smile:

This appears to be working now! Very happy with the result. Thank you for your help.

1 Like