Skip to content

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.

Notifications You must be signed in to change notification settings

tuananohut/Flocking-Simulation

Repository files navigation

Flocking Simulation: AI & Emergent Behavior

Flocking Simulation 26 11 2023 12_31_07

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.


Project Overview

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.


Key Features & Implemented Concepts

The behavior of each boid is governed by three simple steering rules:

  1. Separation: Steer to avoid crowding local flockmates. Each boid calculates a repulsion force away from nearby boids to prevent collisions.
  2. Alignment: Steer towards the average heading of local flockmates. This causes boids to travel in the same general direction.
  3. 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.


Technical Deep Dive & What I Learned

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.


How to Build & Run

This project was developed on Windows using Visual Studio.

  1. Clone the Repository:
    git clone https://github.com/tuananohut/Flocking-Simulation.git
  2. Run from Visual Studio:
    • Open the .sln solution file in Visual Studio.
    • Set the solution configuration to Release and platform to x64.
    • Build and run the project (F5).

Acknowledgements

The algorithm and concepts for this project were heavily influenced by the pioneering work of Craig Reynolds and the excellent resources below:

About

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.

Topics

Resources

Stars

Watchers

Forks

Languages