2D Platformer Pathfinding (gravity sucks) - Part 2 - Model

Posted on December 13, 2014

I can hardly remember the artificial intelligence Udacity courses I listened to years ago, but I still remember that it’s very important to determine how the navigatable space will be represented.

It’s fairly obvious anyways, so here we go: I chose to represent walkable surfaces as lines represented by their horizontal left and right edges and their vertical position. Just a line, really. Something like this:

class Surface
{
    public var right:Float;
    public var left:Float;
    public var y:Float;
}

This is even more convenient because identifying these surfaces in a tilemap is incredibly easy.

All we have to do is iterate through the rows of the tilemap. Sample tilemap:

---###-------
##########---
-------------

When we find a tile that has no upper neighbor, we define this tile as the beginning of a surface. Then, we continue iterating until we find a tile that has either no right neighbour or a right-upper (diagonal) neighbour, we define this tile as the end of the surface.

In this example, surfaces are highlighted in red: