// Manager object for a group of Boid particles class BoidFlock extends Physics{ Point center, migration; float migrationSpeed = 0.005; int count; // Constructor BoidFlock(){ super(); center = new Point(); migration = new Point(); count = 0; } // Add a Boid - in lieu of addParticle so it gets the right object Boid addBoid(float x, float y){ Boid temp = new Boid(x, y, this); temp.id = count; count++; p.add(temp); return temp; } // Move all the boids to their known center void group(){ updateCenter(); for(int i = 0; i < p.size(); i++){ Boid temp = (Boid)p.get(i); temp.group(); } } // Make all the boids avoid each other void avoid(){ for(int i = 0; i < p.size(); i++){ Boid temp = (Boid)p.get(i); temp.avoid(); } } // Make all the boids fly in the same direction void migrate(){ updateMigration(); for(int i = 0; i < p.size(); i++){ Boid temp = (Boid)p.get(i); temp.addVelocity(migration.x * migrationSpeed, migration.y * migrationSpeed); } } // Give all the boids a common goal to fly towards void goal(float x, float y){ for(int i = 0; i < p.size(); i++){ Boid temp = (Boid)p.get(i); temp.goal(x, y); } } // Update the average vector they are all flying in void updateMigration(){ float x = 0; float y = 0; for(int i = 0; i < p.size(); i++){ Boid temp = (Boid)p.get(i); x += temp.x - temp.px; y += temp.y - temp.py; } migration.x = x / p.size(); migration.y = y / p.size(); } // Update the current center of the group void updateCenter(){ float x = 0; float y = 0; for(int i = 0; i < p.size(); i++){ Boid temp = (Boid)p.get(i); x += temp.x; y += temp.y; } center.x = x / p.size(); center.y = y / p.size(); } }