GPS using PathfindingService

I am using PathfindingService to create paths between points. I have the costs set up so that the agent prefers Asphalt (the road material) over anything else. I am trying to figure out how to keep the path on the correct side of the road (that would be the right for my purposes).

Current path:

Ideal path:

Any ideas?

2 Likes

Make the costs for going on the left side of the road higher. You’re going to have to manually assign where they have to go for all the roads (just like irl, there’s a “hard coded” direction you have to follow)

1 Like

Have you tried using pathfindingmodifiers? you could try to make a dynamic system with that.

1 Like

What is the best way to define the left side vs. the right side? It would swap when going the other direction.

Separate this into a pathfinding step, which you already have, and a steering step. The steering step should use the selected path to determine the current and next road. It should use the current movement direction and road segment to generate the target position, on the correct side of the road.

2 Likes

I have one made for the lines, but it didn’t really work that well.

i think it might be better to cover the entire left lane.

1 Like

What do you mean “cover” the left lane?

Make an invisible part that spans the left lane, with a pathfinding modifier.

2 Likes

So, basically regenerate the path after each “steering step”?

I see you’re using the material cost and assigning asphalt. You can also do something similar with any part using a PathfindingModifier. So basically make a “right” and “left” side of the road where players prefer to go on the right side path

documentation: PathfindingModifier | Documentation - Roblox Creator Hub
example: Character pathfinding | Documentation - Roblox Creator Hub

1 Like

Yea this is probably the best option to do

2 Likes

I see. This could work, I’ll give it a shot.

I do really like this idea, so I will probably try this first.

After doing some research, I’ve found that PathfindingService really isn’t the best tool to use for this. After doing some thinking, I realize that what I truly want are predefined nodes that are selected by an algorithm to create the best path (That is how modern GPS systems work) - Dijkstra’s Algorithm and the A* (A star) Algorithm are 2 that are most common for this.

If you have any other suggestions on the best way to go about this method, or a potential better method, I would love to hear them.

I have always been fascinated by the idea of creating a GPS system, but I haven’t really gotten around to making it. Thanks for your input, as it did really help me think through how this system should actually work.

Edit: My thoughts so far are to create a folder called “Nodes”, then have Part instances inside. Under each part will be an ObjectValue that would have the connected node as it’s Value. Each node could have multiple of these values.

Inside of a script, I would start at the selected starting node and get it’s connected nodes (through the connected node values I created). Then I would loop through each of it’s connected nodes for their connected node values, repeating the process until the final node is equal to the selected end point. Obviously this would include checks that ensure a node hasn’t already been used, etc.

I would then have I shortestPath variable, initially set to math.huge. As I loop through and create each “path”, I would keep track of how many nodes are in the path, calculating the total distance and setting it equal to the shortestPath once I reach the final node. For efficiency, if I am processing a new path and it becomes longer than the current shortestPath, I would just return and create the next path, as that path would already be too long to be counted.

1 Like

You can do it this way yes. You can probably find an existing A* that you could adapt to pass info about your roads.

1 Like