FlockCell flockCell; class FlockCell { ArrayList boids2; // An arraylist for all the boids2 FlockCell() { boids2 = new ArrayList(); // Initialize the arraylist } void run() { for (int i = 0; i < boids2.size(); i++) { Boid2 b = (Boid2) boids2.get(i); b.run(boids2); // Passing the entire list of boids2 to each boid individually } } void addBoid2(Boid2 b) { boids2.add(b); } } class Boid2 { Vector3Dcell loc; Vector3Dcell vel; Vector3Dcell acc; float r; float maxforce; // Maximum steering force float maxspeed; // Maximum speed Boid2(Vector3Dcell l, float ms, float mf) { acc = new Vector3Dcell(0,0); vel = new Vector3Dcell(random(-1,1),random(-1,1)); loc = l.copy(); r = 10.0f; // size of boids2 NOTE: size must be changed in render() --> draw_target(size, rings); maxspeed = ms; maxforce = mf; } void run(ArrayList boids2) { flockCell(boids2); update(); borders(); render(); } // We accumulate a new acceleration each time based on three rules void flockCell(ArrayList boids2) { Vector3Dcell sep = separate(boids2); // Separation Vector3Dcell ali = align(boids2); // Alignment Vector3Dcell coh = cohesion(boids2); // Cohesion // Arbitrarily weight these forces sep.mult(0.20f); // higher the number more they separate ali.mult(0.01f); // higher the number the more likely to align with each other coh.mult(0.03f); // higher the number the tighter they clump // 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); // Limit speed vel.limit(maxspeed); loc.add(vel); // Reset accelertion to 0 each cycle acc.setXYZ(0,0,0); } void seek(Vector3Dcell target) { acc.add(steer(target,false)); } void arrive(Vector3Dcell 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 Vector3Dcell steer(Vector3Dcell target, boolean slowdown) { Vector3Dcell steer; // The steering vector Vector3Dcell desired = target.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 = target.sub(desired,vel); steer.limit(maxforce); // Limit to maximum steering force } else { steer = new Vector3Dcell(0,0); } return steer; } void render() { // Draw a triangle rotated in the direction of velocity float theta = vel.heading2D() + radians(90); stroke(40, 0, 0); // stroke colour of the boids2 strokeWeight(2); pushMatrix(); translate(loc.x,loc.y); rotate(theta); // ellipse(r,r, 4*r, 4*r); // ellipse(loc.x,loc.y, 2*r, 3*r); draw_target(40, 3); popMatrix(); } void draw_target(int size, int num) { float grayvalues = 255/num; float steps = size/num; for(int i=0; i width+r) loc.x = -r; if (loc.y > height+r) loc.y = -r; } // Separation // Method checks for nearby boids2 and steers away Vector3Dcell separate (ArrayList boids2) { float desiredseparation = 25.0f; Vector3Dcell sum = new Vector3Dcell(0,0,0); int count = 0; // For every boid in the system, check if it's too close for (int i = 0 ; i < boids2.size(); i++) { Boid2 other = (Boid2) boids2.get(i); float d = loc.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 Vector3Dcell diff = loc.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 Vector3Dcell align (ArrayList boids2) { float neighbordist = 50.0f; Vector3Dcell sum = new Vector3Dcell(0,0,0); int count = 0; for (int i = 0 ; i < boids2.size(); i++) { Boid2 other = (Boid2) boids2.get(i); float d = loc.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 boids2, calculate steering vector towards that location Vector3Dcell cohesion (ArrayList boids2) { float neighbordist = 50.0f; Vector3Dcell sum = new Vector3Dcell(0,0,0); // Start with empty vector to accumulate all locations int count = 0; for (int i = 0 ; i < boids2.size(); i++) { Boid2 other = (Boid2) boids2.get(i); float d = loc.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; } } // Simple Vector3Dcell Class static class Vector3Dcell { float x; float y; float z; Vector3Dcell(float x_, float y_, float z_) { x = x_; y = y_; z = z_; } Vector3Dcell(float x_, float y_) { x = x_; y = y_; z = 0f; } Vector3Dcell() { x = 0f; y = 0f; z = 0f; } void setX(float x_) { x = x_; } void setY(float y_) { y = y_; } void setZ(float z_) { z = z_; } void setXY(float x_, float y_) { x = x_; y = y_; } void setXYZ(float x_, float y_, float z_) { x = x_; y = y_; z = z_; } void setXYZ(Vector3Dcell v) { x = v.x; y = v.y; z = v.z; } float magnitude() { return (float) Math.sqrt(x*x + y*y + z*z); } Vector3Dcell copy() { return new Vector3Dcell(x,y,z); } Vector3Dcell copy(Vector3Dcell v) { return new Vector3Dcell(v.x, v.y,v.z); } void add(Vector3Dcell v) { x += v.x; y += v.y; z += v.z; } void sub(Vector3Dcell v) { x -= v.x; y -= v.y; z -= v.z; } void mult(float n) { x *= n; y *= n; z *= n; } void div(float n) { x /= n; y /= n; z /= n; } void normalize() { float m = magnitude(); if (m > 0) { div(m); } } void limit(float max) { if (magnitude() > max) { normalize(); mult(max); } } float heading2D() { float angle = (float) Math.atan2(-y, x); return -1*angle; } Vector3Dcell add(Vector3Dcell v1, Vector3Dcell v2) { Vector3Dcell v = new Vector3Dcell(v1.x + v2.x,v1.y + v2.y, v1.z + v2.z); return v; } Vector3Dcell sub(Vector3Dcell v1, Vector3Dcell v2) { Vector3Dcell v = new Vector3Dcell(v1.x - v2.x,v1.y - v2.y,v1.z - v2.z); return v; } Vector3Dcell div(Vector3Dcell v1, float n) { Vector3Dcell v = new Vector3Dcell(v1.x/n,v1.y/n,v1.z/n); return v; } Vector3Dcell mult(Vector3Dcell v1, float n) { Vector3Dcell v = new Vector3Dcell(v1.x*n,v1.y*n,v1.z*n); return v; } float distance (Vector3Dcell v1, Vector3Dcell v2) { float dx = v1.x - v2.x; float dy = v1.y - v2.y; float dz = v1.z - v2.z; return (float) Math.sqrt(dx*dx + dy*dy + dz*dz); } }