AI in games I – Autonomous movement

This post is the first part of the talk I gave on the WTM and belongs to this root post.

One of the most fundamental requirements of artificial intelligence is to move characters around in the game sensibly. I mean, how to make a dude move on his own.

At this point we have two options: Steering behaviors and Formations.

1. Steering Behaviors


Steering behaviors are a set of algorithms that allow a character to move across the environment in different ways, depending on a strategy.



So, f.i., a monster who follows you when you walk close to him uses a pursuing strategy. When you run outside his range, he will walk back to the point where he was before you bothered him, employing an arriving strategy. Once he’s there, he will implement a wandering strategy, which will make him move randomly inside a scope.

Strategies can also be combined, for example, the enemy could be pursuing you while evading the walls of the dungeon.

The gifs below show how some of these strategies look like using my beloved gdx-ai tests:

Arrive behavior:


Pursue behavior:

pursueLegal advice: I’m not taking any responsibilities for any harm that octopuses (or similar creatures) may cause when provided with AI. I just do the math.

You can also combine several behaviors using the BlendedSteering behavior.

I’ve shared the screencasts of this behaviors in my youtube channel: Arrive, wander, pursue, BlendedSteering.


Group strategies

Steering behaviors can be applied both for individual and groups. The only difference is that while individuals need a target, groups use a proximity area.

For instance, in the image below, we’re applying a cohesion behavior, which is a group behavior producing a linear acceleration that attempts to move the agent towards the center of mass of the agents in its immediate area defined by the given proximity.


By contrast, separation behavior produces a steering acceleration repelling from the other neighbors within the proximity area:



For implementation we use the calculateSteering method of the SteeringBehavior class, which receives a Steerable agent (the entity to which we want to apply a behavior), and returns the calculated SteeringAcceleration. This output encapsulates both linear and angular accelerations for you to use them on your physics engine.



I’ve written several posts about steering behaviors in the past, find them here, here and here. You can also check davebaol’s tests here and read a deeper explanation at the wiki.


2. Formation Motion


We consider a formation when a group of individuals move in a cohesive way.

gdxAi manages formations using so-called “slots”. Those entities willing to belong to the formation must be assigned a slot. One of the slots is set as the leader, and all the other slots in the formation are defined relative to this slot. Effectively, it defines the “zero” for position and orientation in the formation.

In the following image, the red spacecraft is the leader slot. When it moves, the other spacecrafts will follow it keeping in the V formation pattern:



The leader slot can be replaced by an anchor point if needed, which is just an invisible point with an associated position. For instance in the image below, the cursor has only his position, and will not interact in any way with its environment, even though the kitten formation will follow it:




Implementation is (very) summarized in the following chart:


The Formation class is provided with a FormationPattern. The formation pattern represents the shape of the formation. For instance, in the example above, we were using a V formation pattern.

The Formation class does the magic and outputs a set of slots. These slots must be assigned to the member candidates using a SlotAssignmentStrategy. This assignment strategy will change depending on the role of each slot, but that’s another story.



For a better understanding, look for this subject at the gdxAi wiki. All the tools provided by the framework for formation management can be found here.

3. Conclusion

So what we have so far is:

  • Steering Behaviors using strategies.  wiki
  • Formation motion using slots. wiki

Keep going! In the next post we’ll speak about decision making. ->



Leave a Reply

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

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑