A basic simulation of boids in the C programming language using raylib for rendering.
Put simply, boids are flocking simulations. They emulate the movement of fish shoals or bird flocks which are all forms of swarm intelligence. Each object (hereby called a 'boid') has a set of rules (cohesion, separation and alignment) which can not been seen individually. However when in a group of other boids a flock beings to appear; this is a demonstration of emergence.
You may be currious why I chose the C programming language, instead of a more recent and safer language like Rust or Python. This is because I see C as an industry standard despite all of the flaws it may have and I think it's accessible to many programmers of all backgrounds.
In-depth explanations of boid implementations specifically in C.
This section will be dealing with the functions defined in src/boids.h and/or implemented in src/boids.c.
Function | Explantion |
---|---|
newBoid |
Takes the Boid construction parameters and applies the rotational transformation to the positions as specified by the rotation parameter around the origin parameter. |
updateBoid |
The most complex function of all of them. Increments the origin per the boid velocity and the change in time (seconds). Aggregates pointers to local boids and passes these to cohesion, separation and alignment for that boid. Then rotate the boid based on the decided rotation and update it lastUpdate . |
rotateBoid |
Applies a rotation of theta radians to the boid. It does this through rotational matrices. |
drawBoid |
A function specifically designed to link the goings on of the boids and render them to the screen using raylib. |
distance |
A basic Pythagorean theorem function for two vectors calculating the distance between two points. |
getLocalFlock |
Gets all the boids within a 50px radius of the boid. |
getRotation |
Gets the bearing of two points relative to v1 regardless of quadrant using atan2. |
getCohesion |
Mean all the origins of boids within the localFlock parameter |
getAlignment |
Get the mean alignment of all of the boids within localFlock |
getSeparation |
Gets the "inverse" rotation (opposite direction) of the nearest boid. |