Boids -- The Library and Interface


Getting the Files

Follow these steps:

  1. Change into the directory where your project is.
  2. Make a directory called "demo" (% mkdir demo).
  3. Issue the following commands:
      % cd demo
      % cp ~/../spowers/public/Makefile .
      % cp ~/../spowers/public/demo-boids.c .
      % cp ~/../spowers/public/rgbimage.* .
      % cp ~/../spowers/public/brick-boids.rgb .
    
  4. To build the demo, just type "make". You may also need to "touch depend.make".
  5. To run the demo, type "demo-boids". To see the cubes textured, type "demo-boids brick-boids.rgb".

Now, you will want to use the Makefile...so copy it back one directory (% cp Makefile ..). You will then need to edit the Makefile and change the name of the program and the name of your source file. After these changes, you should be able to compile again without problems.


Interfacing with the Library:

To interface with the boids library, you will need to be aware of how it works. First off, you must create the boid simulation. This defines things like the world dimensions, the dimension of each boid, and the number of boids to create. Everything else is taken care of, with regard to creating the simulation. Next you will need to define your obstacles. You can do this in your function for creating the landscape. Obstacles can be either spheres or rectangles. This does not mean the obstacle that is rendered (the one you draw on the screen) needs to be a rectangle or sphere, as you will never see the obstacle as the boid does internally. Finally, you will need to call into the boid library to find out the new position of the boid. This will be done in your display function.

Here are the steps as done in the demo-boids.c program:

  1. You need to include the header file "boiding.h". You can view this file in my public/include directory if you want. It defines some functions you can call and the array of boids.
  2. Now you need to define how many boids you want to have in your system. This can range anywhere from 20 and up. I would suggest no more than 40 on these computers.
  3. In your init function, add the following:
        createBoidSim(NUMBOIDS,
                      10,
                      10,
                      10,
                      500,
                      500,
                      700);
    
    Here is the prototype as specified in the header file:
    int createBoidSim(int numBoids,     /* number of boids */
                      float bwidth,     /* boid width */
                      float bheight,    /* boid height */
                      float blength,    /* boid length */
                      float wwidth,     /* world width */
                      float wheight,    /* world height */
                      float wlength);   /* world length */
    
    So, you will need to specify your own parameters. If your boid dimensions are different, you will need to be sure to make them the correct size. Please note this is the "scaled" size if you are scaling your boid down.
  4. Next, you'll need to take care of your obstacles. There are no obstacles in the demo-boids program. When you create each of your obstacles, you simply define them for the boids simulation as well. Here are the prototypes:
    int createBoxObstacle(float rulx, float ruly, float rulz,   /* rear up left */
                          float flrx, float flry, float flrz);  /* frnt low right */
    int createSphereObstacle(float cx, float cy, float cz,  /* center */
                             float radius);                 /* radius */
    
    The box obstacle takes 2 points, the upper left rear corner and the lower right front corner. The sphere takes the center point and the radius of the sphere. If you need further explanation, ask.
  5. Now you are ready for the display function mods. First, at the very top of your display function (meaning, the very first thing), you need to add:
        if (!updateBoids()) {
            return;
        }
    
    This is so the boids update much smoother and faster. Next, you'll need to draw the boids. This example calls "draw_borg" which draws one cube with or without texture mapping.
        for (i=0; i<NUMBOIDS; i++) {
            glPushMatrix();
            glTranslatef(boidList[i].x,
                         boidList[i].y,
                         boidList[i].z);
            glRotatef(boidList[i].pitch,1,0,0);
            glRotatef(boidList[i].yaw,0,1,0);
            glRotatef(boidList[i].roll,0,0,1);
            draw_borg();
            glPopMatrix();
        }
    
    That's all there is to it.