Boids: An Implementation of Craig W. Reynolds' Flocking Model

Written by: Peter Keller (psilord at cs.wisc.edu)

Home Page Blog Home Lisp Software, Tutorials, Hacks

Description

This is a naive implementation of some of the ideas created by Craig W. Reynolds in his boids paper. I implemented cohesion, alignment, and separation in addition to point collision avoidance with randomly placed and static points.

The simulation can run in 2D mode (with the Z coordinate range of the universe object set to a min of 0.0 and a max of 0.0 when it is constructed in MAIN). It also has a 3D mode where the universe rotates slowly and the flock can be examined as each boid flies in 3D space.

There are some interesting bits in corrections.lisp where I use multiple dispatch in CLOS in order to implement different policies of how to implement cohesion, alignment, and separation in relations to boids and the scenery they are supposed to avoid. The main thing to realize is that there is only ONE n^2 loop and each kind of correction is accumulated in parallel with the others. It took a slightly surprising number of tries to get especially the neighborhood method right such that the dispatch did what I expected. Since you can have things like a boid conpared to a scenery object, and then later vice versa with the same objects, I had to think carefully about how often the forces were actually applied. In this code, a boid gets a force from a scenery object, but a scenery object doesn't get a force from a boid.

Here is an example of the 2D visualization of the boids. I like this visualization because one can clearly see the effect of avoiding the red dots.

Here is the 3D visualization. It is fun to watch, but unless I add stuff to make the spatial understanding clearer, a screen shot of it isn't that interesting.

Supported Platforms

Currently Boids operates with SBCL on Linux and uses these packages from quicklisp: :sb-cga :cl-opengl :cl-glu :lispbuilder-sdl. Patches will be accepted for other lisp implementations and environments. However, I only have resources to test on SBCL on Linux.

Download

This is the live source. However, I don't plan on working on it much in the future unless something sparks my interest. The HEAD is the development version. All official release tags (if any) are present.

After loading the :boids system and changing to that package, call (MAIN) to run it.

git clone http://pages.cs.wisc.edu/~psilord/lisp-public/public-repos-lisp/boids.git

May this software serve your needs.