Shortest Path Algorithm

Blitz3D Forums/Blitz3D Userlibs/Shortest Path Algorithm

Moraldi(Posted 2007) [#1]
Hi,
I am not sure if this has been discussed before but I implemented the Dijkstra's algorithm trying to solve the Single-pair shortest-path problem in a 3D graph. I think that many people are looking for this because many issues in Game programming are related to this. So here we go:
First the link for the Blitz3D code:

http://www.moraldigames.com/Temp/Spa.zip

The interface is quite simple:
First you must create a graph using the following command:
mygraph = Graph_Create(0)
0 is the graph id (you can create many graphs using different id's)

Then you have to build the graph by adding vertices:
v1.Vertex = Graph_CreateVertex(mygraph, x1, y1, z1)
v2.Vertex = Graph_CreateVertex(mygraph, x2, y2, z2)
...

In order to create connections between vertices use:
Vertex_Connection(v1, v2)
...

After the construction of the graph simply call:
Graph_FindShortestPath(mygraph, src, dest)
where src and dest are existing vertices within mygraph
Each graph has the bestsearch vertex and after the previous call you can trace the shortest path following the predecessor member of the bestsearch vertex

The most interesting is that you can define your own 'cost' between two vertices by modifying the Vertex_GetCost() function in the way you like. Currently in the program the 'cost' between two vertices is their distance

If you are confused check the source.
If you build a graph and you find that the program does not calculate the shortest path please let me now in order to debug it.

Enjoy!


Moraldi(Posted 2007) [#2]
Hey, none has commented this! :(
I thought it was very useful for most on Blitz3D threads.


lo-tekk(Posted 2007) [#3]
Hi Moraldi, this is really good work and comes quite handy. Highly appreciated, Thank You !


Moraldi(Posted 2007) [#4]
I found one! Thanks lo-tekk :)


Dustin(Posted 2007) [#5]
I'll second lo-tekk's thoughts. I haven't DL'd it yet but taking the burden of converting the ol' A* routine is great news. Thanks for the great contribution!


Moraldi(Posted 2007) [#6]
Thanks Dustin but the time you needed to post is much more than the downloading time... :)


Dustin(Posted 2007) [#7]
Agreed. But I'm at work and they've done a fine job of rendering the USBs useless. And lately I've been 12+ hours/day so the machine at home hasn't gotten much use lately. But I'll grab it soon enough.


Moraldi(Posted 2007) [#8]
Oh, I see USB's they are not suffers only in Greece..


puki(Posted 2007) [#9]
I downloaded it - but I am playing Crysis.


Moraldi(Posted 2007) [#10]
puki playing Crysis, my brother playing Crysis, all my friends playing Crysis. I am the only playing with Blitz3D...? :)


ShadowTurtle(Posted 2007) [#11]
at moment i do play Unreal Tournament 3..


Ian Thompson(Posted 2007) [#12]
Good job! :)

Why not place this in the code archives?


Moraldi(Posted 2007) [#13]
Thanks Ian, yea you have right I will put this in code archives...


SabataRH(Posted 2007) [#14]
I will comment, EXCELLENT!
I'm always interested in good pathfinding solutions and this seems to be a good one!

Even on very large layouts this baby rolls right along.
Thanks for sharing this algo.


Moraldi(Posted 2007) [#15]
Thanks Swampdonkey!
Did you build a large graph? can you post it?.


(tu) sinu(Posted 2007) [#16]
Nice, i did this a few years back, don't think i ever released the source or editor.

It's was good learning session. Dijkstra's algorithm is cool and simple to code.


Moraldi(Posted 2007) [#17]
It's was good learning session

It wasn't for me a learning session but decided to release because this wonderful community many times help me too in the past.


SabataRH(Posted 2007) [#18]
Hey Moraldi,

Yah I built a rather extensive scene with it and it worked with no problems. Im intergrating it into my world editor. No screens to show at this time, but later ;)

Thanks again.


Moraldi(Posted 2007) [#19]
Thanks for the info. It helps me a lot because I indent to integrate to my Mesh Factory library


patmaba(Posted 2007) [#20]
I have created a demo about shorthest path on blitzmax
using dijkstra algorithm.

It's a beta version. The Algorithm use Dijkstra. Note A* is an update of Dijktra using an heuristic( usually, distance between start position and target position )

http://www.box.net/shared/kv47bffppz


YellBellzDotCom(Posted 2007) [#21]
I sure would like to se what you have PatMaba, but I dont have the b3d.dll, what library is that from?


Moraldi(Posted 2007) [#22]
Glad to see an increasing interest on shortest path methods. I believe these algorithms can give game-play depth in Blitz3D games.


Axel Wheeler(Posted 2008) [#23]
Well done. I didn't have debug on, so it went fullscreen and thus I had no pointer. I was confused for a bit, but after changing the graphics mode all was well.

Graphics3D 800, 600, 32, 2

I would guess a common use for this might include:

Creatures chasing a character around a complex environment (which could be a single room or an entire complex, as in "Intruder alert: all units to Sector 12")

Economic managment in trading games (space routes, for example)

Other applications? (games or not)


Moraldi(Posted 2008) [#24]
Civ-type or strategy games in general.
You can set a cost for each node taking into account its resources.
This way a computer controlled player can decide to go to the node with maximum cost finding his way with this algorithm