function steerForAlignment (out, boid, maxDistance, boidFieldOfView, flock) {
    // steering accumulator and count of neighbors, both initially zero
    vec2.set(out, 0, 0)
    let neighbors = 0

    // for each of the other vehicles...
    const minDistance = boid.steering.radius * 3
    for (const other of flock) {
        if (inBoidNeighborhood(boid, other, minDistance, maxDistance, boidFieldOfView)) {
            // accumulate sum of neighbor's heading
            const forward = vec2.normalize([0,0], other.rigidBody.velocity)
            vec2.add(out, out, forward)

            // count neighbors
            neighbors++
        }
    }

    // divide by neighbors, subtract off current heading to get error-
    // correcting direction, then normalize to pure direction
    if (neighbors > 0) {
        const forward = vec2.normalize([0,0], boid.rigidBody.velocity)
        vec2.scale(out, out, 1 / neighbors)
        vec2.subtract(out, out, forward)
        vec2.normalize(out, out)
    }

    return out
}
// Alignment behavior: steer to head in same direction as neighbors
Alignment behavior: steer to head in same direction as neighbors
Alignment behavior : steer to head in same direction as neighbors
function steerForAlignment (out, boid, maxDistance, boidFieldOfView, flock) {
    vec2.set(out, 0, 0)
    let neighbors = 0
    const minDistance = boid.steering.radius * 3
    for (const other of flock) {
        if (inBoidNeighborhood(boid, other, minDistance, maxDistance, boidFieldOfView)) {
            const forward = vec2.normalize([0,0], other.rigidBody.velocity)
            vec2.add(out, out, forward)
            neighbors++
        }
    }
    if (neighbors > 0) {
        const forward = vec2.normalize([0,0], boid.rigidBody.velocity)
        vec2.scale(out, out, 1 / neighbors)
        vec2.subtract(out, out, forward)
        vec2.normalize(out, out)
    }
    return out
}
function steerForAlignment ( out , boid , maxDistance , boidFieldOfView , flock ) { vec2 . set ( out , 0 , 0 ) let neighbors = 0 const minDistance = boid . steering . radius * 3 for ( const other of flock ) { if ( inBoidNeighborhood ( boid , other , minDistance , maxDistance , boidFieldOfView ) ) { const forward = vec2 . normalize ( [ 0 , 0 ] , other . rigidBody . velocity ) vec2 . add ( out , out , forward ) neighbors ++ } } if ( neighbors > 0 ) { const forward = vec2 . normalize ( [ 0 , 0 ] , boid . rigidBody . velocity ) vec2 . scale ( out , out , 1 / neighbors ) vec2 . subtract ( out , out , forward ) vec2 . normalize ( out , out ) } return out }
Alignment behavior: steer to head in same direction as neighbors
Alignment behavior : steer to head in same direction as neighbors
// steering accumulator and count of neighbors, both initially zero // for each of the other vehicles... // accumulate sum of neighbor's heading // count neighbors // divide by neighbors, subtract off current heading to get error- // correcting direction, then normalize to pure direction