DE EN FR ES IT

Game AI & real-time strategy

Pathfinding (A*)

Also: A* search, path planning, route finding

Pathfinding is how a game works out a walkable and reasonably short route for a character between two points. A* is the best-known algorithm for the job: a graph search that combines the distance travelled so far with an estimate of the distance still to go.

Dijkstra and A* comparedTwo identical grids with a wall. Dijkstra expands 89 cells, A* with a distance estimate only 27; both find a shortest path of 14 steps.Dijkstra89 cells expanded, path 14 stepsA*27 cells expanded, path 14 steps
Both grids were actually searched when this page was generated (4-neighbourhood, Manhattan distance as the estimate). Light cells: expanded.

Background

Any pathfinder first needs the world expressed as a graph. Common choices are grids (each tile is a node), waypoint networks, polygon navigation meshes and visibility graphs, where the corners of obstacles become the nodes. The choice of representation often matters more for quality and speed than the search algorithm itself.

A* was described in 1968 by Peter Hart, Nils Nilsson and Bertram Raphael. It extends Dijkstra’s algorithm with a heuristic, typically the straight-line distance to the goal. As long as that estimate never overshoots the real remaining distance, A* still finds the shortest path while expanding far fewer nodes. With the heuristic set to zero, A* is simply Dijkstra.

Classic pitfalls: grid paths that zigzag and need smoothing afterwards, characters snagging on corners because their width was never accounted for, and clicks outside the walkable area with no sensible fallback point. Real-time strategy adds another layer: hundreds of units searching at once, blocking each other, while the frame rate has to hold.

How we do it

In the Wurzelwerk engine, pathfinding runs on each scene’s walkable area. nav.ts builds a visibility graph over the corners of the walk polygon and its holes and searches it with Dijkstra. If the target is in direct line of sight, the character just walks straight there. Clicks outside the area snap to the nearest point on its edge, and corner-to-corner visibility is cached.

We deliberately skip A* here: an adventure room has only a few dozen corners, so a heuristic barely makes a measurable difference. Character width is already baked into the area, because the walkable areas for Tatort RMH come out of the Blender pipeline as floor minus obstacles, padded for the width of a character. What pathfinding feels like with many units at once is something our founder knows from his work as an AI developer on real-time strategy in 2000.

Related terms