Skip to content

michael@malura:~$ developer // data hoarder // nerd // homelab enjoyer

Pathfinding on a Grid

The enemy stands on the left, the player on the right, and between them is a wall that opens towards the enemy like a bucket. Walking straight ahead no longer works. Depending on how you search for the path, the enemy runs into the bucket and gets stuck, makes you compute half the map, or takes a detour it never needed.

The map has exactly that bucket and a patch of mud on the straight line. Mud costs 5 per cell, grass 1. It starts with A*. Chips 1 to 4 switch the method and the search then plays out as an animation. You paint walls and mud with the mouse, a right click erases, and S and G can be dragged. Orange cells are known but not yet processed (the frontier), teal cells are done, and the dark line is the path found. Under the map you see how many cells the search touched and what the path costs, compared with the cheapest one.

One loop, four sort orders

All four methods do the same thing. Take the best cell from the frontier, mark it as done, add its neighbors. When the goal comes up, you are finished. The difference is what "best" means. In my code that is a single number:

case Algo::Bfs: key = 0.0; tie = 0.0; break;
case Algo::Dijkstra: key = g; break;
case Algo::Greedy: key = h; tie = 0.0; break;
case Algo::AStar: key = g + options_.weight * h; break;

g is the cost from the start to the cell, h is the estimate to the goal. BFS has no number at all: whoever was discovered first comes first, and every step counts the same, mud or no mud. Red Blob Games explains all of this at length with great animations, in the introduction to A* and in the implementation notes. The idea comes from there, the code is mine.

This is what comes out on the start map with four directions. BFS processes 748 cells and finds a path that costs 79. Dijkstra needs 745 cells, almost the whole map, and gets 51. Greedy gets by with 159 cells and also ends up at 79. A* needs 288 cells and finds the 51. The numbers are live under the map, and of course they move when you add walls and mud.

Who is wrong, and why

BFS counts steps, and to it mud is a cell like any other. So it walks the shortest way straight through and pays 79 instead of 51. Dijkstra knows the costs and gets it right, but it looks equally far in every direction until it reaches the goal. You can see that nicely: the area grows evenly until it arrives on the right.

Greedy only looks at the straight-line distance. Inside the bucket every cell is closer to the goal than the way around, so it fills the bucket up to the wall and only comes out at the rim. It is fast, but the path ends up 55 percent more expensive than necessary. Mud it never sees anyway.

A* fills the bucket too. The estimate knows nothing about walls, and from its point of view the bottom of the bucket is close to the goal. But it leaves large parts of the map alone, because g counts as well. Compared with Dijkstra that saves more than half of the cells and changes nothing about the result.

The estimate

For four directions h is the Manhattan distance. With diagonals I use the octile distance: as many diagonal steps as possible, the rest straight.

return (dx + dy) + (kSqrt2 - 2.0) * std::min(dx, dy);

Neither ever overestimates, because the cheapest step costs 1 (grass), and the real path can never be shorter than the estimate. That is the condition for A* to find the cheapest path.

The weight

The slider scales h. At 0 only g is left, which is Dijkstra, 745 cells. At 1 it is plain A*, 288 cells. At 2 it is 201 cells, and on this map the answer is still 51, but that is luck here and no promise. At 5 it is 195 cells and the path costs 79. At 10 it is 161, practically greedy. Above 1 the estimate overestimates and the path can get more expensive. My search processes every cell at most once and does not reopen finished ones. Up to weight 1 that is enough, above it the correction is missing.

Diagonals

The key D (or the chip) adds eight directions. A diagonal step costs the square root of 2 times the cell it enters. There is also a rule that is easy to forget: a diagonal step must not cut a wall corner.

if (dx != 0 && dy != 0) return grid.passable(from.x + dx, from.y) && grid.passable(from.x, from.y + dy);

Without it the path squeezes between two walls that touch only at a corner. The test has a 2x2 map with a wall in the top right: from top left to bottom right there is no diagonal step, only down and then right. On the start map the path with diagonals costs 44, Dijkstra needs 726 cells and A* 250. Greedy is fast at 133 cells, but its path costs 80.3 instead of 44.

Pitfalls

When a cell is reached again by a cheaper way, it is already in the queue. The old entry stays there and gets skipped when it is taken out:

if (state_[e.index] != CellState::Open || e.g != g_[e.index]) continue;

On a tie I take the cell with the smaller h. That makes A* walk straight to the goal on an empty field, where all cells have the same value. The test calculates this by hand: on an empty 5x5 field from corner to corner A* takes exactly 9 cells and Dijkstra all 25.

If the goal sits behind a closed wall, the search floods its half of the map and reports "no path". The number shown is then the count of reachable cells. A start or goal inside a wall gives "no path" right away.

And painting: the mouse jumps over several cells between two events, so the demo draws a Bresenham line from the last point to the new one. pickCell turns pixels into cells with floor before it converts to int, otherwise a click just left of the map would land in cell 0.

The code is in the repo under astar-vergleich. The project Game Dev Snippets collects all demos of the series.

25.11.2026 updated 06.11.2026
Share X Reddit Hacker News LinkedIn E-Mail
Subscribe to new posts RSS JSON