
Boids Simulation
Implementing a Boids simulation to model flocking behavior using LibGDX.Demo
A video demonstration of the simulation in action can be seen below. To try it out yourself, click here.
Video of the simulation in action.Project Goals
In this project, I produced a Boids simulation using the libGDX framework. This project was inspired by a similar project developed by Sebastian Lague, which can be found on YouTube.
What are Boids?
Boids are an algorithmic approach to simulation of flocking behaviour such as that found in flocks of birds or schools of fish.
They achieve this by following a series of simple rules which are defined in the report above and are as follows:
- Collision Avoidance
- Avoid collisions with nearby flock mates.
- Velocity Matching
- Attempt to match velocity with nearby flock mates.
- Flock Centering
- Attempt to stay close to nearby flock mates.
These are in order of decreasing precedence, meaning that Collision Avoidance is the most important, and Flock Centering is the least important.
The Boids algorithm was originally developed by Craig Reynolds in 1986, for more information see this Wikipedia article.
In addition to the basic Boids algorithm, this simulation also implements a simple obstacle avoidance system using raycasting to detect and avoid obstacles.
libGDX
libGDX is a cross-platform Java game development framework that works cross platform on Windows, Linux, macOS, Android, iOS, and browsers. Unlike game engines such as Unity, libGDX is only a framework. It doesn't enforce much in the way of structure, instead expecting you to build out your solution as you see fit. It handles rendering, input, audio, and includes Box2D for physics.
I used libGDX on a previous group project at university and was familiar with it at the time I made this project, which made it the obvious choice.
Spatial partitioning
To improve performance of this boid implementation, we make use of spatial partitioning. This prevents us from having to loop over every other boid, per boid, for each frame. Instead, each boid may simply loop over it's list of neighbours for force calculations.
This implementation uses the underlying Box2D physics engine with sensor fixtures so that boids can only "perceive" other boids that are within a certain radius of them.
Configuration
The simulation can be dynamically configured using a series of tools built into the simulation UI, the following tools are available:
- Force Configuration
- Configure the scalar for each of the three boid rules/forces.
- Debug Overlay
- Includes overlays to view the underlying physics engine, and forces acting on each boid.
- Boid Spawner
- Create and remove boids from the simulation.
- Obstacle Spawner
- Create and remove obstacles from the simulation.
Results
The simulation works well for a few hundred boids, but performance with large number of boids could be improved. There are a couple of ways to improve performance. We could offload the boid calculations to separate threads, or even create a GPU compute shader to handle the calculations for us (which would be significantly faster).
LibGDX is a great framework, but it's not as widely used as more popular game engines. In a future project, I would like to explore using other engines such as Unity, Unreal, or Godot to see what I'm missing.
3bb7c66 at 4th Aug 2026 22:28:39