// Flocking // Daniel Shiffman // The Nature of Code, Spring 2009 // Demonstration of Craig Reynolds' "Flocking" behavior // See: http://www.red3d.com/cwr/ // Rules: Cohesion, Separation, Alignment // Click mouse to add boids into the system Flock flock; int colorDiff = 0 ; int maxDiff = 0; int minDiff = 0; float rDiff = 0 ; float maxRDiff = 0; float minRDiff = 0; float rThresh = 2.0; int colorThresh = 20; void setup() { size(600,600); flock = new Flock(); // Add an initial set of boids into the system for (int i = 0; i < 100; i++) { flock.addBoid(new Boid(new PVector(width/2,height/2),5.0,0.05)); } smooth(); } void draw() { background(255); flock.run(); //flock.tellColor(); compareColor(); compareR(); } // Add a new boid into the System void mousePressed() { flock.addBoid(new Boid(new PVector(mouseX,mouseY),2.0,0.05f)); } void compareColor() { for (int i=0; i < flock.getBoidNum(); i++) { Boid b1 = (Boid)(flock.boids).get(i); for (int j = i; j maxDiff) maxDiff= colorDiff; if(colorDiff colorThresh){ b1.colorDifference(30); } else{ b1.colorDifference(1); } /* if( b1.colorBoid == b2.colorBoid){ b1.colorDifference(1); } else{ b1.colorDifference(10); }*/ } } } void compareR() { for (int i=0; i < flock.getBoidNum(); i++) { Boid b1 = (Boid)(flock.boids).get(i); for (int j = i; j maxRDiff) maxRDiff= rDiff; if(rDiff rThresh){ b1.velocityVal(1.0); } else{ b1.velocityVal(1); }*/ } } } void keyPressed(){ if(key == '='){ colorThresh++; } else if (key == '-') { colorThresh--; } else if(key == 'z'){ rThresh++; } else if (key == 'x') { rThresh--; } }