Search code examples
path-finding

Pathfinding algorithm with "directionally-dependent" obstacles?


I've run across the following problem. I was programming a game and it will use a pathfinding algorithm to determine how to make tunnels for a room-and-tunnel maze. But I need an algorithm that will find paths through obstacle courses in which the direction of approach is relevant. I.e. a path passing horizontally through an obstacle may be OK, while one going vertically may not.

A diagrammed example:

. = free space
X = path
| = vertical-blocking obstacle
a = start point
b = end point

If we have

.....
a.|.b
.....

then we should get a path like

.....
XXXXX
.....

But if we have

..a..
..|..
..b..

then we should get a path like

..XX.
..|X.
..XX.

What kind of algorithm would do this? Can "A*" be modified to do that?


Solution

  • A* will solve the problem without modification.

    You're considering path-finding on a grid, but (at least in discrete work spaces) that problem is equivalent to planning on a graph. Consequently, if you can convert your grid to an appropriate graph, any complete graph search will successfully solve this problem, and any optimal graph search will give the plans you requested (or paths of equivalent cost).

    The challenge then becomes formulating the graph from the grid. A undirected graph of the grid you've supplied above will look something like:

    Graph image