HOME | DD

JohnJensen — A* Pathfinding in my RPG

Published: 2012-03-19 21:44:51 +0000 UTC; Views: 2416; Favourites: 15; Downloads: 22
Redirect to original
Description So I've come to the point in my development where the basics of the battle system are complete and the inventory systems are basically complete - few things missing here and there.

Then I was debating with myself how to make the overworld in my game, and I settled with a click based movement system - since it's not a real-time swordplay game like Zelda, but a turn-based RPG like Final Fantasy, I thought I might aswell make the overworld clickable. I started reading about graph search algorithms and pathfinding in my book "Algorithms In A Nutshell", and I settled with using A* pathfinding, which is a great algorithm that finds a path based on a heuristic. What in the world is a heuristic, you may ask, and I didn't know either at first, but I found out it's a method in which the search algorithm (we're performing a search of the destination tile from the source tile) in which we calculate the direction of the destination compared to the current position and compared to the source tile, and settle with the direction with the least cost. The great thing about A* search is that it only searches in areas of the map the algorithm finds wise to look in, for example the Dijkstra's algorithm searches the whole map and finds a path, which will result in worse performance since we want a game to run fast, so A* is a better solution in this case.

Wikipedia has a great visualization of both A* and Dijkstra:
Dijkstra: [link]
A*: [link]

As you can see in the two examples above, A* looks through a lot less nodes (tiles) than Dijkstra, so it is more efficient, as I just want to find a good path, fast.

I'm considering precalculating paths for the final game when the final maps are made, which means all possible paths will be precalculated by me outside the game, and path lookup will be blitzingly fast.

I must warn you, you may click on a tile that's so far away it might take a few seconds to compute the path, but I'm not sure, when I've been testing this in a browser it's been running faster than in Flash, so I'm not entirely sure.

I hope you enjoy this example of A* pathfinding.
Related content
Comments: 14

fknudsen [2012-03-29 21:29:55 +0000 UTC]

Long paths took exactly 0.01 seconds to calculate. Not even noticeable.

👍: 0 ⏩: 0

livachof [2012-03-26 03:02:02 +0000 UTC]

beautifull

👍: 0 ⏩: 0

KupoGames [2012-03-19 22:55:14 +0000 UTC]

Neato, I'm gonna be doing similar navigation in my next RPG, and was thinking how I'd do the path finding.

I was planning on just using Dijkstra, but since you've brought it up, I may look up some other solutions.

Well, since my maps are small, efficiency isn't really an issue for me...

👍: 0 ⏩: 1

JohnJensen In reply to KupoGames [2012-03-20 11:40:04 +0000 UTC]

Dijkstra's alright if you got some fairly small maps as you said, I've just done some testing of my A* implementation on a 9,000,000 node map with 3000x3000 tiles, if you let a blind algorithm like Dijkstra loose on a map like that, it could definitely lead to some overkill. xD

👍: 0 ⏩: 0

CreativeSparkStudios [2012-03-19 22:39:44 +0000 UTC]

Good job on this!

👍: 0 ⏩: 1

JohnJensen In reply to CreativeSparkStudios [2012-03-20 11:40:19 +0000 UTC]

Thanks! :)

👍: 0 ⏩: 0

UnrealCanine [2012-03-19 22:39:12 +0000 UTC]

I noticed a little blimp at one point, but it has some good pathfinding

👍: 0 ⏩: 1

JohnJensen In reply to UnrealCanine [2012-03-20 11:41:10 +0000 UTC]

Yeah, it was bound to happen. xD It usually happens when computing paths more than 150 tiles long in this example. Thanks!

👍: 0 ⏩: 0

KnebulaNight [2012-03-19 22:13:04 +0000 UTC]

Holy cow that is wickedly awesome.

👍: 0 ⏩: 1

JohnJensen In reply to KnebulaNight [2012-03-20 11:41:58 +0000 UTC]

Thank you, Knick :3
Imagine Rex + pathfinding in MnMD3

👍: 0 ⏩: 1

KnebulaNight In reply to JohnJensen [2012-03-20 17:05:19 +0000 UTC]

The gameplay is far to simplistic for Rex to need pathfinding. XD

Someone once mentioned Waypoints, but both would ruin the strategy, really.

Unless the game took to some 3D elements or whatnot.

👍: 0 ⏩: 0

ClearFog [2012-03-19 21:52:44 +0000 UTC]

Vert Smart!

👍: 0 ⏩: 1

JohnJensen In reply to ClearFog [2012-03-19 21:53:32 +0000 UTC]

Thanks! :D

👍: 0 ⏩: 1

ClearFog In reply to JohnJensen [2012-03-19 21:58:34 +0000 UTC]

Np! your awesome!

👍: 0 ⏩: 0