Implementing Flocking

flock0

Step 1: Create a class to represent the boids and their motion.

This class has position, velocity, & acceleration vectors, and updates the position and velocity on each frame.

The boid class is primarily going to serve as the intelligence of the creatures. We need to have some way to use the data from this in our 3D scene. I've chosen to make the boid class a type of dms::Object, so that it can be inserted into a libdms scene graph. The class will create & update a transformation, but will have no geometry - the geometry is taken care of by attaching some other object to the boid. This way the intelligence is independent of what the creatures look like.

flock0.cpp
boid0.h
boid0.cpp


flock1

Step 2: Create a "boid species" class, to store general data about different types of boids, and keep track of all the boids of each type.

The boidSpecies class provides a function to find all the neighbors of a particular boid. It uses the STL vector template for lists of boids.

This program shows the neighbors of one particular boid, by making the neighbor boids larger.

flock1.cpp
boid1.h
boid1.cpp
boidSpecies1.h
boidSpecies1.cpp


flock2

Step 3: Apply the flocking rules, as a set of functions in the boid class (called from update()).

One important issue is how to combine the instructions from the different rules. This implementation uses a very simple prioritization scheme. Whichever rule says it has the highest priority is followed.

flock2.cpp
boid2.h
boid2.cpp
boidSpecies2.h
boidSpecies2.cpp




next