Boids

This is my implementation of the famous "Boids" flocking algorithm, which was developed by Craig Reynolds in 1986 (link to Craig's website).

The boids--which happen to be fish in this implementation--follow 3 relatively simple rules: 

  • Separation (avoid collisions)
  • Alignment (match velocity of local boids)
  • Cohesion (move towards the center of local flock

Yet out of these simple rules, complex and emergent behavior can develop, and the boids tend to form flocks and fly/swim in dynamic ways.

-----------------------

Sit back and relax, watching the fish/boids swim around and form flocks or break off and go solo.  Or change the settings using the sliders to see how it affects their behavior.

Controls:  ESC to bring up the respawn menu if needed.

-----------------------

Note: this version implements boids on an individual basis, i.e. each boid is a separate object/class running the functions of separation, alignment, and cohesion independently.  While this is a very clear and understandable way to implement the algorithm, it is also less efficient as each object needs to run separate updates, and data is spread out over memory which creates additional overhead.

For future versions, I plan to implement 3 other simulation methods, each increasingly more efficient, which will clearly show the performance differences.

Additional simulation methods to be implemented in the future:

  • Boid Manager (fast) - A centralized manager will control all data related to the boids in one class, storing data in contiguous arrays, using only one update method which should greatly improve efficiency.
  • Boid Manager using C# Job System & Burst Compiler (faster) - In addition to using  a centralized boid manager, this method will use the C# Job system along with Unity's burst compiler to drastically improve performance of expensive computations like finding nearest neighbors.
  • Boid Manager using Unity's Entity Component System & C# Job System (fastest) - this method will be implemented using Unity's Entity Component System (ECS), which is a data-oriented paradigm and completely different from the object oriented paradigm used in previous methods.  It will also utilize a manager as well as the C# Job System and Burst compiler, which should lead to the best performance of all methods.

Download

Download
Boids 1.0 (Mac).zip 81 MB
Download
Boids 1.0 (Win).zip 72 MB

Comments

Log in with itch.io to leave a comment.

(2 edits)

i love these types of algos, they always surprise me. in particular i was surprised to see how weird the directionality was with full cohesion, full perception range, and zero separation, zero alignment, i fully expected the fish to all just consolidate immediately in the center but they continued to bounce around for quite a while, and kept “momentum” even after all the fish were fully consolidated. groups of fish even broke off from their initial flocks! (i’m guessing that was the result of their “bouncing” behavior off of the bounds of the space.)

thanks for making this, it was interesting!

(+1)

Excellent guess!  When a boid hits the boundary of the sphere, it picks a new target position on the opposite side of the sphere and rotates towards it.  This leads to some boids turning left and others turning right and they have the potential to break off from the group.  But if the bounds are turned off, then using the settings you had, they would just swim in a tight group forever.  The bounds keep things interesting :)

Thanks for checking it out, glad you enjoyed it!