can any one help me with this code, I need it right now, since I have final submission and presentation today. please help me. my code runs the stop after 10 second
// Path Following
// Using this variable to decide whether to draw all the stuff
boolean debug = true;
Flock flock;
// bus paths
Path pathOrange;
Path pathP1;
Path pathP2;
Path pathG1;
Path pathB1;
// car paths
Path pathQ;
Path pathBroad;
Path pathHW;
Path pathHWR;
// buses
Vehicle bus1;
Vehicle bus2;
Vehicle bus3;
Vehicle bus4;
Vehicle bus5;
//cars
Car car1;
//Vehicle car2;
//Vehicle car3;
//Vehicle car4;
//Vehicle car5;
//Vehicle car6;
//Vehicle car7;
//Vehicle car8;
//Vehicle car9;
//Vehicle car10;
//Vehicle car11;
//Vehicle car12;
//Vehicle car13;
//Vehicle car14;
//Vehicle car15;
//Vehicle car16;
//Vehicle car17;
//Vehicle car18;
//Vehicle car19;
//Vehicle car20;
//Vehicle car21;
//Vehicle car22;
Car BC1;
Car BC2;
Car BC3;
Car BC4;
Car BC5;
Car BC6;
Car BC7;
Car BC8;
Car H1;
Car HR1;
int dayCnt = 0;
void setup() {
size(1280, 800);
//wind
flock = new Flock();
// Add an initial set of boids into the system
for (int i = 0; i < 350; i++) {
flock.addBoid(new Boid(width/2,height/2));
}
// Call a function to generate new Path object
newPath();
// Each vehicle has different maxspeed and maxforce for demo purposes
//buses
bus1 = new Vehicle(new PVector(0, height/2), .5, 0.1);
bus2 = new Vehicle(new PVector(0, height/2), .25, 0.1);
bus3 = new Vehicle(new PVector(250, height+20), .5, .1);
bus4 = new Vehicle(new PVector(0, height+20), .5, .1);
bus5 = new Vehicle(new PVector(420, -20), .25, .1);
//cars
car1 = new Car(new PVector(10, height+20), .5, 0.1);
//car2 = new Vehicle(new PVector(10, height+19), .5, 0.1);
//car3 = new Vehicle(new PVector(10, height+18), .5, 0.1);
//car4 = new Vehicle(new PVector(10, height+17), .5, 0.1);
//car5 = new Vehicle(new PVector(10, height+16), .5, 0.1);
//car6 = new Vehicle(new PVector(10, height+15), .5, 0.1);
//car7 = new Vehicle(new PVector(10, height+14), .5, 0.1);
//car8 = new Vehicle(new PVector(10, height+13), .5, 0.1);
//car9 = new Vehicle(new PVector(10, height+12), .5, 0.1);
//car10 = new Vehicle(new PVector(10, height+11), .5, 0.1);
//car11 = new Vehicle(new PVector(10, height+10), .5, 0.1);
//car12 = new Vehicle(new PVector(10, height+9), .5, 0.1);
//car13 = new Vehicle(new PVector(10, height+8), .5, 0.1);
//car14 = new Vehicle(new PVector(10, height+7), .5, 0.1);
//car15 = new Vehicle(new PVector(10, height+6), .5, 0.1);
//car16 = new Vehicle(new PVector(10, height+5), .5, 0.1);
//car17 = new Vehicle(new PVector(10, height+4), .5, 0.1);
//car18 = new Vehicle(new PVector(10, height+3), .5, 0.1);
//car19 = new Vehicle(new PVector(10, height+2), .5, 0.1);
//car20 = new Vehicle(new PVector(10, height+1), .5, 0.1);
//car21 = new Vehicle(new PVector(10, height), .5, 0.1);
//car22 = new Vehicle(new PVector(10, height-1), .5, 0.1);
BC1 = new Car(new PVector(-20, 350), .5, 0.1);
BC2 = new Car(new PVector(-18, 350), .4, 0.15);
BC3 = new Car(new PVector(-16, 350), .3, 0.13);
BC4 = new Car(new PVector(-14, 350), .6, 0.18);
BC5 = new Car(new PVector(-12, 350), .7, 0.12);
BC6 = new Car(new PVector(-10, 350), .45, 0.11);
BC7 = new Car(new PVector(-8, 350), .35, 0.1);
BC8 = new Car(new PVector(-6, 350), .25, 0.16);
H1 = new Car(new PVector(-20, 50), .5, 3);
HR1 = new Car(new PVector(465, height+20), 1, .1);
}
void draw() {
background(50);
println(frameCount);
flock.run();
// Display the path
pathOrange.display();
pathP1.display();
pathP2.display();
pathG1.display();
pathB1.display();
pathQ.display();
pathBroad.display();
pathHW.display();
pathHWR.display();
// The boids follow the path
bus1.follow(pathOrange);
bus2.follow(pathP1);
bus3.follow(pathP2);
bus4.follow(pathG1);
bus5.follow(pathB1);
car1.follow(pathQ);
//car2.follow(pathQ);
//car3.follow(pathQ);
//car4.follow(pathQ);
//car5.follow(pathQ);
//car6.follow(pathQ);
//car7.follow(pathQ);
//car8.follow(pathQ);
//car9.follow(pathQ);
//car10.follow(pathQ);
//car11.follow(pathQ);
//car12.follow(pathQ);
//car13.follow(pathQ);
//car14.follow(pathQ);
//car15.follow(pathQ);
//car16.follow(pathQ);
//car17.follow(pathQ);
//car18.follow(pathQ);
//car19.follow(pathQ);
//car20.follow(pathQ);
//car21.follow(pathQ);
//car22.follow(pathQ);
BC1.follow(pathBroad);
BC2.follow(pathBroad);
BC3.follow(pathBroad);
BC4.follow(pathBroad);
BC5.follow(pathBroad);
BC6.follow(pathBroad);
BC7.follow(pathBroad);
BC8.follow(pathBroad);
H1.follow(pathHW);
HR1.follow(pathHWR);
// Call the generic run method (update, borders, display, etc.)
bus1.run();
bus2.run();
bus3.run();
bus4.run();
bus5.run();
car1.run();
//car2.run();
//car3.run();
//car4.run();
//car5.run();
//car6.run();
//car7.run();
//car8.run();
//car9.run();
//car10.run();
//car11.run();
//car12.run();
//car13.run();
//car14.run();
//car15.run();
//car16.run();
//car17.run();
//car18.run();
//car19.run();
//car20.run();
//car21.run();
//car22.run();
BC1.run();
BC2.run();
BC3.run();
BC4.run();
BC5.run();
BC6.run();
BC7.run();
BC8.run();
H1.run();
HR1.run();
bus1.borders(pathOrange);
bus2.borders(pathP1);
bus3.borders(pathP2);
bus4.borders(pathG1);
bus5.borders(pathB1);
car1.borders(pathQ);
//car2.borders(pathQ);
//car3.borders(pathQ);
//car4.borders(pathQ);
//car5.borders(pathQ);
//car6.borders(pathQ);
//car7.borders(pathQ);
//car8.borders(pathQ);
//car9.borders(pathQ);
//car10.borders(pathQ);
//car11.borders(pathQ);
//car12.borders(pathQ);
//car13.borders(pathQ);
//car14.borders(pathQ);
//car15.borders(pathQ);
//car16.borders(pathQ);
//car17.borders(pathQ);
//car18.borders(pathQ);
//car19.borders(pathQ);
//car20.borders(pathQ);
//car21.borders(pathQ);
//car22.borders(pathQ);
BC1.borders(pathBroad);
BC2.borders(pathBroad);
BC3.borders(pathBroad);
BC4.borders(pathBroad);
BC5.borders(pathBroad);
BC6.borders(pathBroad);
BC7.borders(pathBroad);
BC8.borders(pathBroad);
H1.borders(pathHW);
HR1.borders(pathHWR);
// Instructions
fill(255);
text("DAY: " + dayCnt, width-100, height-20);
}
// Add a new boid into the System
void mousePressed() {
flock.addBoid(new Boid(mouseX,mouseY));
}
// 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);
}
}
void newPath() {
// A path is a series of connected points
// A more sophisticated path might be a curve
//orange
pathOrange = new Path();
pathOrange.addPoint(-20, height/2);
pathOrange.addPoint(50, height/2);
pathOrange.addPoint(60, 425);
pathOrange.addPoint(70, 550);
pathOrange.addPoint(90, 560);
pathOrange.addPoint(110, 575);
pathOrange.addPoint(690, 575);
pathOrange.addPoint(700, 560);
pathOrange.addPoint(710, 540);
pathOrange.addPoint(720, 450);
pathOrange.addPoint(730, 440);
pathOrange.addPoint(745, 450);
pathOrange.addPoint(750, 690);
pathOrange.addPoint(760, 700);
pathOrange.addPoint(780, 720);
pathOrange.addPoint(760, 765);
pathOrange.addPoint(750, height+20);
//purple1
pathP1 = new Path();
pathP1.addPoint(-20, height/2);
pathP1.addPoint(60, height/2);
pathP1.addPoint(70, 410);
pathP1.addPoint(960, 405);
pathP1.addPoint(width+20, height/2);
//purple2
pathP2 = new Path();
pathP2.addPoint(250, height+20);
pathP2.addPoint(250, 610); //turn right
pathP2.addPoint(260, 600);
pathP2.addPoint(420, 600);
pathP2.addPoint(430, 590);
pathP2.addPoint(430, 360);
pathP2.addPoint(440, 350);
pathP2.addPoint(720, 350);
pathP2.addPoint(730, 360);//turn down
pathP2.addPoint(730, 730);
pathP2.addPoint(740, 740);
pathP2.addPoint(845, 740);
pathP2.addPoint(850, 750);
pathP2.addPoint(850, height+20);
//green
pathG1 = new Path();
pathG1.addPoint(0, height +20);
pathG1.addPoint(5, 615);
// pathG1.addPoint(0, 600);turn right
pathG1.addPoint(15, 600);
pathG1.addPoint(175, 600);
//pathG1.addPoint(200, 600); turn up
pathG1.addPoint(200, 575);
pathG1.addPoint(200, 375);
// pathG1.addPoint(200, 350); turn right
pathG1.addPoint(215, 350);
pathG1.addPoint(730, 350);
pathG1.addPoint(width+20, 340);
//blue
pathB1 = new Path();
pathB1.addPoint(420,-20);
pathB1.addPoint(420, 0);
pathB1.addPoint(430, 70);
pathB1.addPoint(420, 350);//bottom point
pathB1.addPoint(380, 350);
pathB1.addPoint(380, 250);
pathB1.addPoint(290, 250);
pathB1.addPoint(290, 150);
pathB1.addPoint(260, 75);
pathB1.addPoint(400, 50);
pathB1.addPoint(400, -20);
//Q
pathQ = new Path();
pathQ.addPoint(10, height+20);
pathQ.addPoint(10, -20);
//Broad
pathBroad = new Path();
pathBroad.addPoint(-20, 350);
pathBroad.addPoint(730, 350);
pathBroad.addPoint(width+20, 340);
//Highway
pathHW = new Path();
pathHW.addPoint(-20, 50);
pathHW.addPoint(80, 55);
pathHW.addPoint(560, 70);
pathHW.addPoint(590, 100);
pathHW.addPoint(620, 200);
pathHW.addPoint(630, 300);
pathHW.addPoint(630, 400);
pathHW.addPoint(610, 500);
pathHW.addPoint(570, 600);
pathHW.addPoint(520, 700);
pathHW.addPoint(450, height+20);
pathHWR = new Path();
pathHWR.addPoint(465, height+20);
pathHWR.addPoint(525, 700);
pathHWR.addPoint(575, 600);
pathHWR.addPoint(615, 500);
pathHWR.addPoint(640, 400);
pathHWR.addPoint(645, 300);
pathHWR.addPoint(660, 200);
pathHWR.addPoint(665, 100);
pathHWR.addPoint(620, 60);
pathHWR.addPoint(80, 45);
pathHWR.addPoint(-20, 45);
}
public void keyPressed() {
if (key == ' ') {
debug = !debug;
}
}
//walmart destination (0, 150)`Preformatted text`
//class bus
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Path Following
// Vehicle class
class Vehicle {
// All the usual stuff
PVector position;
PVector velocity;
PVector acceleration;
float r;
float maxforce; // Maximum steering force
float maxspeed; // Maximum speed
// Constructor initialize all values
Vehicle( PVector l, float ms, float mf) {
position = l.get();
r = 2.0; //size of v
maxspeed = ms;
maxforce = mf;
acceleration = new PVector(0, 0);
velocity = new PVector(maxspeed, 0);
}
// Main "run" function
public void run() {
update();
display();
}
// This function implements Craig Reynolds' path following algorithm
// http://www.red3d.com/cwr/steer/PathFollow.html
void follow(Path p) {
// Predict position 50 (arbitrary choice) frames ahead
// This could be based on speed
PVector predict = velocity.get();
predict.normalize();
predict.mult(10); // moves off the line
PVector predictpos = PVector.add(position, predict);
// Now we must find the normal to the path from the predicted position
// We look at the normal for each line segment and pick out the closest one
PVector normal = null;
PVector target = null;
float worldRecord = 1000000; // Start with a very high record distance that can easily be beaten
// Loop through all points of the path
for (int i = 0; i < p.points.size()-1; i++) {
// Look at a line segment
PVector a = p.points.get(i);
PVector b = p.points.get(i+1);
// Get the normal point to that line
PVector normalPoint = getNormalPoint(predictpos, a, b);
// This only works because we know our path goes from left to right
// We could have a more sophisticated test to tell if the point is in the line segment or not
if (normalPoint.x < a.x || normalPoint.x > b.x) {
// This is something of a hacky solution, but if it's not within the line segment
// consider the normal to just be the end of the line segment (point b)
normalPoint = a.get();
}
// How far away are we from the path?
float distance = PVector.dist(predictpos, normalPoint);
// Did we beat the record and find the closest line segment?
if (distance < worldRecord) {
worldRecord = distance;
// If so the target we want to steer towards is the normal
normal = normalPoint;
// Look at the direction of the line segment so we can seek a little bit ahead of the normal
PVector dir = PVector.sub(b, a);
dir.normalize();
// This is an oversimplification
// Should be based on distance to path & velocity
dir.mult(10); //how far leader is ahead of dot
target = normalPoint.get();
target.add(dir);
}
}
// Only if the distance is greater than the path's radius do we bother to steer
if (worldRecord > p.radius) {
seek(target);
}
// Draw the debugging stuff
if (debug) {
// Draw predicted future position
stroke(0);
fill(0);
line(position.x, position.y, predictpos.x, predictpos.y);
ellipse(predictpos.x, predictpos.y, 4, 4);
// Draw normal position
stroke(0);
fill(0);
ellipse(normal.x, normal.y, 4, 4);
// Draw actual target (red if steering towards it)
line(predictpos.x, predictpos.y, normal.x, normal.y);
if (worldRecord > p.radius) fill(255, 0, 0);
noStroke();
ellipse(target.x, target.y, 8, 8);
}
}
// A function to get the normal point from a point (p) to a line segment (a-b)
// This function could be optimized to make fewer new Vector objects
PVector getNormalPoint(PVector p, PVector a, PVector b) {
// Vector from a to p
PVector ap = PVector.sub(p, a);
// Vector from a to b
PVector ab = PVector.sub(b, a);
ab.normalize(); // Normalize the line
// Project vector "diff" onto line by using the dot product
ab.mult(ap.dot(ab));
PVector normalPoint = PVector.add(a, ab);
return normalPoint;
}
// Method to update position
void update() {
// Update velocity
velocity.add(acceleration);
// Limit speed
velocity.limit(maxspeed);
position.add(velocity);
// Reset accelertion to 0 each cycle
acceleration.mult(0.2);
}
void applyForce(PVector force) {
// We could add mass here if we want A = F / M
acceleration.add(force);
}
// A method that calculates and applies a steering force towards a target
// STEER = DESIRED MINUS VELOCITY
void seek(PVector target) {
PVector desired = PVector.sub(target, position); // A vector pointing from the position to the target
// If the magnitude of desired equals 0, skip out of here
// (We could optimize this to check if x and y are 0 to avoid mag() square root
if (desired.mag() == 0) return;
// Normalize desired and 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
applyForce(steer);
}
void display() {
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading2D() + radians(90);
fill(255, 85, 0, 90);
noStroke();
pushMatrix();
translate(position.x, position.y);
rotate(theta);
/*
beginShape(PConstants.TRIANGLES);
vertex(0, -r*2);
vertex(-r, r*2);
vertex(r, r*2);
endShape();
*/
rect(0, 0, 5, 15);
popMatrix();
}
// Wraparound
void borders(Path p) {
if (position.x > p.getEnd().x + r) {
position.x = p.getStart().x - r;
position.y = p.getStart().y + (position.y-p.getEnd().y);
}
}
}
//class wind
// The Boid class
class Boid {
PVector position;
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);
// This is a new PVector method not yet implemented in JS
// velocity = PVector.random2D();
// Leaving the code temporarily this way so that this example runs in JS
float angle = random(TWO_PI);
velocity = new PVector(cos(angle), sin(angle));
//speed of wind lines
position = new PVector(x, y);
r = 2.0;
maxspeed = .5;
maxforce = 0.01;
}
void run(ArrayList<Boid> boids) {
flock(boids);
update();
borders();
render();
}
void applyForce(PVector force) {
// We could add mass here if we want A = F / M
acceleration.add(force);
}
// We accumulate a new acceleration each time based on three rules
void flock(ArrayList<Boid> boids) {
PVector sep = separate(boids); // Separation
PVector ali = align(boids); // Alignment
PVector coh = cohesion(boids); // Cohesion
// Arbitrarily weight these forces
sep.mult(4);
ali.mult(3);
coh.mult(.5);
// Add the force vectors to acceleration
applyForce(sep);
applyForce(ali);
applyForce(coh);
}
// Method to update position
void update() {
// Update velocity
velocity.add(acceleration);
// Limit speed
velocity.limit(maxspeed);
position.add(velocity);
// Reset accelertion to 0 each cycle
acceleration.mult(0);
}
// A method that calculates and applies a steering force towards a target
// STEER = DESIRED MINUS VELOCITY
PVector seek(PVector target) {
PVector desired = PVector.sub(target, position); // A vector pointing from the position to the target
// Scale to maximum speed
desired.normalize();
desired.mult(maxspeed);
// Above two lines of code below could be condensed with new PVector setMag() method
// Not using this method until Processing.js catches up
// desired.setMag(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);
// heading2D() above is now heading() but leaving old syntax until Processing.js catches up
fill(167, 227, 250);
noStroke();
pushMatrix();
translate(position.x, position.y);
rotate(theta);
/*
beginShape(TRIANGLES);
vertex(0, -r*2);
vertex(-r, r*2);
vertex(r, r*2);
endShape();
*/
rect(0, 0, .5, 10);
popMatrix();
}
// Wraparound
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;
}
// Separation
// Method checks for nearby boids and steers away
PVector separate (ArrayList<Boid> boids) {
float desiredseparation = 90.0f;
PVector steer = new PVector(0, 0, 0);
int count = 0;
// For every boid in the system, check if it's too close
for (Boid other : boids) {
float d = PVector.dist(position, other.position);
// 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(position, other.position);
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);
}
// As long as the vector is greater than 0
if (steer.mag() > 0) {
// First two lines of code below could be condensed with new PVector setMag() method
// Not using this method until Processing.js catches up
// steer.setMag(maxspeed);
// Implement Reynolds: Steering = Desired - Velocity
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 = 75;
PVector sum = new PVector(0, 0);
int count = 0;
for (Boid other : boids) {
float d = PVector.dist(position, other.position);
if ((d > 0) && (d < neighbordist)) {
sum.add(other.velocity);
count++;
}
}
if (count > 0) {
sum.div((float)count);
// First two lines of code below could be condensed with new PVector setMag() method
// Not using this method until Processing.js catches up
// sum.setMag(maxspeed);
// Implement Reynolds: Steering = Desired - Velocity
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 position (i.e. center) of all nearby boids, calculate steering vector towards that position
PVector cohesion (ArrayList<Boid> boids) {
float neighbordist = 70;
PVector sum = new PVector(0, 0); // Start with empty vector to accumulate all positions
int count = 0;
for (Boid other : boids) {
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);
}
}
}
// end of wind
// The Boid class
class Boid {
PVector position;
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);
// This is a new PVector method not yet implemented in JS
// velocity = PVector.random2D();
// Leaving the code temporarily this way so that this example runs in JS
float angle = random(TWO_PI);
velocity = new PVector(cos(angle), sin(angle));
//speed of wind lines
position = new PVector(x, y);
r = 2.0;
maxspeed = .5;
maxforce = 0.01;
}
void run(ArrayList<Boid> boids) {
flock(boids);
update();
borders();
render();
}
void applyForce(PVector force) {
// We could add mass here if we want A = F / M
acceleration.add(force);
}
// We accumulate a new acceleration each time based on three rules
void flock(ArrayList<Boid> boids) {
PVector sep = separate(boids); // Separation
PVector ali = align(boids); // Alignment
PVector coh = cohesion(boids); // Cohesion
// Arbitrarily weight these forces
sep.mult(4);
ali.mult(3);
coh.mult(.5);
// Add the force vectors to acceleration
applyForce(sep);
applyForce(ali);
applyForce(coh);
}
// Method to update position
void update() {
// Update velocity
velocity.add(acceleration);
// Limit speed
velocity.limit(maxspeed);
position.add(velocity);
// Reset accelertion to 0 each cycle
acceleration.mult(0);
}
// A method that calculates and applies a steering force towards a target
// STEER = DESIRED MINUS VELOCITY
PVector seek(PVector target) {
PVector desired = PVector.sub(target, position); // A vector pointing from the position to the target
// Scale to maximum speed
desired.normalize();
desired.mult(maxspeed);
// Above two lines of code below could be condensed with new PVector setMag() method
// Not using this method until Processing.js catches up
// desired.setMag(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);
// heading2D() above is now heading() but leaving old syntax until Processing.js catches up
fill(167, 227, 250);
noStroke();
pushMatrix();
translate(position.x, position.y);
rotate(theta);
/*
beginShape(TRIANGLES);
vertex(0, -r*2);
vertex(-r, r*2);
vertex(r, r*2);
endShape();
*/
rect(0, 0, .5, 10);
popMatrix();
}
// Wraparound
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;
}
// Separation
// Method checks for nearby boids and steers away
PVector separate (ArrayList<Boid> boids) {
float desiredseparation = 90.0f;
PVector steer = new PVector(0, 0, 0);
int count = 0;
// For every boid in the system, check if it's too close
for (Boid other : boids) {
float d = PVector.dist(position, other.position);
// 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(position, other.position);
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);
}
// As long as the vector is greater than 0
if (steer.mag() > 0) {
// First two lines of code below could be condensed with new PVector setMag() method
// Not using this method until Processing.js catches up
// steer.setMag(maxspeed);
// Implement Reynolds: Steering = Desired - Velocity
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 = 75;
PVector sum = new PVector(0, 0);
int count = 0;
for (Boid other : boids) {
float d = PVector.dist(position, other.position);
if ((d > 0) && (d < neighbordist)) {
sum.add(other.velocity);
count++;
}
}
if (count > 0) {
sum.div((float)count);
// First two lines of code below could be condensed with new PVector setMag() method
// Not using this method until Processing.js catches up
// sum.setMag(maxspeed);
// Implement Reynolds: Steering = Desired - Velocity
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 position (i.e. center) of all nearby boids, calculate steering vector towards that position
PVector cohesion (ArrayList<Boid> boids) {
float neighbordist = 70;
PVector sum = new PVector(0, 0); // Start with empty vector to accumulate all positions
int count = 0;
for (Boid other : boids) {
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);
}
}
}
// end of wind
// class path
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Path Following
class Path {
// A Path is an arraylist of points (PVector objects)
ArrayList<PVector> points;
// A path has a radius, i.e how far is it ok for the boid to wander off
float radius;
Path() {
// Arbitrary radius of 20
radius = 20;
points = new ArrayList<PVector>();
}
// Add a point to the path
void addPoint(float x, float y) {
PVector point = new PVector(x, y);
points.add(point);
}
PVector getStart() {
return points.get(0);
}
PVector getEnd() {
return points.get(points.size()-1);
}
// Draw the path
void display() {
// Draw thick line for radius
//stroke(175);
//strokeWeight(radius);
//noFill();
//beginShape();
//for (PVector v : points) {
// vertex(v.x, v.y);
//}
//endShape();
// Draw thin line for center of path
stroke(200);
strokeWeight(.5);
noFill();
beginShape();
for (PVector v : points) {
vertex(v.x, v.y);
}
endShape();
}
}