Tut­o­r­i­a­l 7

by Sonia White
  • Show Sketch
/** @peep sketchcode */
 
Flock flock;
 
void setup() {
  size(400,400);
  colorMode(RGB,255,255,255,100);
  flock = new Flock(10, 50);
  for (int i = 0; i < 10; i++) {
    flock.addBoid(width/2, height/2);
  }
}
 
void draw() {
  background(210,240,255);
  flock.run();
}
 
// Add a new boid into the System
void mousePressed() {
  flock.addBoid(mouseX, mouseY);
}
 
class Flock {
  ArrayList boids; // An arraylist for all the boids
  float maxspeed = 1.5;
  float maxforce = 0.02;
  float desiredseparation = 100.0;
  float neighbordist = 50.0;
 
  Flock(float _desiredseparation, float _neighbordist) {
    desiredseparation = _desiredseparation;
    neighbordist = _neighbordist;
    boids = new ArrayList(); // Initialize the arraylist
  }
 
  void run() {
    for (int i = 0; i < boids.size(); i++) {
      Boid b = (Boid) boids.get(i);  
      b.update();
      b.render();
    }
  }
 
  void addBoid(float x, float y) {
    Boid b = new Boid(new PVector(x, y), this);
    boids.add(b);
  }
}
 
 
class Boid {
  PVector loc;
  PVector vel;
  PVector acc;
  float radius;
  Flock flock;
  int R=random(100,200);
  int G=random(100,255);
  int B=random(255);
 
  Boid(PVector _location, Flock _flock) {
    acc = new PVector(0,0);
    vel = new PVector(random(-1,1),random(-1,1));
    loc = new PVector(_location.x, _location.y);
    radius = 2.0f;
    flock = _flock;
  }
 
  void update() {
    acc.set(0,0,0); // Reset accelertion to 0 each cycle
    // We accumulate a new acceleration each time based on three rules
    PVector sep = separate();   // Separation
    PVector ali = align();      // Alignment
    PVector coh = cohesion();   // Cohesion
    // Arbitrarily weight these forces
    sep.mult(2.0f);
    ali.mult(1.0f);
    coh.mult(1.0f);
    // Add the force vectors to acceleration
    acc.add(sep);
    acc.add(ali);
    acc.add(coh);
 
    vel.add(acc); // Update velocity
    vel.limit(flock.maxspeed); // Limit speed
    loc.add(vel); // Update location
 
    // Wraparound borders
    if (loc.x < -radius) loc.x = width+radius;
    if (loc.y < -radius) loc.y = height+radius;
    if (loc.x > width+radius) loc.x = -radius;
    if (loc.y > height+radius) loc.y = -radius;
  }
 
  void render() {
    // Draw a triangle rotated in the direction of velocity
    float theta = atan2(vel.y, vel.x);
    fill(R,G,B);
    stroke(R,G,B);
    pushMatrix();
    translate(loc.x,loc.y);
    rotate(theta);
    triangle(radius*random(2,10), 0, -radius*random(2,10), radius, -radius*random(2,10), -radius*random(2,10));
    popMatrix();
  }
 
  // Separation
  // Method checks for nearby boids and steers away
  PVector separate() {
    PVector sum = new PVector(0, 0, 0);
    PVector diff = new PVector(0, 0, 0);
    int count = 0;
    // For every boid in the system, check if it's too close
    for (int i = 0 ; i < flock.boids.size(); i++) {
      Boid other = (Boid) flock.boids.get(i);
      if (this != other) {
        float d = PVector.dist(loc, other.loc);
        // If the distance is greater than 0 and less than an arbitrary amount
        if ((d > 0) && (d < flock.desiredseparation)) {
          // Calculate vector pointing away from neighbor
          diff.set(loc.x, loc.y, loc.z);
          diff.sub(other.loc);
          diff.normalize();
          diff.div(d);        // Weight by distance
          sum.add(diff);
          count++;            // Keep track of how many
        }
      }
    }
    // Average -- divide by how many
    if (count > 0) {
      sum.div(count);
    }
    return sum;
  }
 
  // Alignment
  // For every nearby boid in the system, calculate the average velocity
  PVector align() {
    PVector sum = new PVector(0,0,0);
    int count = 0;
    for (int i = 0 ; i < flock.boids.size(); i++) {
      Boid other = (Boid) flock.boids.get(i);
      if (this != other) {
        float d = PVector.dist(loc, other.loc);
        if ((d > 0) && (d < flock.neighbordist)) {
          sum.add(other.vel);
          count++;
        }
      }
    }
    if (count > 0) {
      sum.div(count);
      sum.limit(flock.maxforce);
    }
    return sum;
  }
 
  // Cohesion
  // For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
  PVector cohesion() {
    PVector sum = new PVector(0,0,0);   // Start with empty vector to accumulate all locations
    int count = 0;
    for (int i = 0 ; i < flock.boids.size(); i++) {
      Boid other = (Boid) flock.boids.get(i);
      float d = PVector.dist(loc, other.loc);
      if ((d > 0) && (d < flock.neighbordist)) {
        sum.add(other.loc); // Add location
        count++;
      }
    }
    if (count > 0) {
      sum.div((float)count);
      return steer(sum,false);  // Steer towards the location
    }
    return sum;
  }
 
  // A method that calculates a steering vector towards a target
  // Takes a second argument, if true, it slows down as it approaches the target
  PVector steer(PVector target, boolean slowdown) {
    PVector steer = PVector.sub(target,loc);  // A vector pointing from the location to the target
    float d = steer.mag(); // Distance from the target is the magnitude of the vector
    // If the distance is greater than 0, calc steering (otherwise return zero vector)
    if (d > 0) {
      // Normalize desired
      steer.normalize();
      // Two options for desired vector magnitude (1 -- based on distance, 2 -- maxspeed)
      if ((slowdown) && (d < 100.0f)) steer.mult(maxspeed*(d/100.0f)); // This damping is somewhat arbitrary
      else steer.mult(flock.maxspeed);
      // Steering = Desired minus Velocity
      steer.sub(vel);
      steer.limit(flock.maxforce);  // Limit to maximum steering force
    } else {
      steer.set(0,0);
    }
    return steer;
  }
}

Comments

Nobody has said anything yet.