UNPKG

@thi.ng/boids

Version:

n-dimensional boids simulation with modular behavior system

34 lines (33 loc) 847 B
import { __ensureFn } from "../internal/ensure.js"; const separation = (minDist, weight = 1, amp = () => 1) => { const $minDist = __ensureFn(minDist); const force = []; const delta = []; return { weight: __ensureFn(weight), update: (boid) => { const { maddN, magSq, setN, sub } = boid.api; const pos = boid.pos.curr; const neighbors = boid.neighbors($minDist(boid), pos); const num = neighbors.length; let n; setN(force, 0); for (let i = 0; i < num; i++) { n = neighbors[i]; if (n !== boid) { sub(delta, pos, n.pos.curr); maddN( force, delta, amp(boid, n) / (magSq(delta) + 1e-6), force ); } } return boid.averageForce(force, num - 1); } }; }; export { separation };