A* Pathfinding Project

Adding Nodes on Runtime


#1

Hello there,

i want to know if its possible to add nodes on runtime and how?


#2

Hi

You can do this using the PointGraph.AddNode method.

AstarPath.active.AddWorkItem(new AstarWorkItem(ctx => {
	var graph = AstarPath.active.data.pointGraph;
	// Add 2 nodes and connect them
	var node1 = graph.AddNode((Int3)transform.position);
	var node2 = graph.AddNode((Int3)(transform.position + Vector3.right));
	var cost = (uint)(node2.position - node1.position).costMagnitude;
	node1.AddConnection(node2, cost);
	node2.AddConnection(node1, cost);
	// Required because we have changed connectivity of the graph
	ctx.QueueFloodFill();
}));

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


#3

Thanks, i got it.
I just need to scan on Start the Graph, and every new Node is recognized, thanks this works.

Now i have a problem, and i dont know how to solve it.

I want to create an road/traffic system for my game, where cars need to drive from Point A to Point B and on the right side.
Travelling from Point A to Point B works, but i dont know, how to tell my vehicle to drive on the right side and not on the left side.


#4

Hi

If you need them to only move in one direction then you should only add connections in one direction instead of both directions. However note that this package is not intended for road/traffic systems which often require relatively complicated rules to work well.


#5

Thank you, i have only one way directions.
But i have also intersections where you can cross to the other path.

Is it maybe possible to say, follow only the nodes witch point with the z-axe?


#6

Is it maybe possible to say, follow only the nodes witch point with the z-axe?

The only thing the system cares about is if there are connections in different directions. You will have to construct the connections between the nodes so that they follow the rules you have for the roads, but I cannot guarantee that this will be possible.


#7

So, adding a penalty for moving in the positive Z direction is not possible?
If i could do this, this would helps me a lot.


#8

You can make different connections have different costs (see code example in my first post) which means you can make connections in the +Z direction have a higher cost.