A Star Pathfinding

Monkey Forums/Monkey Code/A Star Pathfinding

Pakz(Posted 2015) [#1]
Here is the A* pathfinding algorithm.

The code makes a map and creates and draws a path every second. (SetUpdateRate(1))

Here a screenshot :


Here a video of it running at SetUpdateRate(60) on a Intel N2820 laptop.

Here the code :




Pakz(Posted 2015) [#2]
Here is the A* pathfinding algorithm.

The code makes a map and creates and draws a path every second. (SetUpdateRate(1))

Here a screenshot :


Here a video of it running at SetUpdateRate(60) on a Intel N2820 laptop:


Here the code :




CGV(Posted 2015) [#3]
Nice work.
Is it possible to switch off diagonals?
In some types of games you only want horizontal or vertical moves.


Pakz(Posted 2015) [#4]
Here is a modified version of the code with horizontal and vertical moves.




[VOLOFORCE](Posted May) [#5]
That's great. In case the programmer is still available why is there:

Local gg = map[newx][newy]+1

Could you explain why you add 1?


Pakz(Posted May) [#6]
gg is the go to cost.

It was a time ago that I long ago worked with it but I thought I converted it from the book I had.

You could try removing the +1 and see if it works then. The terrain cost is added in that line. I think that the +1 is needed though.


[VOLOFORCE](Posted May) [#7]
Thanks. I changed the number a bit but the result was always the same. It's a pretty neat implementation of the algorithm, I must say. I have ported it also to C#.

Is it possible to modify the code so that the terrain cost can be modifed by the direction you enter it, e.g. for height levels, coming from height 2 to 4 (more costs) or coming from 4 to 2 (less costs).


Pakz(Posted May) [#8]

Is it possible to modify the code so that the terrain cost can be modifed by the direction you enter it, e.g. for height levels, coming from height 2 to 4 (more costs) or coming from 4 to 2 (less costs).



I am not quite understanding what you are writing here. The map value (each cell) has it's own height value. It reads that value and then adds this to the list. It then calculates the lowest value in the code and choses this as the next step.
On the map you assign each grid value with a height value. On the image in this thread the lighter colors are higher height values and the darker colors are lower step values. If the image was one color then all map values would be have the same cost value.

Edit: The way it works is that it floods the map (think water) and it flows into the lowest area first. It also bases it next flow direction on the distance of the current position and the target position. Think of map() of a heightmap with each cell having a height.


[VOLOFORCE](Posted May) [#9]
I am just stating that e.g. heights work different from terrain type. With heights it depends on the direction you enter the square, with terrain it needn't matter, as e.g.street would be cost 1, mud would be 3, rock would be 5, but regardless of the direction you enter the square. So I suppose the first case is included, while the second one would be done by simply adding the cost.

[3][5][1]
So entering from 3 to 5 would cost less than from 1 to 5?


Pakz(Posted May) [#10]
The height of the square you enter gets added. It does not matter what the height is on the tile you are on. So from 3 to 5 costs 5. And from 1 to 5 costs 5.


[VOLOFORCE](Posted May) [#11]
Yes, that's how I thought it works. So I will have to modify it.


[VOLOFORCE](Posted May) [#12]
For Local y=-1 To 1
For Local x=-1 To 1
newx = tx+x
newy = ty+y
If newx>=0 And newy>=0 And newx<mapwidth And newy<mapheight
If olmap[newx][newy] = 0
If clmap[newx][newy] = 0
olmap[newx][newy] = 1
Local gg = map[newx][newy]+1
Local hh = distance(newx,newy,ex,ey)
Local ff = gg+hh
ol.AddLast(New openlist(newx,newy,ff,gg,hh,tx,ty))
End If
End If
End If


So, Pakz, do you think it will work when I compare the height of tx/ty with that of newx and newx...

so kinda like

if height[tx][ty] < height[newx][newy] then gg = gg + 5


I have the feeling that that would be too easy :)


Pakz(Posted May) [#13]
You can always make special cases with which to add the gg cost. I am still not too sure what it is for but I understand a little bit better what you mean now.