FOR INTERACTIVE SKETCH, CLICK HERE.
^the program runs too slow on this page…
Flock series, 2018-current.
Rendered with code in Processing.
Flock flock;
ArrayList boids;
float numBoids = 200;
boolean fade = false;
int fadeCount;
void setup() {
size(320, 500);
//size(2560, 1440);
flock = new Flock();
for (int i = 1; i < numBoids+1; i++) {
boids.add(new Boid(i, random(0, width), random(0, height)));
}
background(255);
}
void draw() {
background(255);
noStroke();
//fill(0, 4);
fill(255);
rect(0, 0, width, height);
flock.run();
if (boids.size() <= 0) {
fade = true;
}
if (fade) {
noStroke();
//fill(0, 10);
fill(255);
rect(0, 0, width, height);
fadeCount ++;
}
}
void addBoid(int howMany, float x, float y) {
for (int i = 0; i < howMany; i++) {
boids.add(new Boid(boids.size()+1, x, y));
}
}
void deleteBoid(int delid) {
boids.remove(delid-1);
for (int i = delid-1; i();
}
void run() {
for (int i = 0; i < boids.size(); i++) {
Boid b = boids.get(i);
b.run(boids);
}
}
}
class Boid {
PVector position;
PVector velocity;
PVector acceleration;
float r;
float maxforce; // Maximum steering force
float maxspeed; // Maximum speed
float xx ;
float xd = .1;
boolean seeking = false;
float scale = random(1, 2);
int id;
Boid(int idint, float x, float y) {
id = idint;
acceleration = new PVector(0, 0);
float angle = random(TWO_PI);
velocity = new PVector(cos(angle), sin(angle));
position = new PVector(x, y);
r = 2.0;
maxspeed = 2;
maxforce = .01;
}
void run(ArrayList boids) {
flock(boids);
update();
borders();
render();
textSize(12);
fill(255);
}
void applyForce(PVector force) {
// We could add mass here if we want A = F / M
acceleration.add(force);
}
void flock(ArrayList boids) {
PVector sep = separate(boids);
PVector ali = align(boids);
PVector coh = cohesion(boids);
sep.mult(4);
ali.mult(1);
coh.mult(1);
applyForce(sep);
applyForce(ali);
applyForce(coh);
}
void update() {
velocity.add(acceleration);
velocity.limit(maxspeed);
position.add(velocity);
acceleration.mult(0);
}
PVector seek(PVector target) {
PVector desired = PVector.sub(target, position);
desired.normalize();
desired.mult(maxspeed);
PVector steer = PVector.sub(desired, velocity);
steer.limit(maxforce);
return steer;
}
void render() {
float theta = velocity.heading() + radians(90);
xx += xd;
if (xx >=2 || xx <= -2)xd*=-1;
fill(255);
stroke(map(scale, 1.5, 2, 0, 255), 0, map(scale, 2, 1.5, 0, 255));
if (seeking) {
if (scale < 2) {
xx+=xd;
scale+=.001;
}
} else {
if (scale>=.1) {
scale-=.1;
}
}
if (scale<=.1 && id>0) {
deleteBoid(id);
}
strokeWeight(2);
pushMatrix();
translate(position.x, position.y);
rotate(theta);
beginShape();
vertex((-r+xx)*scale, (r*2)*scale);
vertex((-r)*scale, (-r*2)*scale);
vertex((r)*scale, (-r*2)*scale);
vertex((r-xx)*scale, (r*2)*scale);
endShape();
popMatrix();
}
void borders() {
if (position.x < -r) position.x = width+r;
if (position.y < -r) position.y = height+r;
if (position.x > width+r) position.x = -r;
if (position.y > height+r) position.y = -r;
}
PVector separate (ArrayList boids) {
float desiredseparation = 30.0f;
PVector steer = new PVector(0, 0, 0);
int count = 0;
for (int i = 0; i < boids.size(); i++) {
Boid other = boids.get(i);
float d = PVector.dist(position, other.position);
if ((d > 0) && (d < desiredseparation)) {
PVector diff = PVector.sub(position, other.position);
diff.normalize();
diff.div(d); // Weight by distance
steer.add(diff);
count++; // Keep track of how many
}
}
if (count > 0) {
steer.div((float)count);
}
if (steer.mag() > 0) {
steer.normalize();
steer.mult(maxspeed);
steer.sub(velocity);
steer.limit(maxforce);
}
return steer;
}
PVector align (ArrayList boids) {
float neighbordist = 50;
PVector sum = new PVector(0, 0);
int count = 0;
for (int i = 0; i < boids.size(); i++) {
Boid other = boids.get(i);
float d = PVector.dist(position, other.position);
if ((d > 0) && (d < neighbordist)) {
sum.add(other.velocity);
count++;
}
}
if (count > 0) {
seeking = true;
sum.div((float)count);
sum.normalize();
sum.mult(maxspeed);
PVector steer = PVector.sub(sum, velocity);
steer.limit(maxforce);
return steer;
} else {
seeking = false;
return new PVector(0, 0);
}
}
PVector cohesion (ArrayList boids) {
float neighbordist = 100;
PVector sum = new PVector(0, 0); // Start with empty vector to accumulate all positions
int count = 0;
for (int i = 0; i < boids.size(); i++) {
Boid other = boids.get(i);
float d = PVector.dist(position, other.position);
if ((d > 0) && (d < neighbordist)) {
sum.add(other.position); // Add position
count++;
}
}
if (count > 0) {
sum.div(count);
return seek(sum); // Steer towards the position
} else {
return new PVector(0, 0);
}
}
void reOrder() {
id-=1;
}
}
Seriously, don’t open the code on your phone.