HTML and CSS Reference
In-Depth Information
A* with Node Weights
For Example 8-16 , we will be adding weighs to our nodes. We'll do this by simply adding in
some grass tiles to the tile map we have been using in the previous examples. By doing this,
we can change the A* search result in a path avoiding the grass tiles has a lower total node
value sum than one that travels over the grass tiles.
We can add to the weight of each open node by simply giving it a number higher than 1. We
have created our tile sheet to make this very simple. The third tile (or tile index 2) is a grass
tile. With astar.as , as long as a tile has a node value greater than 0, it is considered a movable
ues into account, is the lowest, the path will cross these nodes with higher values. To demon-
strate this, we will now add some grass tiles to the tile map. The changes for Example 8-16
are below. Notice that we are also removing the diagonal movement from Example 8-15 , but
it is not mandatory that you do so. We will look at that in Example 8-17 :
//Example 8-16 changes to example 8-15 tile map
var
var mapRows = 15 ;
var
var mapCols = 15 ;
var
var tileMap = [
[ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
,[ 0 , 1 , 2 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 ]
,[ 0 , 1 , 0 , 1 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 1 , 0 , 1 , 0 ]
,[ 0 , 1 , 0 , 1 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 1 , 0 , 1 , 0 ]
,[ 0 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 , 0 ]
,[ 0 , 2 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 0 ]
,[ 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 ]
,[ 0 , 1 , 1 , 1 , 2 , 1 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 0 ]
,[ 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 ]
,[ 0 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 0 ]
,[ 0 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 , 0 ]
,[ 0 , 1 , 0 , 1 , 0 , 0 , 2 , 0 , 1 , 0 , 0 , 1 , 0 , 1 , 0 ]
,[ 0 , 1 , 0 , 1 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 1 , 0 , 1 , 0 ]
,[ 0 , 1 , 1 , 1 , 1 , 1 , 1 , 2 , 1 , 1 , 1 , 1 , 1 , 1 , 0 ]
,[ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
];
//set up a* graph
var
var graph = new
new Graph ( tileMap );
var
var startNode = { x : 4 , y : 1 }; // use values of map turned on side
var
var endNode = { x : 13 , y : 10 };
//create node list
var
var start = graph . nodes [ startNode . x ][ startNode . y ];
Search WWH ::

Custom Search