How to build a platformer AI: From low-level trajectories to high-level behaviors

A line drawing showing a path across surfaces from a human character jumping onto a bug character.

Overview

In this series of posts, I describe techniques for implementing AI, pathfinding, and character movement in 2D platformers. I start out by covering some general mechanics of platformers. Then I describe a series of powerful abstraction layers built on top of these basics.

I make references to the Surfacer framework for real-world examples of these techniques. Surfacer is a procedural pathfinding 2D-platformer framework for Godot. It makes it easy to create point-and-click platformer games and to define high-level NPC behaviors.

Although Surfacer is written for Godot, the techniques we discuss in this series are not specific to Godot, and will work in any game engine of your choice!

Contents

Part 1: Basic movement

A line drawing showing a human character jumping with a parabolic trajectory.
  • How an animation frame loop and physics engine work.
  • General platformer mechanics.
  • Surfacer's extensible action-handler system.

Part 2: AI, graphs, and surfaces

A screenshot showing individual surfaces that have been constructed from underlying tiles.
  • How AI movement usually works.
  • How to represent a platformer level as a graph.
  • How to parse Godot tile-maps into their constituent surfaces—floors, walls, ceilings.

Part 3: Calculating jump trajectories

A screenshot showing a jump trajectory between surfaces, with corresponding instructions.
  • Some trade-offs of this approach.
  • How to decide on "good" jump and land positions for a pair of surfaces.
  • How to handle start velocity.
  • Separating the vertical component of motion from the horizontal component.
  • Detecting collisions with intermediate surfaces.
  • Calculating "waypoints" to move around intermediate surfaces.
  • Backtracking to consider a higher max jump height.

Part 4: The platform graph and navigation - (COMING SOON!)

A screenshot showing all of the possible edge trajectories in a platform graph, and a path through the graph.
  • How to construct a platform graph.
  • Nodes as positions along surfaces.
  • Edges as movement from one position to another.
  • Handling platform immutability.
  • Navigating through the platform graph to an arbitrary position.
  • Optimizing edge trajectories at run-time for a more organic composite trajectory.
  • Executing playback of edge instructions.
  • Correcting for runtime vs build-time trajectory discrepancies.

Part 5: Character behaviors - (COMING SOON!)

An animated GIF of a recording showing various character-movement behaviors in Surfacer.
  • Examples of common platformer behaviors.
  • Transitioning between active behaviors.
  • Adding extra bounce.
  • Timing behaviors.


Comments