Hi there
I don’t have a specific question per se, but would appreciate some discussion on this topic.
As with many game developers, I have now reached the point where Pathfinding has become a problem. I have spent some time on the following idea, and would like you to comment on it, or find errors in my thinking. I realize that I am probably re-inventing the wheel, in which case I would appreciate it if you point that out to me. Also, I think that this method may only work for small networks, because you start running into problems somewhere between “The Towers of Hanoi” and “The Traveling Salesman”.
So, without further ado…
Here is the network of nodes connected by existing paths. SP is the Start Point, EP is the end point. How to get from SP to EP (shortest route)? A very important consideration in this case is that by “shortest path”, I mean “the least number of nodes to traverse”, and NOT “shortest physical distance”. That would be “Best Path”. However, it it easy to figure out which path is the shortest if you have the x, y (and z) coordinates of the nodes.
http://www.gps-coords.co.za/path/network.jpg
Make a list of all the connections. At this point check if a connection between SP and EP exists (in this case it would have been 9-6). If there is, you have found the only (and shortest) path. If it doesn’t, continue.
http://www.gps-coords.co.za/path/cnxlist.jpg
Starting at the top left of the connections list (node 1), start eliminating duplicate connections. In this example, 1-3 is the same as 3-1, 1-4 is the same as 4-1, etc.
http://www.gps-coords.co.za/path/cnxrem.jpg
You are now left with a list of all the connections
http://www.gps-coords.co.za/path/cnx2.jpg
Separate the start and end points from the intermediaries. If a path element contains the start point (9) it will be considered a start path, if the path element contains the end point (6) it will be considered an end path, all other path elements are considered intermediaries. At this point, check if there are any shared nodes between start paths and end paths (9-x, x-6). If there are, calculate the lengths of these paths to determine the shortest route. There may be any number of these three-node paths, find all of them, find the shortest, end. If not, continue…
http://www.gps-coords.co.za/path/cnxsep.jpg
When no two node paths (9-6), or three node paths (9-x-6) could be found, things start getting a little complex. In our example, there are two path leading from node 9 (start point) and two paths leading to node 6 (end point). Let’s call the start point paths 9-a and 9-b, and the end point paths x-6 and y-6. Ideally, if there exists a 4 node path, we should look for a-x, a-y, b-x or b-y. These mid-paths, if they exist, will be found in the Intermediary path list. If more than one path is found, calculate distance, find shortest, end.
http://www.gps-coords.co.za/path/iterate.jpg
If no 4-node path was found, the next logical step will be to seek for a 5-node path. More or less the same procedure is followed. Again, we know that there are two paths leading from SP, namely 9-1 and 9-8. We also know that there are 4 intermediary paths with node 1 as an element, namely 1-3, 1-4, 1-7 and 1-8. Since the 9-1-3 path is established, we now consider the first intermediary path element (1-3) as the start point, and use the same method as above to find the shortest route to EP. Ideally this will be 3-2-6 (looking for 2-3 or 3-2) or 3-5-6 (looking for 3-5 or 5-3), since we know that the two paths converging on node 6 come from node 2 and node 5. If such a path is found, we would have 9-1, 1-3, 3-2, 2-6 (9-1-3-2-6 - five nodes) or 9-1, 1-3, 3-5, 5-6 (9-1-3-5-6 - five nodes).
If no path was found, we take the second path element from the node 1 list, namely 1-4. We now consider this intermediary path element (1-4) as the start point, and use the same method as above to find the shortest route to EP. Ideally this will be 4-2-6 (looking for 2-4 or 4-2) or 4-5-6 (looking for 4-5 or 5-4), since we know that the two paths converging on node 6 come from node 2 and node 5. If such a path is found, we would have 9-1, 1-4, 4-2, 2-6 (9-1-4-2-6 - five nodes) or 9-1, 1-4, 4-5, 5-6 (9-1-4-5-6 - five nodes).
If more than one 5-node path was found, find the one with the shortest physical distance. End.
If no 5-node path was found…
And so on.
Comments?