- Home /
How to find the shortest path based on waypoints and their neigbors to reach a destination
Hello! So im want to make a game where are some nodes or planets, some planets are connected and you can send your army through them. Im stuck at implementing system that determines the shortes path from selected start planet to selected end planet. Look at screenshot from game Eufloria (pay attention to arrow)
I know there is A* and Djikstra pathfinding, but i saw only how they are implemented on grid based system. How can i do that with waypoint system, noticing that, not all waypoints are connected with each other.
I figured that every planet should know its neighbors, easy. Checking if neighbor planet has at least 1 path is easy too, but how to find paths and to choose shortest is a bit confusing. And what if you have situation like this.
As you can see first node from red path is closer to end goal ( heuristic if using a*), but the total blue path is more shorter. Or am i wrong, can you explain should i use pathfinding on system like this. Thanks in advance.
Answer by Hilfor · Nov 16, 2016 at 08:46 AM
You actually can use Djikstra for this, but you'll need implement the algorithm yourself.
You need to create a graph containig all of the nodes (planets) and their connections to the other planets. If these are static nodes (i.e not moving) then the distance between every node can be calculated at the creation time using the absolute value of (transform.postition - targetNode.transform.position) and saved on every node. If the nodes are not static then the distance should be calculated every "tick" i.e on Update.
When you actually need to move you need to execute the algorithm and it should scan every possible path from the start node to the target node (planet), and then return the path to the start node.
You can checkout this video explaining Djikstra's algorithm
https://www.youtube.com/watch?v=WN3Rb9wVYDY
@UNDERHILL, this is not really a TSP (Traveling Sales Person) problem.
TSP is about connecting all of the nodes on a graph in the most optimal way possible.
The path, that we get after solving TSP, is the shortest path that connectes all of the nodes on the graph.
You need a logical representaion of the nodes layout in the level. Imagine you have your planets as nodes N1..N4 (for example). When you create the level, lets say that you manually set the neighbours of your nodes, for example:
N1 is neighbour of N2, N3
N2 is neighbour of N1, N3, N4
N3 is neighbour of N1
N4 is neighbour of N2, N1
Next, you need to (in code) search the level for the nodes and when creating the graph ask every node who are his neighbours and add them to a list or something. There are many ways to implement a graph structure, just google it (found a good in depth on msdn here)
I'm guessing that at some point the user will create an action involving one of the nodes (the start node) and another action with another node (the target node). So now you have the start and the end nodes, all is left for you to do is to execute the Djikstra algorithm with the start and end nodes as parameters, and voilà you have a the shortest path between the 2 nodes.
So graph is just a class that contains list of nodes?
And the connections (if any) between them, yes
is this possible to store in a list more than one value for 1 index,
0 - 1,2,3
1 - 5,46
2 - 10,100,1000
or
0 - "$$anonymous$$r Clause", "Snake", "$$anonymous$$ario"
1 - "sadsdasd", "hello", "good bye"
or
0 - "0.1131231f","123.12323f" etc ?
Yes, there are a number of ways you can do that. A matrix (2 dimentional array), a list of arrays, a list of hash sets or a list of any other data structure where you can store more than one value.
In general it is a bad habbit to store more than one value type in a data structure (i.e list, tree, a hash set or in this case a matrix). This will over complicate your code and introduce bugs. For example if you want to compare "hello" and 0, this will not work because one is a number (an integer to be exact) the other one is a string.
I dont need more than one value type in one list, i thought if list can store values in such way, that would be great to store neighbours like you listed above.
neigbors[0] = 2,3,4. So neighbours of planet 0 are 2,3,4.
Answer by UNDERHILL · Nov 16, 2016 at 07:25 AM
Check out http://www.lalena.com/AI/Tsp/
This is one of the most difficult and fundamental challenges in computing.
Answer by Zodiarc · Nov 16, 2016 at 08:51 AM
You can also try the A* Algorithm or if you want something more fancy the Jump Point Search Algorithm
Your answer

Follow this Question
Related Questions
Race Interfaces 0 Answers
Move an object through a set of positions 3 Answers
Facing moving direction using Waypoints 1 Answer
How do I add more waypoints to a moving platform script. 1 Answer