new AStar(isWalkable)
Implements the A* pathfinding algorithm on a 2-dimensional grid. You can use this to find a path between a source and destination coordinate while avoiding obstacles.
Parameters:
Name | Type | Description |
---|---|---|
isWalkable |
isWalkable | A function to test if a coordinate is walkable by the entity you're performing the pathfinding for. |
Methods
heuristic(x, y)
The A* heuristic, commonly referred to as h(x), that estimates how far a location is from the destination. This implementation is the Manhattan method, which is good for situations when the entity can travel in four directions. Feel free to replace this with a different heuristic implementation.
Parameters:
Name | Type | Description |
---|---|---|
x |
number | The x coordinate to estimate the distance to the destination. |
y |
number | The y coordinate to estimate the distance to the destination. |
search(srcX, srcY, destX, destY) → {Array}
Search for an optimal path between srcX, srcY and destX, destY, while avoiding obstacles.
Parameters:
Name | Type | Description |
---|---|---|
srcX |
number | The starting x coordinate |
srcY |
number | The starting y coordinate |
destX |
number | The destination x coordinate |
destY |
number | The destination y coordinate |
Returns:
The optimal path, in the form of an array of objects that each have an x and y property.
- Type
- Array