I’ve been intrigued by flocking, schooling, and herding algorithms since running across the Boids web site.

Link to the Boids page.

The emergent “behavior” of flocking actually comes from three basic rules.

  • Grouping – tell the Boids to group together.
  • Alignment – tell the Boids to move in the same general direction.
  • Avoidance – tell the Boids to avoid one another to avoid over crowding.

So when I was working in JavaScript for work I thought a fun exercise would be to implement the Boids in JavaScript. Unfortunately, it turns out that has already been done.  See the following link.

Link to the JavaScript Boids page.

So my next thought was to refactor the JavaScript Boids code to my own liking and then add an additional behavior, evading a predator. My inspiration for this came while watching a nature program where a school of fish would avoid a shark as the shark swam through the school of fish.
So the predator and prey share the same basic move behavior which also includes a bit of code that “compels” them to stay in the bounds of their “world”. In Object Oriented Programming they both extend from the same base object. I then added the chase “behavior” to the predator and now I have a JavaScript based model for a predator perpetually chasing its prey. You can see the page via the following link.

Link to my predator and prey boids page.

It’s a little mesmerizing to watch for an extended period of time. On occasion, the prey will seem to not avoid the predator as much as they do other times. I’m not entirely sure why that is. But the interesting thing is that all of this behavior is provided by only a few basic rules. You can read more about Boids via the following link.

Link to the Boids wikipedia page.

Leave a Reply