Simulate a flock of birds using the multi-agent system and flutter flames. by Julian Duriebreaux | October, 2022

An in-depth guide to this fascinating project

Unsplash. Photo by Per Thom Mussett

About multi-agent systems

A multi-agent system (or “self-organizing system”) is a computerized system composed of multiple interacting intelligent agents. Multi-agent systems can solve problems that are difficult or impossible for an individual agent or a monolithic system to solve. Intelligence can include methodological, functional, procedural approaches, algorithmic discovery or reinforcement learning. Wikipedia.

As you can see, building a multi-agent system is a clever way to simulate and understand complex systems with very few parameters. Each agent must be locally aware of its environment, autonomous and should have no authority over other agents. You can find such systems in online trading, disaster response, surveillance, structure modeling and in video games like Sim City.


The main purpose of this article is to get acquainted with multi-agent systems and to experiment with flutter flames. To do this, I have decided to simulate a flock of birds. First, it is very satisfying to watch, and second, we can change parameters such as their flight ability or field of view and see how this affects their perceived behavior.

Read more about it in this paper.

tl; Dr

Get the full project on my GitHub. Play with it or watch it live there.

Before playing (a bit) with Flame, we need to define our models. It requires 2D coordinates, speed, bearing, field de view, view distance and constraints to limit its mobility. All of them will be expressed in the International System of Units.

And using those abilities, he should be able to follow three rules:

  • Move towards the center of the herd to avoid predators.
  • Move away from other birds to avoid collision.
  • Fly in the same direction as the closest birds in your field of view.

Finally, to stay close to real-world scale, the simulation will run in fullscreen, and each pixel will be equal to one meter. Additionally, we will consider the environment to be toric, which means that the top left corner will be connected to the bottom right corner.

Usually, I like to make a environment data provided to agents during the simulation. In this case, we don’t need much.

To make things more interesting, we will create two types Agent, represent two different species of birds. Each bird flies in its own way.

During each rendering computation, a bird will execute action imitating its behavior, and updatePositionInToricSpace Will handle the toric constraint of our environment.

is the main method of viewing action , When we get to the actual implementation, this is where the behavior will reside. dt The delta is the time between two actions, expressed in seconds.

our imitation will behave pigeon And sparrow , They share some flying characteristics, but we’ll assume that sparrows are faster and more agile than pigeons.

Whenever a bird tries to exceed its maximum speed, we adjust its velocity vector.

Behavioral methods should only take agents into account fieldOfView And distanceView Of the present You can’t see what’s in the back of your head.

The first rule, average their position, calculate the new vector towards it and apply a coherence factor to control how fast they converge.

The second rule, compute the new avoidance vector, each of them close to the collision threshold and apply the separation factor to handle how fast they change trajectory.

The third rule, to handle mean neighbor velocity and how fast we change speed, is to apply alignment factors.

All this leads to the behavior expressed in action method below. Move towards the center of the flock of your kind, avoid hitting all the birds and fly in the same direction/speed of your kind. the same goes for Sparrow Class.

Finally, we need to make our Component For each type of bird. Those are the squares that Flame will use to render our birds.

action is overridden to update only position provided by vector PositionComponent And dt is simply the delta time between two frames, and we all know that distance = speed * time,

Then we just need to randomly fill our world with birds, and you’re done!

As you can see, multi-agent systems are a powerful tool for understanding complex behaviors with simple rules. You can extend this example by adding a predator, obstacles, feeding locations, and variations on each agent’s attributes.

Your imagination is the limit!

Leave a Comment