Virtual pathfinding for world generation

Hi there!

I’ve got a block-based world generator where each block is quite large (10m x 10m) and uses a pretty simple couple of noise passes for height. The world is generated at runtime.

What I’m ideally looking to do is use this package to generate roads in my world by creating a ‘virtual’ grid graph and assigning the height values to it programmatically and use those height values as the penalty scores so that roads try and stick to the lowest height levels of the map. Then I would like to somehow path find between two points on the world and access the path data and then assign each block that the path crosses as a “road” block that will then display the appropriate texture.

I know I can do the first part (assigning penalty data to the graph programmatically) fairly easily, but I’m not sure if it’s possible to do the second part (find a virtual path between two points and access which grid spaces the path is on). Is this possible with this package? If so, do you have any recommendations on how I should approach this?

Worst case I write my own simple A* solution for this purpose, but figured I’d see since I have A* Pathfinding Project anyway.

Thanks for your time!

-Chris

Hi!
Have you seen this?
https://arongranberg.com/astar/documentation/4_0_8_e597295/accessing-data.php
https://arongranberg.com/astar/documentation/dev_4_0_0_2e59f2b/graph-updates.php (penalty section)

I think you can do what you want to.
After requesting a path from A to B you will get all the points that lead to the destination. You can loop them and check:
AstarPath.active.GetNearest(position).node.Penalty

Alternatively you can make 2D matrix

byte[,] Matrix
Matrix = new byte[Width, Height];

where one point in the matrix will be one grid on the map.
You can then fulfill this matrix will all the data you want (like penalty). Then when you receive all the points leading to destination, you can loop them and check each point in the matrix, like this:

if (Matrix[x, y] > 12)
{
    //do something
}

Hi Keypax - thanks for the reply!

Huh, I hadn’t seen that documentation site, is it the ‘correct’ one vs. this one? It seems to have more on it, which is great!

The adding/storing/using penalties seems pretty straightforward; for acutally getting the path data does this sound correct (i.e. not using a Seeker gameobject - just through code):

It looks like I would call ABPath to pass my two points, then use the returned path and access the nodes through p.path: then reconcile those nodes with my world grid. I’m a little confused on how AstarPath.StartPath() fits in though - once I return a path from ABPath, what does StartPath() do? The example on this page under “Calling AstarPath directly” is what I’m referring to. Don’t I already have the path once I’ve gotten the return from ABPath?

Thanks so much for your time, this has helped a lot!

-Chris

No problem :slight_smile:
As far I know, there is old version of the documentation and the new one, but author will need to verify my words :slight_smile:

In my project I only need two things. Request for the path and the output. Output is a list of all the points (Vector3) that should be crossed to reach the destination.
In my option you need to do:

  • request for path from A to B
  • check if path is successfully generated
  • get vector points and save it wherever you want
  • follow the path from the output

I also using this plugin without GameObjects (I use them only in necessary).
Seeker component does a lot things for you and personally I use the poll of Seekers components with separate poll for “smooth modifiers”. If you want, I can share my solution with you. I have different Seekers prefabs for different movement types (like animals and npcs).

It’s the old documentation. It has less stuff on it :stuck_out_tongue: It might look like more because it’s less organized though.

Calling ABPath.Construct only creates the path request. But the path hasn’t been calculated yet. You need to call AstarPath.StartPath (or seeker.StartPath) to calculate it. The path will be calculated asynchronously and you can access it later (you can also force it to run synchronously, more about that in the docs).

Haha thanks Aron, much appreciated - that’s the info I needed!