Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pathfinding algorithm for trains

I'm trying to find a solution for pathfinding in a trains game where there are different kinds of bifurcations. I want the train to go from one rail to another, everything is implemented except the pathfinding.

I need to get a list of rails so the train can follow. Now, the problem is how do I get the list.

  • I've tried A*, didn't work because it stops searching if the node (rail) is already visited. This is a problem, because maybe the way to reach a point is by travelling through the longest route.
  • Tried flood fill, this time made it not stop searching if already visited, the problem is how do I reconstruct the path and how does it choose that it can't go backwards again.

The thing is that there are cases in which the train must go through a rail multiple times to reach its destination.

Any ideas?

Starting point is A, end B. As you see the green path is the way it should travel. The balck circle are the rails which the train will step more than once, in this case 2 times.

A->B

enter image description here

And obviously, you need to come from 2 black to get to 3 red. You can't just go 1black->2red->1red->3red.

like image 255
marcg11 Avatar asked Oct 01 '13 07:10

marcg11


People also ask

What is the best pathfinding algorithm?

A* is the most popular choice for pathfinding, because it's fairly flexible and can be used in a wide range of contexts. A* is like Dijkstra's Algorithm in that it can be used to find a shortest path. A* is like Greedy Best-First-Search in that it can use a heuristic to guide itself.

Should I use A * for pathfinding?

A* pathfinding algorithm is arguably the best pathfinding algorithm when we have to find the shortest path between two nodes. A* is the golden ticket, or industry standard, that everyone uses. Dijkstra's Algorithm works well to find the shortest path, but it wastes time exploring in directions that aren't promising.

What is the fastest pathfinding algorithm?

Dijkstra's algorithm is used for our fastest path algorithm because it can find the shortest path between vertices in the graph. The coordinates on the arena are considered as the vertices in the graph.

What is the simplest pathfinding algorithm?

A pathfinding algorithm takes a start point (also known as a node) and a goal and attempts to make the shortest path between the two given possible obstacles blocking the way. I've always thought the simplest example of pathfinding is a 2D grid in a game, it can be used to find a path from A to B on any type of graph.


1 Answers

Looking at this picture

junctions

It appears your problem would be represented well by a directed graph. Give each stop and each junction two nodes in the graph, one for each direction of the train. Dijkstra's algorithm works perfectly on directed graphs, so once you have the problem in that form, the rest is easy.

So for example, in the picture above, starting from A, we move to junction 1. From there, there's only one place to move to, junction 2, so there'd be an arrow from A --> junction 1 and an arrow from junction 1 --> junction 2. Regardless of which choice you make, you end up at junction 1, but moving in the other direction, so we create a separate node from there. From there, you have the option of going to A or B.

graph

Notice that I removed one of the J1's, since it is superfluous (there's only one place to go).

If the train can stop and turn around at stops like A, we can connect those two nodes by edges in both directions, or just combine them into one node.

You can give the edges weights to specify distances.

like image 92
BlueRaja - Danny Pflughoeft Avatar answered Nov 15 '22 04:11

BlueRaja - Danny Pflughoeft