Skip to content

Instantly share code, notes, and snippets.

@logtdm
Created February 13, 2016 02:04
Show Gist options
  • Save logtdm/1a4bfe19598a30925651 to your computer and use it in GitHub Desktop.
Save logtdm/1a4bfe19598a30925651 to your computer and use it in GitHub Desktop.
// Taylor Martin
// The following is Processing Sketch the draws a 3D City Scape
// The Camera function and mouseX determines the movements of the
// Mouse and how it will react when dragged across the Screen
// In order to Mash this project with previously designed code
// Flocking from Processing.org was used, this code created an
// Abstract image of "Birds", this worked while with the City Scape Theme
Flock flock;
void setup() {
size(1500, 1000, P3D);//setting canvas size and 3D drawings
flock = new Flock();// creating the flock class
// Add an initial set of boids into the system
for (int i = 0; i < 150; i++) {
flock.addBoid(new Boid(width/2,height/2)); //if statement that adds new boid
}
}
void draw() {
background(0);//setting the background colour as black
flock.run();//calling the flock class
//with 3D drawing shapes must have and x, y, and z axis now
//the following code is for the distant white buildings
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0); //determines the movements of the mouse and how it will react when dragged across the canvas
translate(width/9, height/9, -700);//building placement on the canvas
stroke(0);//line colour on buildings
fill(255);//colour of buildings
box(200, 900, 200);//size of buildings
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/3.9, height/9, -700);
stroke(0);
fill(255);
box(200, 900, 200);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/2.5, height/9, -700);
stroke(0);
fill(255);
box(200, 900, 200);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.84, height/9, -700);
stroke(0);
fill(255);
box(200, 900, 200);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.46, height/9, -700);
stroke(0);
fill(255);
box(200, 900, 200);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.21, height/9, -700);
stroke(0);
fill(255);
box(200, 900, 200);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.033, height/9, -700);
stroke(0);
fill(255);
box(200, 900, 200);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/.9, height/9, -700);
stroke(0);
fill(255);
box(200, 900, 200);
//the following code is for the black buildings
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/9, height/4, -400);
stroke(255);
fill(0);
box(200, 700, 200);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/3.5, height/4, -400);
stroke(255);
fill(0);
box(200, 700, 200);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/2.2, height/4, -400);
stroke(255);
fill(0);
box(200, 700, 200);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.6 , height/4, -400);
stroke(255);
fill(0);
box(200, 700, 200);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.25, height/4, -400);
stroke(255);
fill(0);
box(200, 700, 200);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.03, height/4, -400);
stroke(255);
fill(0);
box(200, 700, 200);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/.87, height/4, -400);
stroke(255);
fill(0);
box(200, 700, 200);
//the following is code for the white building in the front of the canvas
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.5, height/2.85, -100);
stroke(0);
fill(255);
box(200, 500, 200);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.5, height/1.82, -20);
stroke(255);
fill(0);
box(50, 100, 50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.6, height/2.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.5, height/2.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.41, height/2.3, -20);
stroke(255);
fill(0);
box(50);
//
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.6, height/3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.5, height/3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.41, height/3, -20);
stroke(255);
fill(0);
box(50);
//
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.6, height/4.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.5, height/4.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.41, height/4.3, -20);
stroke(255);
fill(0);
box(50);
//
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.6, height/7, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.5, height/7, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.41, height/7, -20);
stroke(255);
fill(0);
box(50);
//this is in place to seperate the code for each white building
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.2, height/2.85, -100);
stroke(0);
fill(255);
box(200, 500, 200);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.2, height/1.82, -20);
stroke(255);
fill(0);
box(50, 100, 50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.2, height/2.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.265, height/2.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.143, height/2.3, -20);
stroke(255);
fill(0);
box(50);
//
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.2, height/3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.265, height/3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.143, height/3, -20);
stroke(255);
fill(0);
box(50);
//
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.2, height/4.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.265, height/4.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.143, height/4.3, -20);
stroke(255);
fill(0);
box(50);
//
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.2, height/7, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.265, height/7, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.143, height/7, -20);
stroke(255);
fill(0);
box(50);
//this is in place to seperate the code for each white building
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1, height/2.85, -100);
stroke(0);
fill(255);
box(200, 500, 200);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1, height/1.82, -20);
stroke(255);
fill(0);
box(50, 100, 50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1, height/2.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.045, height/2.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/.96, height/2.3, -20);
stroke(255);
fill(0);
box(50);
//
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1, height/3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.045, height/3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/.96, height/3, -20);
stroke(255);
fill(0);
box(50);
//
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1, height/4.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.045, height/4.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/.96, height/4.3, -20);
stroke(255);
fill(0);
box(50);
//
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1, height/7, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.045, height/7, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/.96, height/7, -20);
stroke(255);
fill(0);
box(50);
//this is in place to seperate the code for each white building
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/2, height/2.85, -100);
stroke(0);
fill(255);
box(200, 500, 200);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/2, height/1.82, -20);
stroke(255);
fill(0);
box(50, 100, 50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/2, height/2.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.85, height/2.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/2.18, height/2.3, -20);
stroke(255);
fill(0);
box(50);
//
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/2, height/3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.85, height/3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/2.18, height/3, -20);
stroke(255);
fill(0);
box(50);
//
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/2, height/4.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.85, height/4.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/2.18, height/4.3, -20);
stroke(255);
fill(0);
box(50);
//
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/2, height/7, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.85, height/7, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/2.18, height/7, -20);
stroke(255);
fill(0);
box(50);
//this is in place to seperate the code for each white building
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/3, height/2.85, -100);
stroke(0);
fill(255);
box(200, 500, 200);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/3, height/1.82, -20);
stroke(255);
fill(0);
box(50, 100, 50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/3, height/2.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/2.67, height/2.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/3.42, height/2.3, -20);
stroke(255);
fill(0);
box(50);
//
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/3, height/3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/2.67, height/3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/3.42, height/3, -20);
stroke(255);
fill(0);
box(50);
//
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/3, height/4.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/2.67, height/4.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/3.42, height/4.3, -20);
stroke(255);
fill(0);
box(50);
//
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/3, height/7, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/2.67, height/7, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/3.42, height/7, -20);
stroke(255);
fill(0);
box(50);
//this is in place to seperate the code for each white building
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/6, height/2.85, -100);
stroke(0);
fill(255);
box(200, 500, 200);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/6, height/1.82, -20);
stroke(255);
fill(0);
box(50, 100, 50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/6, height/2.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/4.77, height/2.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/8, height/2.3, -20);
stroke(255);
fill(0);
box(50);
//
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/6, height/3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/4.77, height/3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/8, height/3, -20);
stroke(255);
fill(0);
box(50);
//
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/6, height/4.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/4.77, height/4.3, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/8, height/4.3, -20);
stroke(255);
fill(0);
box(50);
//
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/6, height/7, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/4.77, height/7, -20);
stroke(255);
fill(0);
box(50);
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/8, height/7, -20);
stroke(255);
fill(0);
box(50);
//this is in place to seperate the code for the grey road
camera(mouseX, height/2, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
translate(width/1.2, height/1.35, -200);
stroke(0);
fill(109, 123, 141);
box(4500, 200, 200);
//////////////////////////////////////////////////////////////
}
//this is where the coding for the "birds" begins
// The Boid class
class Boid {
PVector location;
PVector velocity;
PVector acceleration;
float r;
float maxforce; // maximum steering force
float maxspeed; // maximum speed
Boid(float x, float y) {
acceleration = new PVector(0, 0);
float angle = random(TWO_PI);
velocity = new PVector(cos(angle), sin(angle));
location = new PVector(x, y);
r = 2.0;
maxspeed = 5; //determining the speed of the boids
maxforce = 0.03;
}
void run(ArrayList<Boid> boids) {
flock(boids);
update();
borders();
render();
}
void applyForce(PVector force) {
acceleration.add(force);
}
//acceleration changes based on separation, alignment and cohesion
void flock(ArrayList<Boid> boids) {
PVector sep = separate(boids); // separation
PVector ali = align(boids); // alignment
PVector coh = cohesion(boids); // cohesion
sep.mult(1.5);
ali.mult(1.0);
coh.mult(1.0);
applyForce(sep);
applyForce(ali);
applyForce(coh);
}
void update() {
velocity.add(acceleration);
velocity.limit(maxspeed);//gives a max speed
location.add(velocity);
acceleration.mult(0);// resets acceleration
}
PVector seek(PVector target) {
PVector desired = PVector.sub(target, location); // a vector pointing from the location to the target
// Scale to maximum speed
desired.normalize();
desired.mult(maxspeed);
// steering = desired minus velocity
PVector steer = PVector.sub(desired, velocity);
steer.limit(maxforce); // Limit to maximum steering force
return steer;
}
void render() {
// draw a triangle rotated in the direction of velocity
float theta = velocity.heading2D() + radians(90);
fill(255);
stroke(255);
pushMatrix();
translate(location.x, location.y);
rotate(theta);
beginShape(TRIANGLES);
vertex(0, -r*2);
vertex(-r, r*2);
vertex(r, r*2);
endShape();
popMatrix();
}
void borders() {
if (location.x < -r) location.x = width+r;
if (location.y < -r) location.y = height+r;
if (location.x > width+r) location.x = -r;
if (location.y > height+r) location.y = -r;
}
// separation
// checks for nearby boids and steers away
PVector separate (ArrayList<Boid> boids) {
float desiredseparation = 25.0f;
PVector steer = new PVector(0, 0, 0);
int count = 0;
// check if boids are too close
for (Boid other : boids) {
float d = PVector.dist(location, other.location);
// 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
PVector diff = PVector.sub(location, other.location);
diff.normalize();
diff.div(d); // weight by distance
steer.add(diff);
count++; // keep track of how many
}
}
// average -- divide by how many
if (count > 0) {
steer.div((float)count);
}
// vector is greater than 0
if (steer.mag() > 0) {
steer.normalize();
steer.mult(maxspeed);
steer.sub(velocity);
steer.limit(maxforce);
}
return steer;
}
// Alignment
// For every nearby boid in the system, calculate the average velocity
PVector align (ArrayList<Boid> boids) {
float neighbordist = 50;
PVector sum = new PVector(0, 0);
int count = 0;
for (Boid other : boids) {
float d = PVector.dist(location, other.location);
if ((d > 0) && (d < neighbordist)) {
sum.add(other.velocity);
count++;
}
}
if (count > 0) {
sum.div((float)count);
sum.normalize();
sum.mult(maxspeed);
PVector steer = PVector.sub(sum, velocity);
steer.limit(maxforce);
return steer;
}
else {
return new PVector(0, 0);
}
}
// Cohesion
// For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
PVector cohesion (ArrayList<Boid> boids) {
float neighbordist = 50;
PVector sum = new PVector(0, 0); // Start with empty vector to accumulate all locations
int count = 0;
for (Boid other : boids) {
float d = PVector.dist(location, other.location);
if ((d > 0) && (d < neighbordist)) {
sum.add(other.location); // Add location
count++;
}
}
if (count > 0) {
sum.div(count);
return seek(sum); // Steer towards the location
}
else {
return new PVector(0, 0);
}
}
}
// The Flock (a list of Boid objects)
class Flock {
ArrayList<Boid> boids; // An ArrayList for all the boids
Flock() {
boids = new ArrayList<Boid>(); // Initialize the ArrayList
}
void run() {
for (Boid b : boids) {
b.run(boids); // Passing the entire list of boids to each boid individually
}
}
void addBoid(Boid b) {
boids.add(b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment