Home▸Articles▸Algorithms & AI

2D Boids: Flocking Behavior in Algorithms

A fascinating exploration of emergent behavior through simple rules.

mysimulator teamUpdated June 2026≈ 4 min read▶ Open the simulation

What 2D Boids Are

The term 'Boids' refers to a model of individual entities (or agents) that exhibit flocking behavior. This phenomenon is observed when a group of animals such as birds or fish move together in coordinated patterns, like flocks flying or schools swimming. In the context of 2D Boids, these agents follow simple rules: separation, alignment, and cohesion, which collectively result in complex emergent behaviors.

The model was developed by Craig Reynolds in 1986 to simulate natural flocking behavior using a set of three steering forces that each agent applies to itself. These forces are computed based on the relative positions and velocities of nearby agents.

Why It Happens

The key to understanding 2D Boids lies in how these simple rules interact with one another. Each agent is influenced by its neighbors, adjusting its movement based on their positions and velocities. Separation ensures that agents do not collide; alignment makes them move in the same direction as nearby agents; cohesion pulls them towards the center of mass of the group.

These interactions are governed by mathematical functions that calculate the forces acting on each agent. For example, separation might involve a repulsive force between agents when they get too close, while alignment and cohesion use attractive forces to align velocities or pull closer together.

live demo · related simulation● LIVE

Real-World Applications

The principles behind 2D Boids have been applied in various fields beyond just simulations. In video game design, they are used to create realistic movement for crowds and creatures. In robotics, similar algorithms can be used to coordinate the movements of multiple robots or drones.

Additionally, these models help in understanding and predicting animal behavior in nature, which is crucial for conservation efforts and wildlife management.

Control Summary

The controls allow users to manipulate key parameters that influence the flocking behavior. The 'Count' slider adjusts the number of agents in the simulation, while 'Max speed' and 'Perception radius' control how fast they move and how far they can see their neighbors.

Separation, Alignment, and Cohesion sliders let you fine-tune the balance between these forces, and the predator feature adds an external influence that can be used to study avoidance behaviors.

Frequently asked questions

How do the three rules (separation, alignment, cohesion) work together?

The three rules work in concert: separation prevents agents from colliding by pushing them apart; alignment makes them move in a similar direction as their neighbors; and cohesion pulls them towards the center of mass of the group, ensuring they stay close.

Why is the spatial hash neighbor search important?

A spatial hash neighbor search efficiently finds nearby agents without checking every possible pair, making the simulation more computationally efficient and scalable for larger numbers of agents.

Can 2D Boids be used to model real-world animal behavior?

Yes, while simplified, 2D Boids can provide insights into collective behaviors observed in nature. Researchers use these models to study flocking and schooling patterns in animals like birds or fish.

What is the role of the predator in the simulation?

The predator introduces an external force that agents must avoid, allowing for the study of avoidance behaviors and how flocks react to threats in their environment.

Try it live

Everything above runs in your browser — open 2D Boids — Flocking on a Flat Canvas (2D) and change the parameters while it is running. Nothing is installed, nothing is uploaded, the whole model lives in one tab.

▶ Open 2D Boids — Flocking on a Flat Canvas (2D) simulation

What did you find?

Add reproduction steps (optional)