UNPKG

@thi.ng/boids

Version:

n-dimensional boids simulation with modular behavior system

26 lines (25 loc) 704 B
import { __ensureFn } from "../internal/ensure.js"; const alignment = (maxDist, weight = 1, amp) => { const $maxDist = __ensureFn(maxDist); const force = []; return { weight: __ensureFn(weight), update: (boid) => { const { add, maddN, setN } = boid.api; const neighbors = boid.neighbors($maxDist(boid), boid.pos.curr); const num = neighbors.length; let i, n; setN(force, 0); for (i = 0; i < num; i++) { n = neighbors[i]; if (n !== boid) { amp ? maddN(force, n.vel.curr, amp(boid, n), force) : add(force, force, n.vel.curr); } } return boid.averageForce(force, num - 1); } }; }; export { alignment };