function separate (boids) {
    let currentSep = Math.max(0.9, sepSine) * 200 + sepRange - (200 * 0.9);
    let desiredseparation = currentSep;
    let steer = new THREE.Vector3();
    let count = 0;
    // For every boid in the system, check if it's too close
    for (let i = 0; i < boids.length; i++) {
      let d = obj.position.distanceTo(boids[i].getObj().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
        let other = boids[i].getObj();
        let diff = new THREE.Vector3(obj.position.x - other.position.x, obj.position.y - other.position.y, obj.position.z - other.position.z);
        diff.normalize();
        diff.divideScalar(d);        // Weight by distance
        steer.add(diff);
        count++;            // Keep track of how many
      }
    }
    // Average -- divide by how many
    if (count > 0) {
      steer.divideScalar(count);
    }

    // As long as the vector is greater than 0
    if (steer.length() > 0) {
      // Implement Reynolds: Steering = Desired - Velocity
      steer.normalize();
      steer.multiplyScalar(maxspeed);
      steer.sub(velocity);
      steer.clampLength(0, maxforce);
    }
    return steer;
  }
// Method checks for nearby boids and steers away
Method checks for nearby boids and steers away
Method checks for nearby boids and steers away
function separate (boids) {
    let currentSep = Math.max(0.9, sepSine) * 200 + sepRange - (200 * 0.9);
    let desiredseparation = currentSep;
    let steer = new THREE.Vector3();
    let count = 0;
    for (let i = 0; i < boids.length; i++) {
      let d = obj.position.distanceTo(boids[i].getObj().position);
      if ((d > 0) && (d < desiredseparation)) {
        let other = boids[i].getObj();
        let diff = new THREE.Vector3(obj.position.x - other.position.x, obj.position.y - other.position.y, obj.position.z - other.position.z);
        diff.normalize();
        diff.divideScalar(d);        
        steer.add(diff);
        count++;            
      }
    }
    if (count > 0) {
      steer.divideScalar(count);
    }
    if (steer.length() > 0) {
      steer.normalize();
      steer.multiplyScalar(maxspeed);
      steer.sub(velocity);
      steer.clampLength(0, maxforce);
    }
    return steer;
  }
function separate ( boids ) { let currentSep = Math . max ( 0.9 , sepSine ) * 200 + sepRange - ( 200 * 0.9 ) ; let desiredseparation = currentSep ; let steer = new THREE . Vector3 ( ) ; let count = 0 ; for ( let i = 0 ; i < boids . length ; i ++ ) { let d = obj . position . distanceTo ( boids [ i ] . getObj ( ) . position ) ; if ( ( d > 0 ) && ( d < desiredseparation ) ) { let other = boids [ i ] . getObj ( ) ; let diff = new THREE . Vector3 ( obj . position . x - other . position . x , obj . position . y - other . position . y , obj . position . z - other . position . z ) ; diff . normalize ( ) ; diff . divideScalar ( d ) ; steer . add ( diff ) ; count ++ ; } } if ( count > 0 ) { steer . divideScalar ( count ) ; } if ( steer . length ( ) > 0 ) { steer . normalize ( ) ; steer . multiplyScalar ( maxspeed ) ; steer . sub ( velocity ) ; steer . clampLength ( 0 , maxforce ) ; } return steer ; }
Method checks for nearby boids and steers away
Method checks for nearby boids and steers away
// For every boid in the system, check if it's too close // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself) // Calculate vector pointing away from neighbor // Weight by distance // Keep track of how many // Average -- divide by how many // As long as the vector is greater than 0 // Implement Reynolds: Steering = Desired - Velocity