@@ -11,7 +11,7 @@ class Boid {
1111
1212 Boid (float x , float y ) {
1313 acceleration = new PVector (0 ,0 );
14- velocity = new PVector ( random ( - 1 , 1 ), random ( - 1 , 1 ) );
14+ velocity = PVector . random2D( );
1515 location = new PVector (x,y);
1616 r = 2.0 ;
1717 maxspeed = 2 ;
@@ -60,9 +60,8 @@ class Boid {
6060 // STEER = DESIRED MINUS VELOCITY
6161 PVector seek (PVector target ) {
6262 PVector desired = PVector . sub(target,location); // A vector pointing from the location to the target
63- // Normalize desired and scale to maximum speed
64- desired. normalize();
65- desired. mult(maxspeed);
63+ // Scale to maximum speed
64+ desired. setMag(maxspeed);
6665 // Steering = Desired minus Velocity
6766 PVector steer = PVector . sub(desired,velocity);
6867 steer. limit(maxforce); // Limit to maximum steering force
@@ -120,8 +119,7 @@ class Boid {
120119 // As long as the vector is greater than 0
121120 if (steer. mag() > 0 ) {
122121 // Implement Reynolds: Steering = Desired - Velocity
123- steer. normalize();
124- steer. mult(maxspeed);
122+ steer. setMag(maxspeed);
125123 steer. sub(velocity);
126124 steer. limit(maxforce);
127125 }
@@ -143,8 +141,7 @@ class Boid {
143141 }
144142 if (count > 0 ) {
145143 sum. div((float )count);
146- sum. normalize();
147- sum. mult(maxspeed);
144+ sum. setMag(maxspeed);
148145 PVector steer = PVector . sub(sum,velocity);
149146 steer. limit(maxforce);
150147 return steer;
0 commit comments