import Vector from './vector.mjs'; const findNeighbors = (boids, boid, radius) => { const neighbors = []; for (let otherBoid of boids) { if (boid != otherBoid) { const dist = boid.pos.sub(otherBoid.pos).normSquared(); if (dist < radius * radius) { neighbors.push(otherBoid); } } } return neighbors; }; export const update = (boids, params, time) => { for (let boid of boids) { const visible = findNeighbors(boids, boid, params.visibleDist); const close = findNeighbors(boids, boid, params.closeDist); // Attract towards center of visible flock const center = new Vector(0, 0); for (let otherBoid of visible) { center.addMut(otherBoid.pos); } if (visible.length >= 1) { center.divMut(visible.length); boid.vel.addMut(center.sub(boid.pos).mul(params.centerAccel)); } // Attract toward center of screen boid.vel.addMut(boid.pos.mul(-0.01)); // Repel away from close boids for (let otherBoid of close) { boid.vel.addMut(boid.pos .sub(otherBoid.pos) .mul(params.repelAccel)); } // Match other boids’ velocity const velocity = new Vector(0, 0); for (let otherBoid of visible) { velocity.addMut(otherBoid.vel); } if (visible.length >= 1) { velocity.divMut(visible.length); boid.vel.addMut(velocity.sub(boid.vel).mul(params.matchAccel)); } // Do not surpass maximum speed const speed = boid.vel.normSquared(); if (speed > params.maxSpeed * params.maxSpeed) { boid.vel.divMut(Math.sqrt(speed)).mulMut(params.maxSpeed); } boid.pos.addMut(boid.vel.mul(time)); } };