boids/scripts/boid.js
/**
   * Steer this boid away from other nearby boids
   * @param {*} boidsInView
   */
Steer this boid away from other nearby boids
@param {*} boidsInView
Steer this boid away from other nearby boids @param { * } boidsInView
separate(boidsInView) {
    let steering = boidsInView.reduce((steering, otherBoid) => {
      const d = dist(this.pos.x, this.pos.y, otherBoid.pos.x, otherBoid.pos.y);
      steering.add(p5.Vector.sub(this.pos, otherBoid.pos).div(d * d));
      return steering;
    }, createVector()); 
    if (boidsInView.length > 0) {
      steering.div(boidsInView.length);
      steering.setMag(maxSpeed);
      steering.sub(this.vel);
      steering.limit(maxForce);
    }
    return steering;
  }
separate ( boidsInView ) { let steering = boidsInView . reduce ( ( steering , otherBoid ) => { const d = dist ( this . pos . x , this . pos . y , otherBoid . pos . x , otherBoid . pos . y ) ; steering . add ( p5 . Vector . sub ( this . pos , otherBoid . pos ) . div ( d * d ) ) ; return steering ; } , createVector ( ) ) ; if ( boidsInView . length > 0 ) { steering . div ( boidsInView . length ) ; steering . setMag ( maxSpeed ) ; steering . sub ( this . vel ) ; steering . limit ( maxForce ) ; } return steering ; }
Steer this boid away from other nearby boids
 @param {*} boidsInView
Steer this boid away from other nearby boids @param { * } boidsInView
// Go through each of the other boids // Calculate the distance between this boid and the other boid // Add to the steering sum the vector distance divided by the linear distance squared. Why? I don't know // Initial value for steering