Description
A class to simplify A* pathfinding on a 2x2 grid with a optional height field
The single fields can have any value where negative values mean ‘not walkable’ and positive values are interpreted debending on the path finding strategy used
Constructor
new gamvas.AStarGrid(w, h, diag, withFirst, strt, dflt);
Parameters
w | The width of the grid |
h | The height of the grid |
diag | Allow diagonal steps (optional, default: false) |
withFirst | Include the first node of the path in the result (optional, default: true) |
strt | The path finding strategy (see below) (optional) |
dflt | The default value for the grid fields (optional, default: 0) |
Extends
gamvas.AStarMap
Strategy
There are several strategies, how the values are interpreted. They only give minor differnce in the result, but in certain situations it might be necessary to use a different strategy
gamvas.AStar.STRATEGY_AVOID_STEPS | (default) The algorithm tries to avoid height differences, so if you start in a valley, it will try to stay in the valley and run around mountains, until there is nothing left other then stepping on a mountain, once on the moutain, it tries to stay on it, until it has to go down to the valley again. |
gamvas.AStar.STRATEGY_IGNORE_STEPS | The algorithm completely ignores height information and always tries to go straight to the target |
Example
The following would create a grid with 50 by 50 fields. As we do not set any field values, default is 0, which means perfect ground to walk on, this example would result in a path that represents a straight line from upper left to lower right corner of the grid.
var pathMap = new gamvas.AStarGrid(50, 50);
var result = pathMap.find(0, 0, 49, 49);
for (var i = 0; i < result.length; i++) {
console.log('Step '+i+' is at 'result[i].position.x+','+result[i].position.y);
}
See
<gamvas.AStar>
Summary
gamvas.AStarGrid | A class to simplify A* pathfinding on a 2x2 grid with a optional height field |
Functions | |
setValue | Set a value val in the grid on position x, y |
getValue | Get the field value of position x,y in the grid |
find | Find a path between two positions in the grid |