mirror of
https://github.com/processing/processing4.git
synced 2026-02-12 01:50:44 +01:00
100 lines
2.4 KiB
Plaintext
100 lines
2.4 KiB
Plaintext
// Separation
|
|
// Daniel Shiffman <http://www.shiffman.net>
|
|
// The Nature of Code, 2011
|
|
|
|
// Vehicle class
|
|
|
|
class Vehicle {
|
|
|
|
// All the usual stuff
|
|
PVector location;
|
|
PVector velocity;
|
|
PVector acceleration;
|
|
float r;
|
|
float maxforce; // Maximum steering force
|
|
float maxspeed; // Maximum speed
|
|
|
|
// Constructor initialize all values
|
|
Vehicle(float x, float y) {
|
|
location = new PVector(x, y);
|
|
r = 12;
|
|
maxspeed = 3;
|
|
maxforce = 0.2;
|
|
acceleration = new PVector(0, 0);
|
|
velocity = new PVector(0, 0);
|
|
}
|
|
|
|
void applyForce(PVector force) {
|
|
// We could add mass here if we want A = F / M
|
|
acceleration.add(force);
|
|
}
|
|
|
|
// Separation
|
|
// Method checks for nearby vehicles and steers away
|
|
void cohesion (ArrayList<Vehicle> vehicles) {
|
|
float desiredseparation = r*2;
|
|
PVector sum = new PVector();
|
|
int count = 0;
|
|
// For every boid in the system, check if it's too close
|
|
for (Vehicle other : vehicles) {
|
|
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 > desiredseparation) {
|
|
// Calculate vector pointing away from neighbor
|
|
PVector diff = PVector.sub(location, other.location);
|
|
diff.normalize();
|
|
diff.mult(-d); // Weight by distance
|
|
sum.add(diff);
|
|
count++; // Keep track of how many
|
|
}
|
|
}
|
|
// Average -- divide by how many
|
|
if (count > 0) {
|
|
sum.div(count);
|
|
// Our desired vector is the average scaled to maximum speed
|
|
sum.normalize();
|
|
sum.mult(maxspeed);
|
|
// Implement Reynolds: Steering = Desired - Velocity
|
|
PVector steer = PVector.sub(sum, velocity);
|
|
steer.limit(maxforce);
|
|
applyForce(steer);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Method to update location
|
|
void update() {
|
|
// Update velocity
|
|
velocity.add(acceleration);
|
|
// Limit speed
|
|
velocity.limit(maxspeed);
|
|
location.add(velocity);
|
|
// Reset accelertion to 0 each cycle
|
|
acceleration.mult(0);
|
|
}
|
|
|
|
void display() {
|
|
fill(175);
|
|
stroke(0);
|
|
pushMatrix();
|
|
translate(location.x, location.y);
|
|
ellipse(0, 0, r, r);
|
|
popMatrix();
|
|
}
|
|
|
|
// Wraparound
|
|
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;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|