boids algorithm

Artificial Life is a field of study and an associated art form which examine systems related to life, its processes, and its evolution, through the use of simulations with computer models, robotics, and biochemistry.

Mechanical Life is the term I have always preferred.  Artificial Life sounds so pejorative.  What is life and what qualifies it real versus artificial?  There's a lot of great discussion to be had here, it's one of the reasons why I wanted to create this website.  Regardless, this has to be the most compelling art form I've ever encountered.  

The boids algorithm is a classic example.  Here simple rules interact and result in emergent behavior that defies all expectation.  Velotron has been tinkering with this for years, so I decided to implement my own version of the boids algorithm in Unity.  Here it is running in Unity WebGL.  Click the link to launch in your browser...



Boids follow three simple rules:

  • Separation - steer to avoid crowding local flockmates
  • Alignment - steer toward the average heading of local flockmates
  • Cohesion - steer to move toward the average position (center of mass) of local flockmates

The original boids algorithm was presented at SIGGRAPH 1987 by Craig Reynolds, a pioneer of early 3D computer graphics who did work on the original Tron movie - the most legit OG Tech Artist cred I can imagine.  This line from Wikipedia about the name cracks me up, so I'll quote it directly...

The name “boid” corresponds to a shortened version of “bird-oid object”, which refers to a bird-like object. Its pronunciation evokes that of “bird” in a stereotypical New York accent.

lol!! Craig Reynolds' original boids simulation used simple triangular shapes without feathers and wings so he felt it necessary to call that out in the name.  Now every time I talk about it people must think I'm trying to sound like I'm from the east cost.

The code is really straightforward.  There is a function for each of the three rules.  The returned vectors get normalized and weighted based on the UI sliders.  That results in the new heading and the object is then moved along its forward vector taking momentum into account.  All together less than 150 lines of code.  It's not optimized at all, but I don't care right now because I'm too busy playing around with the sliders.  Someday I will do an optimization pass and see how many boids I can get on screen at once.  I've seen versions of the boids algorithm that run 4k instances at 60 hertz... in a web browser.  Welcome to the knee of the curve.

Here it is in Unity C# for anyone who wants to try it out.  Drop it in your scripts directory and assign it to any objects in the scene.


As soon as I got it working I got sucked in and couldn't stop playing around with it.  I have spent hours on end fiddling with the sliders that change the weight values of separation, alignment and cohesion.  It is so much fun!!  Although, it has had a negative effect on my productivity.  Every time I do this I discover new flock behaviors that surprise me.  At first it was fun to attempt to replicate real birds.  But soon I found it was more interesting to explore the abstract emergent behaviors inherent to the unique details of my own implementation.  


boid flocking patterns

A couple of patterns jumped out at me right away.  They make interesting shapes in phase space.  Enter the values in the demo to see it live!


realistic bird flock

realistic bird flock -  At times this can look somewhat close to real bird flocking.  Starting from these settings, shift cohesion and separation up and down to keep them looking lively.  I wanted to get the literal stuff out of the way so we can get on to the fun abstract emergent territory.  


mega-boid - High cohesion values with no alignment or separation.  All the boids clump together into one.  Make the clump fatter by slowly increasing separation.


explosion - Starting from a mega-boid pattern quickly increase separation.  They explode outward spherically.  


halo - Zero separation and alignment, low values of cohesion.  The boids orbit around each other in a pattern that resembles a divine headdress, or a giant sci-fi space installation.  Diameter of the halo is inversely proportional to cohesion.

boid planetoid

boid planetoid - Balanced separation and cohesion with low alignment stabilizes into a spherical shape. 

black hole

black hole - Starting from a planetoid pattern quickly increase cohesion.  They all collapse into one point resembling a gravitational singularity.  The event horizon is clearly visible when viewed in phase-space.

synchronized dancing

synchronized dancing - When alignment is set really high the boids all try to aim in the same direction.  Low momentum causes them to immediately flip to the new orientation.  Large sections of the flock keep flipping back and forth 180 degrees as the whole group eventually converges on the same heading.  Looks like they are in a music video flash mob or something.


v-formation - Back to realistic again... this one freaked me out when I discovered it.  I was like, holy shit, they are migrating South for the winter!!  Starting with a mega-boid slowly increase alignment.  It suddenly takes on a life of its own.  Good luck keeping them on camera as they swoop around looking for warmer climates.


I had so much fun painting in phase space with the boids.  Shapes came up that I couldn't have predicted.  It got really crazy when I applied the boid component to the camera itself.  I can't help but see what looks like landscape imagery in these screenshots.  Created totally by accident.


These swooping shapes are fun to look at too.  The camera is stationary here.  This is what it looks like when I apply the boids component to my affine transformation fractals.


Here is a gallery of some more fun boids images I grabbed while experimenting in Unity...

I raised a big question in the intro paragraph.  What is life and what qualifies it as real versus artificial? I don't intend to answer it, but rather to explore it and raise even more questions for further discussion.  Obviously, these boids don't have much in the way of real intelligence, and any comparison to biological life doesn't stack up.  But if a machine passes the Turing Test well enough, who is to say for sure?