@@ -170,24 +170,19 @@ class Boid {
170170 // Node
171171 Node node ;
172172 // fields
173- Vector position , velocity , acceleration , alignment , cohesion , separation ; // position, velocity, and acceleration in
173+ Vector velocity , acceleration , alignment , cohesion , separation ; // position, velocity, and acceleration in
174174 // a vector datatype
175- float neighborhoodRadius ; // radius in which it looks for fellow boids
176- float maxSpeed = 4 ; // maximum magnitude for the velocity vector
177- float maxSteerForce = 0.1f ; // maximum magnitude of the steering vector
178- float sc = 3 ; // scale factor for the render of the boid
179- float flap = 0 ;
180- float t = 0 ;
175+ final float neighborhoodRadius = 100 ; // radius in which it looks for fellow boids
176+ final float maxSpeed = 4 ; // maximum magnitude for the velocity vector
177+ final float maxSteerForce = 0.1f ; // maximum magnitude of the steering vector
178+ final float sc = 3 ; // scale factor for the render of the boid
181179
182180 Boid (Vector inPos ) {
183- node = new Node (this ::display );
181+ node = new Node (inPos );
182+ node .setShape (this ::display );
184183 startAnimation ();
185- position = new Vector ();
186- position .set (inPos );
187- node .setPosition (new Vector (position .x (), position .y (), position .z ()));
188184 velocity = Vector .random ();
189185 acceleration = new Vector ();
190- neighborhoodRadius = 100 ;
191186 }
192187
193188 public void startAnimation () {
@@ -235,25 +230,23 @@ public void display(PGraphics pg) {
235230
236231 Vector avoid (Vector target ) {
237232 Vector steer = new Vector (); // creates vector for steering
238- steer .set (Vector .subtract (position , target )); // steering vector points away from
239- steer .multiply (1 / sq (Vector .distance (position , target )));
233+ steer .set (Vector .subtract (node . position () , target )); // steering vector points away from
234+ steer .multiply (1 / sq (Vector .distance (node . position () , target )));
240235 return steer ;
241236 }
242237
243238 //-----------behaviors---------------
244239
245240 void behavior (Graph graph ) {
246- t += 0.1 ;
247- flap = 10 * sin (t );
248241 // acceleration.add(steer(new Vector(mouseX,mouseY,300),true));
249242 // acceleration.add(new Vector(0,.05,0));
250243 if (avoidWalls ) {
251- acceleration .add (Vector .multiply (avoid (new Vector (position .x (), flockHeight , position .z ())), 5 ));
252- acceleration .add (Vector .multiply (avoid (new Vector (position .x (), 0 , position .z ())), 5 ));
253- acceleration .add (Vector .multiply (avoid (new Vector (flockWidth , position .y (), position .z ())), 5 ));
254- acceleration .add (Vector .multiply (avoid (new Vector (0 , position .y (), position .z ())), 5 ));
255- acceleration .add (Vector .multiply (avoid (new Vector (position .x (), position .y (), 0 )), 5 ));
256- acceleration .add (Vector .multiply (avoid (new Vector (position .x (), position .y (), flockDepth )), 5 ));
244+ acceleration .add (Vector .multiply (avoid (new Vector (node . position () .x (), flockHeight , node . position () .z ())), 5 ));
245+ acceleration .add (Vector .multiply (avoid (new Vector (node . position () .x (), 0 , node . position () .z ())), 5 ));
246+ acceleration .add (Vector .multiply (avoid (new Vector (flockWidth , node . position () .y (), node . position () .z ())), 5 ));
247+ acceleration .add (Vector .multiply (avoid (new Vector (0 , node . position () .y (), node . position () .z ())), 5 ));
248+ acceleration .add (Vector .multiply (avoid (new Vector (node . position () .x (), node . position () .y (), 0 )), 5 ));
249+ acceleration .add (Vector .multiply (avoid (new Vector (node . position () .x (), node . position () .y (), flockDepth )), 5 ));
257250 }
258251 //alignment
259252 alignment = new Vector ();
@@ -267,20 +260,20 @@ void behavior(Graph graph) {
267260 for (int i = 0 ; i < flock .size (); i ++) {
268261 Boid boid = flock .get (i );
269262 //alignment
270- float distance = Vector .distance (position , boid .position );
263+ float distance = Vector .distance (node . position () , boid .node . position () );
271264 if (distance > 0 && distance <= neighborhoodRadius ) {
272265 alignment .add (boid .velocity );
273266 alignmentCount ++;
274267 }
275268 //cohesion
276- float dist = dist (position .x (), position .y (), boid .position .x (), boid .position .y ());
269+ float dist = dist (node . position () .x (), node . position () .y (), boid .node . position () .x (), boid .node . position () .y ());
277270 if (dist > 0 && dist <= neighborhoodRadius ) {
278- posSum .add (boid .position );
271+ posSum .add (boid .node . position () );
279272 cohesionCount ++;
280273 }
281274 //separation
282275 if (distance > 0 && distance <= neighborhoodRadius ) {
283- repulse = Vector .subtract (position , boid .position );
276+ repulse = Vector .subtract (node . position () , boid .node . position () );
284277 repulse .normalize ();
285278 repulse .divide (distance );
286279 separation .add (repulse );
@@ -294,7 +287,7 @@ void behavior(Graph graph) {
294287 //cohesion
295288 if (cohesionCount > 0 )
296289 posSum .divide ((float ) cohesionCount );
297- cohesion = Vector .subtract (posSum , position );
290+ cohesion = Vector .subtract (posSum , node . position () );
298291 cohesion .limit (maxSteerForce );
299292 acceleration .add (Vector .multiply (alignment , 1 ));
300293 acceleration .add (Vector .multiply (cohesion , 3 ));
@@ -307,26 +300,25 @@ void move() {
307300 velocity .add (acceleration ); // add acceleration to velocity
308301 velocity .limit (maxSpeed ); // make sure the velocity vector magnitude does not
309302 // exceed maxSpeed
310- position .add (velocity ); // add velocity to position
311- node .setPosition (position );
303+ node .translate (velocity );
312304 node .setOrientation (Quaternion .multiply (Quaternion .from (Vector .plusJ , atan2 (-velocity .z (), velocity .x ())),
313305 Quaternion .from (Vector .plusK , asin (velocity .y () / velocity .magnitude ()))));
314306 acceleration .multiply (0 ); // reset acceleration
315307 }
316308
317309 void checkBounds () {
318- if (position .x () > flockWidth )
319- position .setX (0 );
320- if (position .x () < 0 )
321- position .setX (flockWidth );
322- if (position .y () > flockHeight )
323- position .setY (0 );
324- if (position .y () < 0 )
325- position .setY (flockHeight );
326- if (position .z () > flockDepth )
327- position .setZ (0 );
328- if (position .z () < 0 )
329- position .setZ (flockDepth );
310+ if (node . position () .x () > flockWidth )
311+ node . position () .setX (0 );
312+ if (node . position () .x () < 0 )
313+ node . position () .setX (flockWidth );
314+ if (node . position () .y () > flockHeight )
315+ node . position () .setY (0 );
316+ if (node . position () .y () < 0 )
317+ node . position () .setY (flockHeight );
318+ if (node . position () .z () > flockDepth )
319+ node . position () .setZ (0 );
320+ if (node . position () .z () < 0 )
321+ node . position () .setZ (flockDepth );
330322 }
331323 }
332324
0 commit comments