Tilemap Pathfinding now with Travel Costs

TilemapKit is now able to generate grid graphs from tile costs. Rather than using walkable/blocked flags on tiles you would use a “cost” property whose floating point value is in the range 0.0 to infinity.

Though you should try to restrict your value range from 0.0 to 1.0 or 0 to 100, that’ll make it easier to get a feel for the relation of individual cost values.

In this example, the road has a cost of 0.5 while grass is 3.0, ice is 4.0 and mud is 6.0. Any tile without a cost is considered blocked, but you can also specify a negative value to block off a tile.

Tile cost for pathfinding, Tiled propertiesTile cost property for road

The code to generate this graph is even simpler than what you would use for non-cost based graphs:

Each node in the cost graph uses the TKGridGraphNode subclass of GKGridGraphNode, which adds a cost property and implements the costToNode: function to simply return the cost.

You can further subclass this class and override the costToNode: function if you need to base the cost on certain conditions. Or you could fix certain undesirable behavior such as where the roads don’t actually connect even though their tiles are neighbors the cost should probably be higher. You can see this issue in the video if you watch closely:

Path … never been so much fun. 😀


Loading Facebook Comments ...
2 responses... add one

Any chance you’ll have small example projects showing how this stuff works?

Good point! Noted. I’ll add the GameplayKit stuff to the demo as well.

The demo project is available in the beta (check the forum) that showcases (an older version of) TilemapKit. After release, which hopefully will be in about 2 weeks, I intend to have a free TilemapKit demo for evaluation purposes.

Leave a Reply

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