-
-
Save koji/8cc2d3d087c465f4fa32220e207862fd to your computer and use it in GitHub Desktop.
nature of code example for flocking in 3D.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import peasy.*; | |
Flock flock; | |
int boundary = 500; | |
PeasyCam cam; | |
// GUI | |
import controlP5.*; | |
int sliderValue = 100; | |
ControlP5 cp5; | |
void setup() { | |
size(1024, 768, P3D); | |
background(0); | |
flock = new Flock(); | |
// Add an initial set of boids into the system | |
for (int i = 0; i < 200; i++) { | |
Boid b = new Boid(0,0,0); | |
flock.addBoid(b); | |
} | |
// setup camera | |
cam = new PeasyCam(this,0,0,0,1500); | |
cam.setMinimumDistance(50); | |
cam.setMaximumDistance(1500); | |
} | |
void draw() { | |
background(0); | |
flock.run(); | |
// draw the bounding box | |
pushMatrix(); | |
noFill(); | |
stroke(255,100); | |
box(boundary*2); | |
popMatrix(); | |
//println(frameRate); | |
} | |
// Add a new boid into the System | |
void mouseDragged() { | |
//flock.addBoid(new Boid(0,0,0)); | |
} | |
void keyPressed(){ | |
if (key == CODED) { | |
if (keyCode == UP) { | |
for (int i = 0; i < 10; i++){ | |
flock.addBoid(new Boid(0,0,0)); | |
} | |
} | |
} | |
} | |
class Boid { | |
PVector position; | |
PVector velocity; | |
PVector acceleration; | |
float r; | |
float maxforce; // Maximum steering force | |
float maxspeed; // Maximum speed | |
Boid(float x, float y, float z) { | |
acceleration = new PVector(0,0,0); | |
float angle1 = random(PI); | |
float angle2 = random(TWO_PI); | |
velocity = new PVector(sin(angle1) * cos(angle2), sin(angle1) * sin(angle2), cos(angle1)); | |
position = new PVector(x,y,z); | |
r = 3.0; | |
maxspeed = 3; | |
maxforce = 0.05; | |
} | |
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(1.5); | |
ali.mult(0.8); | |
coh.mult(1.0); | |
// 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 | |
// 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 | |
return steer; | |
} | |
void render() { | |
// Draw a triangle rotated in the direction of velocity | |
//float theta = velocity.heading() + radians(90); | |
fill(255); | |
noStroke(); | |
pushMatrix(); | |
translate(position.x,position.y, position.z); | |
sphereDetail(1); | |
sphere(3); | |
popMatrix(); | |
} | |
// Wraparound | |
void borders() { | |
PVector desired = null; | |
if (position.x < -boundary) desired = new PVector(maxspeed, velocity.y, velocity.z); | |
if (position.x > boundary) desired = new PVector(-maxspeed, velocity.y, velocity.z); | |
if (position.y < -boundary) desired = new PVector(velocity.x, maxspeed, velocity.z); | |
if (position.y > boundary) desired = new PVector(velocity.x, -maxspeed, velocity.z); | |
if (position.z < -boundary) desired = new PVector(velocity.x, velocity.y, maxspeed); | |
if (position.z > boundary) desired = new PVector(velocity.x, velocity.y, -maxspeed); | |
if (desired != null) { | |
desired.normalize(); | |
desired.mult(maxspeed); | |
PVector steer = PVector.sub(desired, velocity); | |
steer.limit(maxforce); | |
applyForce(steer); | |
} | |
} | |
// Separation | |
// Method 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; | |
// 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) { | |
// 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 = 50; | |
PVector sum = new PVector(0,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); | |
sum.normalize(); | |
sum.mult(maxspeed); | |
PVector steer = PVector.sub(sum,velocity); | |
steer.limit(maxforce); | |
return steer; | |
} else { | |
return new PVector(0,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 = 50; | |
PVector sum = new PVector(0,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,0); | |
} | |
} | |
} | |
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