Real-time 2D flocking simulation implementing Craig Reynolds' Boids algorithm with spatial grid optimization. Built with C++17, OpenGL 4.6, GLFW, GLM, and ImGui.
Boids.mov
Key Features:
- Spatial grid partitioning for O(n) neighbor queries
- GPU instanced rendering for thousands of boids
- Real-time parameter tuning via ImGui
- Mouse interaction (attract/repel)
- Dynamic coloring modes (speed-based & friend visualization)
- Configurable edge behaviors (bounce/wrap)
![]() |
![]() |
|---|---|
| Velocity-based color mapping | High-density flocking behavior |
Represents individual boid agents with position, velocity, and behavior rules. Implements Reynolds' three core behaviors:
- Separation: Avoid crowding neighbors
- Alignment: Steer toward average heading of neighbors
- Cohesion: Move toward average position of neighbors
Implements spatial hash grid for efficient neighbor finding. Divides 2D space into uniform cells, reducing collision checks from O(n^2) to O(n).
Core simulation manager that:
- Initializes boid population with random positions/velocities
- Builds spatial grid each frame
- Computes neighbor relationships using optimized pair checking
- Updates all boid behaviors with configurable weights
- Handles mouse interaction (attraction/repulsion)
- Manages edge behavior (bounce/wrap)
OpenGL 4.6 instanced rendering pipeline:
- Vertex shader: Transforms boid triangles via model-view-projection matrices
- Fragment shader: Applies per-boid coloring (velocity-based or friend-based)
- Instancing: Renders all boids in a single draw call using instance matrices
- ImGui overlay: Real-time parameter control and statistics display
- CMake 3.16+
- C++17 compiler (MSVC/GCC/Clang)
- OpenGL 4.6 capable GPU
- vcpkg (for dependency management)
vcpkg install glfw3:x64-windows glm:x64-windows glad:x64-windows imgui[opengl3-binding,glfw-binding]:x64-windows- Open project folder in Visual Studio
- CMake configuration auto-detects
- Build and run the
Boidstarget on release configuration
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=%VCPKG_ROOT%/scripts/buildsystems/vcpkg.cmake
cmake --build build --config Release
./build/Release/Boids.exe| Action | Effect |
|---|---|
| Left Click | Attract boids to cursor |
| Right Click | Repel boids from cursor |
| Middle Click | Spawn boids |
Real-time adjustable parameters:
- Behavior weights: Alignment, Cohesion, Separation
- Speed limits: Min/Max velocity
- Edge behavior: Bounce vs wrap-around
- Visual modes: Friend visualization, speed coloring
- Spawn controls: Add boids at runtime
const int N = 3000; // Starting population
GLuint SCR_WIDTH = 1400; // Screen resolution
GLuint SCR_HEIGHT = 900;
float scale = 1.0f; // Scale of boid instances- Spatial Grid Partitioning: Reduces neighbor search from O(n^2) to O(n)
- Pair Deduplication: Checks pairs where idx < idy, for n(n-1)/2 complexity
- GPU Instancing: Single draw call for all boids
- Reference Passing: Avoids unnecessary boid copies in hot loops
- Cell size must be ≥
fovRadiusor neighbor detection fails - Very high boid counts (10k+) may cause frame drops during grid rebuild
- Friend visualization mode has performance cost (full N^2 check for one boid)
- Attracting boids by LPM might cause frame drops due to a lot of objects in neighbor cells

