// Flocking // Daniel Shiffman // Flock class // Does very little, simply manages the ArrayList of all the boids // Created 2 May 2005 //Avoidance a = new Avoidance(new Vector3D(250,300,0),1,2); //Avoidance a2 = new Avoidance(new Vector3D(450,300,0),1,2); class Flock { ArrayList boids; // An arraylist for all the boids Vector3D a0 =new Vector3D(200,300,0); Vector3D a1 = new Vector3D(400,150,0); Vector3D a2 = new Vector3D(600,400,0); Vector3D a3 = new Vector3D(100,200,0); Vector3D a4 = new Vector3D(300,250,0); Vector3D a5 = new Vector3D(600,250,0); Vector3D[] obstacles = { a0,a1,a2,a3,a4,a5 }; Flock() { boids = new ArrayList(); // Initialize the arraylist } void run() { for (int i = 0; i < boids.size(); i++) { Boid b = (Boid) boids.get(i); Vector3D closestObstacle = new Vector3D(0,0,0);//Vector to hold the closest float closestSoFar = 10000; //arbitary value to hold the cloest distance so far; for(int j = 0; j < obstacles.length; j++){ float closeness = Vector3D.distance(b.loc,obstacles[j]); if(closeness < closestSoFar){ closestSoFar = closeness; closestObstacle = obstacles[j].copy(); } } Vector3D avoidance = b.avoid(closestObstacle); avoidance.mult(3); b.acc.add(avoidance); b.run(boids); // Passing the entire list of boids to each boid individually } noStroke(); fill(255); for(int k = 0;k