gamvas.AStarGrid

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

wThe width of the grid
hThe height of the grid
diagAllow diagonal steps (optional, default: false)
withFirstInclude the first node of the path in the result (optional, default: true)
strtThe path finding strategy (see below) (optional)
dfltThe 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_STEPSThe 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.AStarGridA class to simplify A* pathfinding on a 2x2 grid with a optional height field
Functions
setValueSet a value val in the grid on position x, y
getValueGet the field value of position x,y in the grid
findFind a path between two positions in the grid

Functions

setValue

setValue: function(x,
y,
val)

Set a value val in the grid on position x, y

Description

Negative values are ‘not walkable’ while values of 0 or higher are ‘walkable’ where depending on the strategy the positive values may be interpreted as height map

Parameters

xThe x position in the grid
yThe y position in the grid
valThe value, where < 0 means ‘not walkable’ and >= 0 means ‘walkable’

getValue

getValue: function(x,
y)

Get the field value of position x,y in the grid

Parameters

xThe x position in the grid
yThe y position in the grid

find

find: function(xs,
ys,
xe,
ye)

Find a path between two positions in the grid

Parameters

xsThe x position of the start field
ysThe y position of the start field
xeThe x position of the end field (aka target)
yeThe y position of the end field (aka target)

Returns

An array of gamvas.AStarGridNode.  If no path is possible, the array has a length of 0

setValue: function(x,
y,
val)
Set a value val in the grid on position x, y
getValue: function(x,
y)
Get the field value of position x,y in the grid
find: function(xs,
ys,
xe,
ye)
Find a path between two positions in the grid
A flexible 3D node based A* pathfinding system
The node information of a gamvas.AStarGrid map
Close