June 16, 2025
instead of resetting the grid every frame, I just update the changes to the grid when a boid moves between cells. sim time halved again!! I think I might be reaching the limits of python without stuff like vectorization that would destroy my ability to add new features, so I'll instead focus on new features and maybe switch to something like c++ later to continue optimizing
https://github.com/D-Colak-PC/boids/commit/59e92ea0fb1aeca655437ac676bb0243fc8a883d
tried storing distance and passing as a parameter, was slower
tried using only squared distances to save sqrt, much faster
implemented spatial grid optimization where screen is divided into chunks and boids only check their immediate chunk neighbors - halved simulation time in 2!!
https://github.com/D-Colak-PC/boids/commit/761004afc62b2b346dd09d76a4c138d0b10c09eb
added performance monitoring so I can see if later optimizations actually do anything
https://github.com/D-Colak-PC/boids/commit/960954e45ee2467796893cb9fd83aabefe5cd45d
https://github.com/D-Colak-PC/boids/commit/5f8efdff65fb44289af97f5b7b42e20d7ef77b5b
I did 2 things:
1. refractor literally the whole codebase. I wanted to make it super modular and I learned about SOLID principles so I tried to apply them here. The reason for this is to both learn best practices and make my life simpler for optimizations down the line
2. Added to the readme a very very comprehensive breakdown of the maths involved and how all the formulas are derived. This helps people seeing the project understand it.
Things I want to do:
- compare boids at precompute using a graph where boids are verticies and neighborability are edges. divides precompute time by 2.
- store distance along with boid in neighbors. distance is recalculated so many times that its worth the memory to store it once.
- spatial optimization. Right now every boid checks every other boid, even on the other side of the screen. Implement some sort of grid, but I want to later improve to like a quadtree or hash grid thingy whatever it is.
- add a ui for turning on and off feature flags and changing weights during runtime.
https://github.com/D-Colak-PC/boids/commit/a0f6f73feeac4c4078fea394c3c7d2f119cecae2
Found the original paper for boids by Craig Reynolds, which I'll implement
https://www.red3d.com/cwr/papers/1987/SIGGRAPH87.pdf
interesting how most of my equations that I'm pulling are from particle physics! Lots of math derivation and linear algebra to get working code for cohesion, seperation, and alignment forces. Added drag because the system looked very strange with no energy loss. I got rid of the svg (weird aliasing issues) and instead used points with rotation matricies to rotate to angles.
I think it looks great, but currently it just eventually evolves into an even grid of boids all facing the same direction and moving together. I spent some time tuning the forces to play nice with eachother, but it still goes back to that annoying state. I'll probably implement some sort of brownian motion or random noise to keep the birds moving interestingly. I also want to limit the angular acceleration of the birds cuz sometimes when velocities are low the just flip 180 degrees instantly lol
Today I had to learn pygame to get the game running!! I spent some time in Adobe Illustrator making a design for the boid as a SVG, then created a simple setup to load it as a BMP scaled to whatever I want it. I made the boids show up on the screen randomly!! I was working around with a lot of the math and it looks like I'll need 3 vectors for position, vel, and acceleration... ugh i don't even know any linear algebra hope this won't bite me later down.
This is a boids simulation in pygame. It follows 3 simple rules of cohesion, alignment, and seperation to form emergent behaviour and complex mechanics over time! I have used this project to learn many optimizations, spatial and computational. Feel free to look at the code!!
This was widely regarded as a great move by everyone.