A* Pathfinding Project4.2.0

Multiple agent types

This page explains how to handle different agent types, such as agents of different sizes.

When you have agents of different sizes they usually cannot take the same routes to the target. Fortunately it is easy to solve this problem. The easiest way is to create multiple graphs, one for each type of agent. If you have a lot of different agent types, or perhaps even a continous spectrum, you may want to group them as having a large number of graphs will increase memory usage and scanning will take longer. On the Seeker component it is possible to set which graphs that the Seeker can use.

 

For grid graphs there are also two additional methods that can be used, they are explained in the Other approaches for grid graphs section.

Example

Lets say we have these two agents:

 

In the AstarPath inspector we can then create 2 different graphs, with the only difference being the character radius that is used (any other parameters can of course be changed if necessary). In this example a recast graph has been used, but this can just as easily be done with another graph type.

 

 

 

 

 

 

When scanning these graphs we will get a result that looks something like this. With the graph for the smaller agent in blue and the graph for the larger agent in purple.

 

On the Seeker component we can then set which graph each agent should use:

 

Now both agents will use graphs appropriate for their size:

Other approaches for grid graphs

As an alternative method on grid graphs it is possible to make the graph generate different tags depending on the distance to the nearest obstacle. This only requires a single graph.

There is also another approach that is very flexible, but unfortunately has a higher computational cost. For a path request one can supply an ITraversalProvider object (see also this tutorial: ITraversalProvider) which allows your code to determine exactly which nodes should not be traversable. Using this we can add custom code to instead of just checking if the current node is traversable, we can check all nodes around it as well in for example a 3x3 or 5x5 square (with the default being equivalent to a 1x1 square).

In the below image you can see it used with a 3x3 square. Note that it does not want to traverse the nodes right next to obstacles even though they are walkable, because it checks in a 3x3 square around it.

 

The major advantage of the ITraversalProvider approach is that it will handle all things that can make a node not be traversable. For example you might use different tags to restrict agent movement, however even if you use multiple graphs the different agents will still be able to walk right up to a region that is not traversable becasue of its tag even if the agent has a very large size. With this approach it will not be able to move as close to it because the nodes with another tag will be detected as not traversable and the NxN nodes around the agent must all be traversable.

The ITraversalProvider can be implemented like this:class GridShapeTraversalProvider : ITraversalProvider {
Int2[] shape;

public static GridShapeTraversalProvider SquareShape (int width) {
if ((width % 2) != 1) throw new System.ArgumentException("only odd widths are supported");
var shape = new GridShapeTraversalProvider();
shape.shape = new Int2[width*width];

// Create an array containing all integer points within a width*width square
int i = 0;
for (int x = -width/2; x <= width/2; x++) {
for (int z = -width/2; z <= width/2; z++) {
shape.shape[i] = new Int2(x, z);
i++;
}
}
return shape;
}

public bool CanTraverse (Path path, GraphNode node) {
GridNodeBase gridNode = node as GridNodeBase;

// Don't do anything special for non-grid nodes
if (gridNode == null) return DefaultITraversalProvider.CanTraverse(path, node);
int x0 = gridNode.XCoordinateInGrid;
int z0 = gridNode.ZCoordinateInGrid;
var grid = gridNode.Graph as GridGraph;

// Iterate through all the nodes in the shape around the current node
// and check if those nodes are also traversable.
for (int i

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值