A* with user defined grid

Hi all. Great project you have here!

I have read though the Documentation and am wondering if this project is the best way to go.

I have a fairly simple game, that requires pathfinding. It is similar to a checkerboard with some squares passable and some not. I already have a grid defined as a 2D Boolean array where each member is whether that square is passable or not.

Is this project usable for this? I wouldn’t be sampling a true terrain, is there an entry point to the library where you can enter a 2D array and have it pathfind between two coordinates on it?

Thanks in advance,.

Hi

You can create a grid graph and then update the nodes with the desired settings.

First create a graph from code like this (or you can do it in the inspector)

 var graph = AstarPath.active.astarData.AddGraph(typeof(GridGraph)) as GridGraph;
 graph.width = 10;
 graph.depth = 10;
 graph.center = Vector3.zero;
 graph.UpdateSizeFromWidthDepth();
 AstarPath.active.Scan();

Then apply settings. The easiest way is to subclass the GraphUpdateObject, but it can be done by directly modifying the nodes as well.

public CustomGraphUpdate : GraphUpdateObject {
	public override void Apply (GraphNode node) {
		var gnode = node as GridNode;
		// I'm not sure if these exist in the latest version or only in the beta
		// If they do not exist you can use
		// var x = gnode.NodeInGridIndex % gg.width;
		// var z = gnode.NodeInGridIndex / gg.width;
		var x = gnode.XCoordinateInGrid;
		var z = gnode.ZCoordinateInGrid;

		// Check your array data here
		gnode.Walkable = x > 10;
	}
}

And to update the graph

var guo = new CustomGraphUpdate();
// Our custom graph update should not do this
guo.updatePhysics = false;
// A bounding box so large that it should cover the whole graph
guo.bounds = new Bounds(Vector3.zero, Vector3.one*100000);
AstarPath.active.UpdateGraphs(guo);

This is easier than setting the node properties directly as it takes care of some things behind the scenes for you (such as recalculating the node connections and calling AstarPath.FloodFill).

I haven’t tested the code above, but I think it should work.

See also http://arongranberg.com/astar/docs/graph-updates.php