I used an earlier, hastier version for the flocks in grow.
For use with browserify:
npm install boids
var boids =raf =var flock =
flock = boids([options])
Moves the boid simulation forward one tick - if you're running an animation, you should be calling this on each frame.
All of your boids are stored as an array of arrays, with each array containing the following variables for a single boid:
xPosition yPosition xSpeed ySpeed xAcceleration yAcceleration
Because the flock is just an array, it should be entirely safe for you to add and remove elements without any unintended side effects, provided all of the arrays are at least 6 elements long and contain numerical values. For example, you can add a new boid moving at a random speed to the origin like so:
You can use attractors to control the flow of the boids - essentially, providing them with goals and obstacles. Each attractor contains:
xPosition yPosition radius force
Note that you can use a negative value for
force to repel boids instead of
attracting them. Again, it should be safe to modify, add and remove these
arrays without any surprises.
benchmark.js yielded the following results in Node:
50 boids: 34013 ticks/sec100 boids: 10000 ticks/sec150 boids: 4537 ticks/sec200 boids: 2583 ticks/sec250 boids: 1653 ticks/sec300 boids: 1159 ticks/sec350 boids: 835 ticks/sec400 boids: 654 ticks/sec450 boids: 518 ticks/sec500 boids: 419 ticks/sec550 boids: 347 ticks/sec600 boids: 292 ticks/sec650 boids: 249 ticks/sec700 boids: 215 ticks/sec750 boids: 187 ticks/sec800 boids: 160 ticks/sec850 boids: 130 ticks/sec900 boids: 119 ticks/sec950 boids: 107 ticks/sec1000 boids: 95 ticks/sec
I'm very much open to pull requests that can help improve performance :)