DEV Community

Cover image for A* Search Algorithm: Game Pathfinding Explained
Doogal Simpson
Doogal Simpson

Posted on • Originally published at doogal.dev

A* Search Algorithm: Game Pathfinding Explained

Quick Answer: Video game characters use the A* (A-Star) search algorithm to navigate complex environments without getting stuck on walls. A* achieves this by calculating the most efficient route using a simple formula: the actual movement cost from the starting point plus the estimated distance to the destination. This prevents unnatural wall-hugging and finds the true shortest path.

Imagine your team is building a top-down game. You have a character in Room A, and you want them to navigate into Room B. There is a wall separating the two rooms, and the doorway is slightly off-center.

If you just tell the character to move toward Room B, they are going to walk straight into the wall. This naive approach to grid movement results in awkward, robotic navigation. To prevent this, we rely on the A* search algorithm.

What is the A-Star search algorithm?

A* (A-Star) is a pathfinding and graph traversal algorithm used to efficiently plot a route between multiple nodes on a grid. It calculates the most efficient path by balancing the known movement cost to reach a specific point with an estimated distance to the final goal.

When calculating routes, A* looks at the space around the character as a grid of interconnected nodes. Before taking a step, the algorithm needs to decide which adjacent node is worth stepping into. It makes this decision using a specific heuristic.

How does the A* heuristic function calculate routes?

The A* heuristic evaluates potential paths by looking at two primary metrics for any given node. It checks if the node gets you physically closer to your destination, and it calculates the total travel cost of getting to that specific node from your starting location.

If you want to find the absolute shortest path into Room B, you have to optimize both of those values.

Let's say your algorithm only optimizes for the first metric: the distance to the destination. As the character checks the nodes around them, every chosen path will prioritize moving closer to Room B. The character makes a beeline straight into the wall because, technically, that wall is physically closest to the target coordinates.

Why do pathfinding algorithms make characters hit walls?

When a pathfinding algorithm only optimizes for the shortest estimated distance to the target, it completely ignores the geometry of obstacles. The path traces a direct line into a wall, spreads out along the perimeter until it finds a gap, and then continues toward the goal.

Once the character's path hits the wall, you still have prioritized nodes checking for a way forward. The algorithm acts like a wave, spreading out up and down the wall from the point of impact. Eventually, this wave finds the off-center doorway. Once the doorway is found, the character makes a straight line into Room B.

But that is not the fastest—or most natural—path. If a player clicks on Room B, they don't want their character to walk straight into a wall, slide sideways along the bricks until they hit a doorframe, and then walk inside.

How does A* prevent wall-hugging in game routing?

A* prevents characters from hugging walls by heavily weighting the travel cost from the starting location, forcing the algorithm to evaluate shorter overall routes. Once the doorway is found, it calculates that a direct diagonal from the start point to the door costs less than walking straight to the wall and sliding down it.

Because A* optimizes the cost from the source location, it doesn't just settle for the first path that makes it through the door. It starts backtracking and checking the paths that bridge the gap between the wall, your destination, and the doorway.

Eventually, it maps out a diagonal path. The character moves in a clean, direct diagonal line from their starting point in Room A, straight through the door, and into Room B.

Pathfinding Optimization Priorities

To see why evaluating both costs is necessary, here is a breakdown of how different routing priorities affect character movement:

  • Destination Distance Only: The character walks straight into obstacles, slides along edges to find openings, and takes a robotic, inefficient path.
  • Source Cost Only: The algorithm explores evenly in every single direction from the starting point, wasting massive amounts of compute time checking useless areas.
  • A* Search (Both): The algorithm directs the search toward the goal while constantly refining the route to ensure the total steps taken are the absolute minimum.

Frequently Asked Questions

Does A* search guarantee the shortest path?

Yes, A* is guaranteed to find the shortest possible path as long as the heuristic function is admissible. This simply means the algorithm must never overestimate the actual distance to the destination.

Why is A* preferred over Dijkstra's algorithm for games?

Dijkstra's algorithm is great at finding shortest paths, but it searches equally in all directions. A* is preferred in games because its heuristic actively pulls the search toward the destination, significantly reducing the number of nodes the CPU has to check.

Can A* be used outside of video game development?

Absolutely. While it is a staple in game development, A* is heavily used in mapping software to find driving directions, in robotics for physical navigation, and in network routing to send data packets efficiently.

Top comments (0)