// Flocking // Daniel Shiffman // Boid class // Methods for Separation, Cohesion, Alignment added // Click mouse to add boids into the system // Created 2 May 2005 class Boid { Vector3D loc; Vector3D vel; Vector3D acc; float r, j, nbd; // r is the radius of the boid, j is the distance it keeps to the border, nbd is its view distance float maxforce; // Maximum steering force float maxspeed; // Maximum speed int steps; // Boid(Vector3D l, float ms, float mf) { acc = new Vector3D(0,0); vel = new Vector3D(random(-1,1),random(-1,1)); loc = l.copy(); r = 4.0f; j = 5.0f; nbd = 30f; maxspeed = ms; maxforce = mf; steps = (int) random(10,30); } Boid(Vector3D l, float ms, float mf, float r_) { acc = new Vector3D(0,0); vel = new Vector3D(random(-1,1),random(-1,1)); loc = l.copy(); r = r_; j = 5.0f; nbd = 30f; maxspeed = ms; maxforce = mf; steps = (int) random(10,30); } void run(ArrayList boids) { flock(boids); update(); borders(); renderCircles(); } // We accumulate a new acceleration each time based on three rules void flock(ArrayList boids) { Vector3D sep = separate(boids); // Separation Vector3D ali = align(boids); // Alignment Vector3D coh = cohesion(boids); // Cohesion // Arbitrarily weight these forces sep.mult(1.5f);// 1.5 ali.mult(0.4f);// 0.5 coh.mult(0.6f);// 0.8 // Add the force vectors to acceleration acc.add(sep); acc.add(ali); acc.add(coh); } // Method to update location void update() { // Update velocity vel.add(acc); // sometimes add a random acceleration. // Limit speed vel.limit(maxspeed); if(steps>0) { loc.add(vel); steps--; //if(random(0,100)<1) vel.mult(-1); //sporadically change direction to a random point } else { if(random(0,100)<60) { loc.add(vel); steps = (int) random(10,70); if(random(0,100)<10) vel = new Vector3D(random(-1,1),random(-1,1)); //if you move again, there's a chance its to a random heading. } } // Reset accelertion to 0 each cycle acc.setXYZ(0,0,0); } void seek(Vector3D target) { acc.add(steer(target,false)); } void arrive(Vector3D target) { acc.add(steer(target,true)); } // A method that calculates a steering vector towards a target // Takes a second argument, if true, it slows down as it approaches the target Vector3D steer(Vector3D target, boolean slowdown) { Vector3D steer; // The steering vector Vector3D desired = Vector3D.sub(target,loc); // A vector pointing from the location to the target float d = desired.magnitude(); // Distance from the target is the magnitude of the vector // If the distance is greater than 0, calc steering (otherwise return zero vector) if (d > 0) { // Normalize desired desired.normalize(); // Two options for desired vector magnitude (1 -- based on distance, 2 -- maxspeed) if ((slowdown) && (d < 100.0f)) desired.mult(maxspeed*(d/100.0f)); // This damping is somewhat arbitrary else desired.mult(maxspeed); // Steering = Desired minus Velocity steer = Vector3D.sub(desired,vel); steer.limit(maxforce); // Limit to maximum steering force } else { steer = new Vector3D(0,0); } return steer; } void render() { // Draw a triangle rotated in the direction of velocity float theta = vel.heading2D() + radians(90); fill(200); stroke(255); pushMatrix(); translate(loc.x,loc.y); rotate(theta); beginShape(TRIANGLES); vertex(0, -r*2); vertex(-r, r*2); vertex(r, r*2); endShape(); popMatrix(); //println(vel.heading2D()); } // SK 20070815 void renderCircles() { // Draw a triangle rotated in the direction of velocity float theta = vel.heading2D() + radians(90); fill(200); stroke(255); pushMatrix(); translate(loc.x,loc.y); rotate(theta); ellipse(0,0,r*3,r*3); popMatrix(); } // Don't Wraparound! // SK 20070816 void borders() { // leftwall if (loc.x <= r+j && (vel.heading2D() > 1.562f || vel.heading2D() < -1.562f)) vel = new Vector3D(random(-1,1),random(-1,1)); // topwall if (loc.y <= r+j && vel.heading2D() > -3.15f && vel.heading2D() < 0f) vel = new Vector3D(random(-1,1),random(-1,1)); // rightwall if (loc.x >= width-(r+j) && vel.heading2D() > -1.562f && vel.heading2D() < 1.562f) vel = new Vector3D(random(-1,1),random(-1,1)); // bottomwall if (loc.y >= height-(r+j) && vel.heading2D() > 0f && vel.heading2D() < 3.15f) vel = new Vector3D(random(-1,1),random(-1,1)); } // Separation // Method checks for nearby boids and steers away Vector3D separate (ArrayList boids) { float desiredseparation = 25.0f; Vector3D sum = new Vector3D(0,0,0); int count = 0; // For every boid in the system, check if it's too close for (int i = 0 ; i < boids.size(); i++) { Boid other = (Boid) boids.get(i); float d = Vector3D.distance(loc,other.loc); // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself) if ((d > 0) && (d < desiredseparation)) { // Calculate vector pointing away from neighbor Vector3D diff = Vector3D.sub(loc,other.loc); diff.normalize(); diff.div(d); // Weight by distance sum.add(diff); count++; // Keep track of how many } } // Average -- divide by how many if (count > 0) { sum.div((float)count); } return sum; } // Alignment // For every nearby boid in the system, calculate the average velocity Vector3D align (ArrayList boids) { float neighbordist = nbd; Vector3D sum = new Vector3D(0,0,0); int count = 0; for (int i = 0 ; i < boids.size(); i++) { Boid other = (Boid) boids.get(i); float d = Vector3D.distance(loc,other.loc); if ((d > 0) && (d < neighbordist)) { sum.add(other.vel); count++; } } if (count > 0) { sum.div((float)count); sum.limit(maxforce); } return sum; } // Cohesion // For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location Vector3D cohesion (ArrayList boids) { float neighbordist = nbd; Vector3D sum = new Vector3D(0,0,0); // Start with empty vector to accumulate all locations int count = 0; for (int i = 0 ; i < boids.size(); i++) { Boid other = (Boid) boids.get(i); float d = Vector3D.distance(loc,other.loc); if ((d > 0) && (d < neighbordist)) { sum.add(other.loc); // Add location count++; } } if (count > 0) { sum.div((float)count); return steer(sum,false); // Steer towards the location } return sum; } }