top of page

BOIDS (FLOCKING) SYSTEM:

A PHYSICALLY BASED ANIMATION PROJECT

 //--------------------------------------
 pba::Vector calcTotalAccel(pba::DynamicalState &state, int boidID)
    {
      //Vectors initialized as (0,0,0).
        pba::Vector d, totalAccel, avoidAccel, velAccel, centerAccel;
        int count, other;
        double rangeWeight, visibility, residual;     //Used in keeping total accel under max accel.

        count = state->nb();
        for(other=0; other<count; other++)
        {
            if (other != boidID)
            {
                d = state->pos(other) - state->pos(boidID);
                rangeWeight = calcRangeWeight( d.magnitude() );
                visibility = calcVisibilityWeight( d, state->vel(boidID) );

                avoidAccel += calcCollisionAvoidance(state,
                            boidID, other, rangeWeight, visibility);
                velAccel += calcVelocityMatching(state,
                            boidID, other, rangeWeight, visibility);
                centerAccel += calcCentering(state,
                            boidID, other, rangeWeight, visibility);
            }
        }


      //Modifier to boids: Limit the acceleration.
        if ( avoidAccel.magnitude() > maxAccel )
        {
            avoidAccel = avoidAccel * (maxAccel / avoidAccel.magnitude());    //Scale down magnitude to maxAccel.
            velAccel.set(0,0,0);         //Already maxed out the total accel, so set these to 0 vectors.
            centerAccel.set(0,0,0);
        }
        else
        {
            residual = maxAccel - avoidAccel.magnitude();   //New threshold for the other accels to stay below.
            if ( velAccel.magnitude() > residual )
            {
                velAccel = velAccel * (residual / velAccel.magnitude());
                centerAccel.set(0,0,0);
            }
            else
            {
                residual = residual - velAccel.magnitude();
                if ( centerAccel.magnitude() > residual )
                {
                    centerAccel = centerAccel * (residual / centerAccel.magnitude());
                }
            }
        }

        totalAccel = avoidAccel + velAccel + centerAccel;
        return totalAccel;
    }



 

Language:  C++ (using OpenGL)

Date:  Fall 2017

Class:  Physically Based Animation (Graduate)

​

The flocking of the particles implemented are the basic "boids" (bird-oid objects, proposed by Craig Reynolds) rules: collision avoidance, velocity matching, and centering (also often called separation, alignment, and cohesion).  These properties are affected by range (distance between a pair of boids) and visibility (applying a "field of view" to a boid in deciding whether it "sees" another boid or not).  Some of the parameters that the user of this simulation can alter are the strengths of collision avoidance, velocity matching, and centering, the range and field-of-view, and a maximum acceleration threshold.  The code sample provided calculates the total acceleration on a single boid based on the factors mentioned and taking into account all of the other boids.  This acceleration is then used in a velocity partial solver when taking into account total forces on a particle.

bottom of page