Motion in Games

In my sixth video from the “Building a Game Development Framework” series, I demonstrate how to build a robust Motion System on top of the LibGDX framework. This system is designed to simplify how character animations are managed and triggered based on the movement direction.

The core of this system is the Motion interface. Similar to the Animation class from the earlier videos, the “motion” encapsulates all the possible animations for a character (e.g., walking north, south, east and west) and decides which one to play base on the character’s current heading.

To handle the movement logic, I introduce a Direction class that uses the FastMath utility class for performance. This class contains:

  • Pythagorean Theorem & Angles: It calculates the angle of movement between an origin and destination.
  • Cardinal Directions: It includes methods to snap to a precise degree (0–360) to the nearest 4-way (North, South, East, West) or 8-way cardinal directions.
  • Normalization: A helper method ensures angles always stay within the 0 to 360-degree range.

I developed classes in the framework to be used depending on how complex your character’s visuals are:

  • Single Motion: Displays the same animation regardless of direction (useful for objects like a spinning coin or a single Animation).
  • Two-Way Motion: Switches between two animations, typically for horizontal (left/right) or vertical (up/down) movement.
  • Four-Way & Eight-Way Motion: These implementations map animations to the standard cardinal and inter-cardinal directions. I show how to streamline this by creating constructors that take a single sprite sheet and automatically slice the necessary frames for each direction.

Since the Motion class contains Animations, it supports the Animation’s set color method, making it easy to change the color.

A significant addition, I added, is the LayeredAnimation class. This allows you to stack multiple animations on top of each other. This Animation class allows several Animations to be treated as a single Animation, layering them to produce a single object. The first Animation added is the bottom layer, and subsequent additions are rendered on top. Here I demonstrated with the “Magic Egg”, with a ring and egg underneath it. This allows us to color these two separately.

This modular approach allows game developers to change a character’s “Motion” type (e.g., from 4-way to 8-way) without rewriting their core game logic, making the framework highly flexible for different art styles.