Hi,
Given a 2D array like bool[100,100]
where true
is a collision in my 2D game, is there a way to use this asset to provide me with a list of coordinates generated from a flood fill starting at a specific coordinate? This is not really about pathfinding but I think that this asset can be used for such purposes.
In my specific case I just need the neighbours to be adjacent, but I guess diagonal is fine as well.
Thanks!
Hi
You can do that like this:
// This holds all graph data
AstarData data = AstarPath.active.data;
// This creates a Grid Graph
GridGraph gg = data.AddGraph(typeof(GridGraph)) as GridGraph;
// Setup a grid graph with some values
int width = 100;
int depth = 100;
float nodeSize = 1;
gg.center = Vector3.zero;
// Updates internal size from the above values
gg.SetDimensions(width, depth, nodeSize);
// Scans all graphs
AstarPath.active.Scan();
bool[,] walkable =new bool[100,100]; // Fill with whatever
// You can alternatively use gg.GetNode(x, z) and a for loop
gg.GetNodes(n => {
var node = n as GridNode;
// Assumes your walkable array is indexed like [y,x] and not [x,y]
node.Walkable = walkable[node.ZCoordinateInGrid, node.XCoordinateInGrid];
}
// Recalculate all grid connections
// This is required because we have updated the walkability of some nodes
gg.GetNodes(node => gg.CalculateConnections((GridNodeBase)node));
List<GraphNode> floodFillResult = PathUtilities.GetReachableNodes(gg.GetNode(10, 10));
for (int i = 0; i < floodFillResult.Count; i++) {
var node = floodFillResult[i] as GridNode;
var x = node.XCoordinateInGrid;
var z = node.ZCoordinateInGrid;
}
See https://arongranberg.com/astar/docs/pathutilities.html#GetReachableNodes
1 Like