Class: AStarPathFinder

AStarPathFinder(width, height)

An A* path finder. This class implements the A* path finding algorithm. It is not optimised for efficiency but is designed for ease of reading and use.

Constructor

new AStarPathFinder(width, height)

Make a new path finder, you'll need to load a map and set the start and destination points, but this is a good start :)
Parameters:
Name Type Description
width number Width of the map (how many nodes across)
height number Height of the map (how many nodes down)
Properties:
Name Type Description
width number Width of the map
height number Height of the map
nodes Array.<AStarNode> Array of traversable map nodes
nodesPosition Array.<AStarNode> Lookup array of map positions [y][x]
start Object The starting position in a simple x, y wrapper
destination Object The destination position in a simple x, y wrapper
wg number g weight, the g value will be multiplied by this, tweak the algorithm to your needs
wh number h weight, the h value will be multiplied by this, tweak the algorithm to your needs
path Array.<AStarNode> The final path if one is found, array of nodes from start to destination
Version:
  • 1.0
Author:
Source:
Example
const myStar = new AStarPathFinder();
myStar.loadMap(myMapArray);
myStar.setStart(0,0);
myStar.setDestination(9,9);
let myPath = myStar.findPath();

Methods

findPath(maxLoops) → {array}

Main path finding loop - call this to get a path back. This is the main logic of the A* algorithm.
Parameters:
Name Type Description
maxLoops number Maximum number of loops we can take, useful for debug and stopping on complex maps
Source:
Returns:
Array of path nodes from start to destination or empty if no path found
Type
array

loadMap(map)

Load the map from the array[y][x] supplied. Populates the nodes and nodesPosition array with new AStarNode instances or false values. Map format : True = traversable, False = wall or obstacle Origin : Top Left is at 0,0
Parameters:
Name Type Description
map array The map array
Source:

lowestFOpenNode() → {Node}

Get the open node with the lowest F value, if multiple nodes have same F the lowest h value is chosen.
Source:
Returns:
Single open node with lowest F value out of all open nodes
Type
Node

neighboursOf(node) → {Array.<AStarNode>}

Get the neighbours of the parent node, Open or Unexplored nodes that surround this node in a 3x3 grid. X and Y loop from -1 to 1, this is added to the node.x and node.y to give the neighbour location.
Parameters:
Name Type Description
node Node The starting node
Source:
Returns:
- Array of open or unexplored neighbour nodes
Type
Array.<AStarNode>

setDestination(x, y)

Set the destination position on the map
Parameters:
Name Type Description
x number The map X position
y number The map Y position
Source:

setStart(x, y)

Set the start position on the map
Parameters:
Name Type Description
x number The map X position
y number The map Y position
Source: