https://youtu.be/e7WxNZbxggs - Black and white
https://youtu.be/SjnZw9wFwO8 - Colorful
A real-time simulation of flocking behavior, implementing Craig Reynolds' classic "Boids" algorithm in C++ with the SFML library. This project explores how complex, life-like group motion can emerge from a few simple, local rules.
This project delves into the fascinating world of emergent behavior and agent-based systems. The goal was to simulate a flock of birds (or a school of fish) where each "boid" operates independently, making decisions based only on its immediate surroundings. There is no central controller; the intricate and coordinated flocking motion is an emergent property of the system itself. This is a foundational concept in artificial life, animation, and AI for games.
The behavior of each boid is governed by three simple steering rules:
- Separation: Steer to avoid crowding local flockmates. Each boid calculates a repulsion force away from nearby boids to prevent collisions.
- Alignment: Steer towards the average heading of local flockmates. This causes boids to travel in the same general direction.
- Cohesion: Steer to move toward the average position (center of mass) of local flockmates. This rule keeps the flock together.
These three forces are calculated and combined each frame to determine the acceleration for each boid, resulting in fluid, natural-looking motion.
This project was a profound exercise in understanding agent-based AI and the challenges of real-time simulation.
-
Emergent Complexity: My most significant takeaway was witnessing complex, organized, global behavior emerge from simple, local interactions. There's no "leader" boid, yet the flock moves as a cohesive, intelligent unit. This provided an incredibly intuitive understanding of decentralized systems.
-
Vector Mathematics in Action: This project was a practical masterclass in vector math. All rules—calculating distance, steering forces, normalizing vectors, and applying acceleration—are fundamentally vector operations. It solidified my ability to use vectors to solve physical simulation problems.
-
Performance Considerations: The current implementation uses a brute-force approach to find neighbors for each boid (comparing every boid to every other boid), which has a time complexity of O(n²). While this works for a small number of boids, I learned that a key area for future optimization would be implementing a spatial partitioning structure like a Quadtree or a spatial hash grid. This would drastically reduce the number of checks needed per boid, allowing the simulation to scale to thousands of agents efficiently.
This project was developed on Windows using Visual Studio.
- Clone the Repository:
git clone https://github.com/tuananohut/Flocking-Simulation.git
- Run from Visual Studio:
- Open the
.slnsolution file in Visual Studio. - Set the solution configuration to Release and platform to x64.
- Build and run the project (F5).
- Open the
The algorithm and concepts for this project were heavily influenced by the pioneering work of Craig Reynolds and the excellent resources below:
- Craig Reynolds' Boids Page: red3d.com/cwr/boids/
- The Coding Train Tutorial: Coding Challenge #124: Flocking Simulation
- Additional Reading: Eater.net Boids & Wikipedia
