# Pathfinding with different sized units [duplicate]

I am actually working on a general purpose pathfinding library (find it here), mostly oriented for grid-based maps. I was looking for ways to integrate support for pathfinding with units of different sizes. Actually, I am working with the assumption that all units are square (i.e. their widths and heights in tile units are the same).

In researching, I came across a very comprehensive article about Annotated A-star, using True clearance values for pathfinding. I tried it, and succeeded in implementing it, and got it working with a wide range of search algorithms (A-star, Theta*, Dijkstra, DFS, Breadth-first search and Jump Point Search).

There are some small details of implementation I think I am still missing. First of all, when an agent is 2x2 sized (i.e it occupies 4 tiles on the grid map), what tile can be assumed to be the agent position ? On the same page, clearance based pathfinding will fail in some cases. Let's consider the following map, where 0's are passable tiles, 1's are obstacles and x's matches walkable goal to be reached. Let's assume we are pathing with a 2x2 sized unit:

00000000
00111100
001xx100
001xx100
00000000


For targets on the left (tiles 4,3 and 4,4), clearance value is 2, for both. No problem here. But for the others (tiles 5,3 and 5,4), clearance value is 1. So depending on how the agent position is taken, pathfinding will fail while obviously, for each of these target, the agent can properly fit into the dead-end space.

Another scenario, with the same 2x2 sized agent:

0000000x
0000000x
0000000x
0000000x
xxxxxxxx


Well, all tiles are walkable here, no obstacles. We want the object to reach any goal on the rightmost or bottom-most border. Each of these targets have a clearance of 1, so annotated pathfinding wil obviously fail here...depending on how the agent's position is taken.

How do I work around this issue?

I'd just modify the algorithm a tiny bit: Instead of evaluating the tile at (x, y) you evaluate four tiles: (x, y), (x + 1, y), (x, y + 1), and (x + 1, y + 1) to calculate costs. If any is unpassable, you consider the tile unreachable. Other values might be combined on a min/max basis or just a median value (this really depends on how they impact movement).

However, there's one small additional modification: The goal is considered to be reached if any of the four tiles are the actual goal. It doesn't have to be the top left one.

Keep in mind that this might need further tweaking, depending on whether you add the remaining distance to your weighting (it might be skewed to the top left corner if you're not carefule).

As an alternative: Create another map using the points where four tiles meet as actual "tiles". Then just apply the standard algorithm.

• To expand on this answer - the first example the asker shows can be fullfilled easily with this type of path finding by taking away the bottom row of x x, this way the path finding will trigger that it has raeched the destination only if the 2x2 object has gone completely into the 2x2 shape fit. – Tom 'Blue' Piddock Apr 8 '13 at 10:29
• Hi Mario, much thans for the answer. I have already considered the alternative (different maps of different tile sizes), it doesn't sound efficient to me at all, as it requires extra memory. – Roland_Y Apr 8 '13 at 18:28
• On the other hand, what you stated first sounds interesting, and I was thinking of something similar. Yet I am not sure I get it right. Can you expand a little bit more ? And when saying "modify the algorithm", are you reffering to standard Astar or Annotated Astar ? Thanks. – Roland_Y Apr 8 '13 at 18:34
• This doesn't really matter which algorithm you pick and the modification is quite minor. You should have some cost determined by the tile/grid position. So, rather than adding tiles[x][y] to the costs per step, you add tiles[x][y] + tiles[x + 1][y] + tiles[x][y + 1] + tiles[x + 1][y + 1] for example. – Mario Apr 9 '13 at 0:00