src/boid.js
function flock(boids) {
        const averageVelocity = velocity.clone();
        const averagePosition = Vec2.get();
        let inSightCount = 0;
        for (let i = 0; i < boids.length; i++) {
            const b = boids[i];
            if (b !== boid && inSight(b)) {
                averageVelocity.add(b.velocity);
                averagePosition.add(b.position);

                if (position.distanceSq(b.position) < minDistanceSq) {
                    flee(b.position);
                }
                inSightCount++;
            }
        }
        if (inSightCount > 0) {
            averageVelocity.divideBy(inSightCount);
            averagePosition.divideBy(inSightCount);
            seek(averagePosition);
            steeringForce.add(averageVelocity.subtract(velocity));
        }
        averageVelocity.dispose();
        averagePosition.dispose();

        return boid;
    }
function flock(boids) {
        const averageVelocity = velocity.clone();
        const averagePosition = Vec2.get();
        let inSightCount = 0;
        for (let i = 0; i < boids.length; i++) {
            const b = boids[i];
            if (b !== boid && inSight(b)) {
                averageVelocity.add(b.velocity);
                averagePosition.add(b.position);

                if (position.distanceSq(b.position) < minDistanceSq) {
                    flee(b.position);
                }
                inSightCount++;
            }
        }
        if (inSightCount > 0) {
            averageVelocity.divideBy(inSightCount);
            averagePosition.divideBy(inSightCount);
            seek(averagePosition);
            steeringForce.add(averageVelocity.subtract(velocity));
        }
        averageVelocity.dispose();
        averagePosition.dispose();

        return boid;
    }
function flock ( boids ) { const averageVelocity = velocity . clone ( ) ; const averagePosition = Vec2 . get ( ) ; let inSightCount = 0 ; for ( let i = 0 ; i < boids . length ; i ++ ) { const b = boids [ i ] ; if ( b !== boid && inSight ( b ) ) { averageVelocity . add ( b . velocity ) ; averagePosition . add ( b . position ) ; if ( position . distanceSq ( b . position ) < minDistanceSq ) { flee ( b . position ) ; } inSightCount ++ ; } } if ( inSightCount > 0 ) { averageVelocity . divideBy ( inSightCount ) ; averagePosition . divideBy ( inSightCount ) ; seek ( averagePosition ) ; steeringForce . add ( averageVelocity . subtract ( velocity ) ) ; } averageVelocity . dispose ( ) ; averagePosition . dispose ( ) ; return boid ; }
flock - group of boids loosely move together
flock - group of boids loosely move together