Pathfinding 2D

Monkey Forums/Monkey Code/Pathfinding 2D

NoOdle(Posted 2011) [#1]
EDIT: New Code further down!!

I decided to try out an idea I had last night to simplify grid based pathfinding. The typical method I have seen a lot of people use is to have a grid separated evenly by some pre-determined tile size. This works well for 2D games but I wanted to speed this up by removing all the tiles that wouldn't need to be considered in pathfinding.

My method was to create nodes on the corners of any solid tile, linking these nodes using line of sight visibility. This node map could then be used for normal A* pathfinding with much fewer iterations.

There is a slight limitation with this method. If you wanted to having different costs for moving from one tile to another this wouldn't be able to account for that.

A fairly decent way to combat this however is to use the same method for these tiles as above. For example if you have an area of muddy swamp you would create the nodes around the solid tiles first (walls) and also create nodes around the swamp area.

When the links are calculated the cost of moving across this link could take into account the type of tile it has to cross. The mud tiles would have a higher cost than a grass tile for example. This would then yield the desired result, a path around the mud would have a lower cost than the direct path through the mud.

Anyway, I will be updating this tonight with an A* pathfinding algorithm implemented to show this. But for now here is a simple demo showing how to generate a simple node map.

Left Mouse to create a tile
D Key to delete a tile
Enter creates the node map

It does take a second or two to make the node map because I was quite lazy implementing this!




matt(Posted 2011) [#2]
I like this, very nice


NoOdle(Posted 2011) [#3]
As promised, here is a pathfinding demo. I haven't had time to implement it into the code above, it is a stand alone example for now.

Press S to set start node
Press G to set goal node
Press Enter to start pathfinding




NoOdle(Posted 2011) [#4]
Here is a newer demo of the both examples combined.

Left mouse places wall tiles on the grid
D Key deletes the wall tile under the cursor
S places start location for pathfinding
G places goal location for the pathfinding

Before you can run the pathfinding you have to create the node map. This would be done when the level is loaded but for this example it is dynamically created during runtime.

Press Space to create the node map and links

Then you can press enter to run the pathfinding. If Start -> Goal is visible it will show a link between the two. Otherwise it will highlight the waypoints and the path route will be displayed in the left hand corner.

You can edit the tiles at any point, but remember to press Space to recreate the node map before running the pathfinding again by pressing Enter.

Enjoy :)




NoOdle(Posted 2011) [#5]
I will be converting, cleaning and optimising this soon and will post it as a module. Please let me know if that will be useful, it will probably make me work a bit quicker ;)


VicViper(Posted 2011) [#6]
Your pathfinding code is useful.
Great idea. Thanks you for your work.

VicViper


slenkar(Posted 2011) [#7]
this will be useful thanks


Neuro(Posted 2011) [#8]
The is definitely pretty damm cool :). Actually, works pretty quick alreadt but wouldn't mind seeing it faster :)!