A* search algorithm

This is probably the most common path finding algorithm used in games, as it is relatively fast. It makes use of a heuristic, which cuts down on computation time through estimation. The algorithm will look to each possible move and measure how far it is from the current position and how far it is from the target position. At each point it will make the optimal choice to move closer to the target. You can expand on the algorithm by adding additional metadata which impacts the cost of different moves.

Visual Representation of A*

A* algorithm in action

Pseudo code

create priority queue of open nodes, initially contains starting node
create a set of closed nodes, initially empty
while (open nodes is not empty) {
    current node = best node from open nodes
    add current node to closed set
    if (current node == target node) break

    for (neighbor in current node) {
        if (neighbor node is in closed set 
                and current cost to neighbor < cached cost to neighbor) {
            remove neighbor from closed
        }
        if (neighbor node is in open queue 
                and current cost to neighbor < cached cost to neighbor) {
            remove neighbor from open
        }
        if (neighbor is not in open or closed) {
            neighbor.parent = current
            neighbor.cached cost = current cost
            neighbor.weight = calculateWeight(neighbor, target)
            add neighbor to open queue
        }
    }
}

See Also

References

design_pattern/a_algorithm.txt · Last modified: 2009/09/25 05:47 by 134.102.132.34
Back to top
Creative Commons License Valid CSS Recent changes RSS feed Valid XHTML 1.0