mirror of
https://github.com/processing/processing4.git
synced 2026-01-26 18:01:07 +01:00
temporarily reverting some PVector methods to be compatible with processingjs.
This commit is contained in:
@@ -9,10 +9,17 @@ class Boid {
|
||||
float maxforce; // Maximum steering force
|
||||
float maxspeed; // Maximum speed
|
||||
|
||||
Boid(float x, float y) {
|
||||
acceleration = new PVector(0,0);
|
||||
velocity = PVector.random2D();
|
||||
location = new PVector(x,y);
|
||||
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));
|
||||
|
||||
location = new PVector(x, y);
|
||||
r = 2.0;
|
||||
maxspeed = 2;
|
||||
maxforce = 0.03;
|
||||
@@ -59,22 +66,30 @@ class Boid {
|
||||
// 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,location); // A vector pointing from the location to the target
|
||||
PVector desired = PVector.sub(target, location); // A vector pointing from the location to the target
|
||||
// Scale to maximum speed
|
||||
desired.setMag(maxspeed);
|
||||
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);
|
||||
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.heading() + radians(90);
|
||||
fill(200,100);
|
||||
float theta = velocity.heading2D() + radians(90);
|
||||
// heading2D() above is now heading() but leaving old syntax until Processing.js catches up
|
||||
|
||||
fill(200, 100);
|
||||
stroke(255);
|
||||
pushMatrix();
|
||||
translate(location.x,location.y);
|
||||
translate(location.x, location.y);
|
||||
rotate(theta);
|
||||
beginShape(TRIANGLES);
|
||||
vertex(0, -r*2);
|
||||
@@ -96,15 +111,15 @@ class Boid {
|
||||
// Method checks for nearby boids and steers away
|
||||
PVector separate (ArrayList<Boid> boids) {
|
||||
float desiredseparation = 25.0f;
|
||||
PVector steer = new PVector(0,0,0);
|
||||
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(location,other.location);
|
||||
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);
|
||||
PVector diff = PVector.sub(location, other.location);
|
||||
diff.normalize();
|
||||
diff.div(d); // Weight by distance
|
||||
steer.add(diff);
|
||||
@@ -118,8 +133,13 @@ class Boid {
|
||||
|
||||
// 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.setMag(maxspeed);
|
||||
steer.normalize();
|
||||
steer.mult(maxspeed);
|
||||
steer.sub(velocity);
|
||||
steer.limit(maxforce);
|
||||
}
|
||||
@@ -130,10 +150,10 @@ class Boid {
|
||||
// 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);
|
||||
PVector sum = new PVector(0, 0);
|
||||
int count = 0;
|
||||
for (Boid other : boids) {
|
||||
float d = PVector.dist(location,other.location);
|
||||
float d = PVector.dist(location, other.location);
|
||||
if ((d > 0) && (d < neighbordist)) {
|
||||
sum.add(other.velocity);
|
||||
count++;
|
||||
@@ -141,12 +161,19 @@ class Boid {
|
||||
}
|
||||
if (count > 0) {
|
||||
sum.div((float)count);
|
||||
sum.setMag(maxspeed);
|
||||
PVector steer = PVector.sub(sum,velocity);
|
||||
// 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);
|
||||
}
|
||||
else {
|
||||
return new PVector(0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,10 +181,10 @@ class Boid {
|
||||
// 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
|
||||
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);
|
||||
float d = PVector.dist(location, other.location);
|
||||
if ((d > 0) && (d < neighbordist)) {
|
||||
sum.add(other.location); // Add location
|
||||
count++;
|
||||
@@ -166,10 +193,10 @@ class Boid {
|
||||
if (count > 0) {
|
||||
sum.div(count);
|
||||
return seek(sum); // Steer towards the location
|
||||
} else {
|
||||
return new PVector(0,0);
|
||||
}
|
||||
else {
|
||||
return new PVector(0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user