【正文】
t fly// infinitely far away from the camera, instead remaining // in a nice viewable area. It does this by wrapping flock// boids around to the other side of the world, so (for example)// they move out the right and return on the left.// Returns: nothing.void CBoid::WorldBound (void){ float maxX = CBox::WorldPtrGetBoxWidth()/2。endif // return change vector return (change)。t turn on a dime.. (MinUrgency)。endif // now that we have the perceived center, puter a vector towards it change = center m_pos。endif // average the positions to get the perceived center of the flock center /= m_num_flockmates_seen。 }ifdef BOID_DEBUG myprintf( perceived center before averaging = %f %f %f\n, , )。 i m_num_flockmates_seen。ifdef BOID_DEBUG myprintf(\nInside SteerToCenter\n)。endif return (m_num_flockmates_seen)。 myprintf( total flockmates seen = %d\n,m_num_flockmates_seen)。 } } // next flockmate flockmate = flockmateGetNext()。 // Test: If this guy is closer than the current // closest, make him the current closest if (dist m_dist_to_nearest_flockmate) { m_dist_to_nearest_flockmate = dist。 // now figure out who we can see while (flockmate != NULL) { // Test: Within sight of this boid?ifdef VISIBILITY_DEBUG myprintf( looking at %x\n,flockmate)。endififdef VISIBILITY_DEBUG myprintf( Building visibilty list for %x...\n,this)。 CBoid *flockmate = first_boid。endif return (m_num_enemies_seen)。 myprintf( total enemies seen = %d\n,m_num_enemies_seen)。 } } // get next enemy in flock enemy = enemyGetNext()。 // Test: Closest enemy? if (dist m_dist_to_nearest_enemy) { // yes...save it off m_dist_to_nearest_enemy = dist。 while (enemy != NULL) { ifdef VISIBILITY_DEBUG myprintf( looking at %x\n,enemy)。在一個群落中遇到天敵的反應(yīng)ifdef VISIBILITY_DEBUG myprintf( Testing to see if %x can see anybody in flock %d\n,this,i)。 i CFlock::FlockCount。 m_dist_to_nearest_enemy = INFINITY。endif // initialize enemy data m_num_enemies_seen = 0。 CBoid *enemy。endif return (change)。t instantly snap to a new heading, can we? (MinUrgency)。ifdef BOID_DEBUG myprintf(\nInside MatchHeading\n)。endif return (change)。re exactly the right distance away, do nothing ()。 } else {ifdef BOID_DEBUG myprintf( just right!\n)。 } else if (m_dist_to_nearest_flockmate SeparationDist) {ifdef BOID_DEBUG myprintf( too far away!\n)。endif if (m_dist_to_nearest_flockmate SeparationDist) {ifdef BOID_DEBUG myprintf( too close!\n)。 if (ratio MaxUrgency) ratio = MaxUrgency。 ifdef BOID_DEBUG myprintf(\nInside KeepDistance\n)。endif}// KeepDistance.// Generates a vector for a flock boid to maintain his// desired separation distance from the nearest flockmate he sees.防止與看到的同類相撞vector CBoid::KeepDistance (void){ float ratio = m_dist_to_nearest_flockmate/SeparationDist。 } // return change vector return (change)。endif // test: Are we too close to our nearest enemy? if (m_dist_to_nearest_enemy KeepAwayDist) {ifdef BOID_DEBUG myprintf( too close to %x\n,m_nearest_enemy)。}遇到天敵逃脫// FleeEnemies.// Generates a vector for a flock boid to avoid the // nearest enemy (boid of a different flock) it sees.vector CBoid::FleeEnemies (void){ vector change。更新速度達(dá)到期望值ifdef BOID_DEBUG myprintf( final change vector from Cruising = %f %f %f\n, , )。t like vertical motion all that much }ifdef BOID_DEBUG myprintf( intermediate change vector from Cruising = %f %f %f\n, , )。 } else { += MinUrgency * SIGN(diff)。 if (jitter ) { += MinUrgency * SIGN(diff)。 if (urgency MaxUrgency) urgency = MaxUrgency。 } else { myprintf( speeding up to meet cruising speed...\n)。 myprintf( initial change vector from Cruising = %f %f %f\n, , )。 myprintf( diff = %f urgency = %f\n,diff, urgency)。 float urgency = (float) fabs(diff)。endif}///////////////////////////// private flocking methods///////////////////////////個體如何反應(yīng)// Cruising.// Generates a vector indicating how a flock boid would// like to move, if it were all up to him and he was under// no other influences of any kind.vector CBoid::Cruising (void){ vector change = m_vel。 myprintf(final velocity = %f %f %f\n, , )。ve wandered outside the world bounds put us back in. You might // not need to do this for your world, but that depends on your implementation. WorldBound()。 }、傾斜等動作 // Step 12: Compute roll/pitch/yaw. // Compute our orientation after all this speed adjustment nonsense. ComputeRPY()。endif (MaxSpeed)。re allowed to move, constrain our velocity. if ((m_speed = ()) MaxSpeed) { // definitely too fast...constrain to maximum speedifdef BOID_DEBUG myprintf(WARNING: constraining speed for boid %x!\n,this)。 // Step 11: Constrain our speed. // If we39。 // Step 10: constraint Y velocity changes. // Attempt to restrict flight straight up/down by damping out Y axis velocity. // This isn39。s where we apply our newly puted acceleration vector // to create a new velocity vector to use next update cycle. m_oldvel = m_vel。endif // definitely too much...constrain to maximum change (MaxChange)。d move if it were all up to us. AccumulateChanges (acc, Cruising())。 AccumulateChanges (acc, FleeEnemies())。 }