AI Pathfinding for Platformers

A Star (*) pathfinding is great, but how do you use it for a 2D platformer game where you have characters jumping from ledge to ledge? To try and demonstrate a potential solution I assembled the following GitHub experiment that explains how to solve the issue.

Screenshot 2014-07-15 12.29.01
View the live demo / GitHub repo.

There are a few simple mechanics you need to know to solve platformer pathfinding, as it isn’t as difficult as you might think.

Find out where you can’t go

First thing is you need to figure out all of the tiles you can’t get to. Usually this means looking at the top of every ledge and marking it as open.

Runoff and drops

At the edge of every ledge you’ll want to draw a line going straight down at about 160 degrees that represents a runoff point. Think of it as a character running off a ledge. In addition you’ll also want to record drop points directly after the ledge.

Note that I record all of the data as traversable links in each individual tile. If the runoff or drop doesn’t check out (such as falling into nothing), you should probably not record it.

Jumps (the tricky part)

You’ll need to simulate all of your jumps from one ledge to another. This is where a lot can go wrong and you’ll have to write code to simulate the jump based on your own physics to make sure the path is clear. If the jump path doesn’t check out, don’t record it.

A Star (*) to the rescue!

Once you have all of your linked data setup you can then traverse it to get insanely accurate paths to destinations. If you want a more visual overview of how this works, I recommend the following video.

If you have any questions about how I got this working in my own game, leave a comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *