A* Pathfinding Visualizer on Navigation Meshes

simulator intermediate ~9 min
Loading simulation...
Path: 28 cells — explored 142 nodes (20% efficiency)

On a 20×20 grid with 25% obstacles, A* (heuristic=1) finds a 28-cell path after exploring 142 nodes. Increasing the heuristic weight reduces exploration but may yield suboptimal paths.

Formula

f(n) = g(n) + w × h(n), where w = heuristic_weight
h(n) = octile distance = max(|dx|, |dy|) + (√2 - 1) × min(|dx|, |dy|)
Efficiency = path_length / nodes_explored × 100%

Finding the Way

Every NPC in every game needs to navigate from point A to point B while avoiding obstacles. The A* algorithm, published by Peter Hart, Nils Nilsson, and Bertram Raphael in 1968, remains the gold standard for pathfinding over 50 years later. It combines the guaranteed optimality of Dijkstra's algorithm with a heuristic that dramatically reduces the search space — making it fast enough for real-time games with hundreds of AI agents.

The A* Algorithm

A* works by maintaining two lists: open (nodes to explore) and closed (nodes already explored). Each node stores g (actual cost from start), h (estimated cost to goal), and f = g + h. The algorithm always expands the node with the lowest f score. When the heuristic h is admissible (never overestimates), A* is guaranteed to find the shortest path. The heuristic weight slider in this simulator lets you trade optimality for speed.

From Grids to Navigation Meshes

Grid-based pathfinding works well for 2D games but scales poorly to 3D environments. Navigation meshes (navmeshes) solve this by decomposing walkable surfaces into convex polygons. Pathfinding operates on the polygon graph rather than individual cells, reducing the search space by orders of magnitude. Unity and Unreal Engine both include built-in navmesh generation and A* pathfinding, making it accessible to all game developers.

Heuristic Weight: The Speed-Quality Tradeoff

The heuristic weight parameter controls the balance between exploration thoroughness and speed. At weight 0, A* becomes Dijkstra's algorithm — optimal but slow, exploring outward like a flood fill. At weight 1, standard A* finds optimal paths efficiently. Above 1, weighted A* becomes greedy — finding paths very quickly but potentially suboptimal. Games with many simultaneous agents often use weight > 1 because 'good enough' paths at 60fps matter more than perfect paths at 10fps.

FAQ

How does A* pathfinding work?

A* maintains a priority queue of nodes to explore, ranked by f(n) = g(n) + h(n), where g(n) is the actual cost from start to n, and h(n) is a heuristic estimate of cost from n to the goal. It always expands the most promising node first, guaranteeing the shortest path when the heuristic never overestimates (is admissible).

What is a navigation mesh (navmesh)?

A navigation mesh is a collection of convex polygons that define the walkable surface of a game level. Unlike grid-based pathfinding, navmeshes represent complex 3D geometry efficiently. Characters pathfind between polygon centers, then smooth the path for natural-looking movement. Most modern 3D games (Unity, Unreal) use navmeshes.

What is the difference between A* and Dijkstra's algorithm?

Dijkstra's algorithm is A* with h(n) = 0 — it explores in all directions equally, guaranteeing the shortest path but visiting many unnecessary nodes. A* adds a heuristic that biases exploration toward the goal, dramatically reducing the number of nodes explored while still finding optimal paths.

Why do NPCs sometimes take weird paths in games?

Common causes include: navmesh gaps (the mesh doesn't cover all walkable areas), local avoidance conflicts (multiple NPCs trying to avoid each other create oscillation), path smoothing artifacts, and greedy heuristics that sacrifice optimality for speed. Most AAA games use hierarchical pathfinding with multiple refinement passes.

Sources

Embed

<iframe src="https://homo-deus.com/lab/game-design/pathfinding-navmesh/embed" width="100%" height="400" frameborder="0"></iframe>
View source on GitHub