
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.
Demo video of the simulation in action.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.
With each of these rules individually, the boids will not exhibit flocking behaviour. But when combined, the result looks very realistic (and mesmerising to watch).
The Boids algorithm was originally developed by Craig Reynolds in 1986, for more information see this Wikipedia article.
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.
In addition to the basic Boids algorithm, this simulation also implements a simple obstacle avoidance system using raycasting to detect and avoid obstacles.
I selected libGDX as the framework for this project, as I was familiar with it at the time from a previous project. I have recently made some extensions to the simulation to allow for additional boids to be spawned, and obstacles to be added dynamically to the simulation.
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 force you to adhere to a particular structure, but it also leaves it up to you to build your own structure. 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
In an attempt to improve performance of this boid implementation, we make use of spatial partitioning to prevent us from looping over every other boid for each frame, this implementation uses the underlying Box2D physics engine 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 integrated:
- Force Configuration
- Configure the force scalar for each of the three boid rules.
- 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 dynamically.
- Obstacle Spawner
- Create and remove obstacles from the simulation dynamically.
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 performance could be improved, 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 other engines such as Unity, Unreal, or Godot. It's also comparatively very simple and doesn't force a particular structure on you which is nice, but I may look at these other engines in a future project as I'd like to see how they compare.
91b042f at 2nd Aug 2026 16:08:22