@thi.ng/boids
Version:
n-dimensional boids simulation with modular behavior system
29 lines (28 loc) • 761 B
JavaScript
import { __ensureFn } from "../internal/ensure.js";
const separation = (minDist, weight = 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, 1 / (magSq(delta) + 1e-6), force);
}
}
return boid.computeSteer(force, num - 1);
}
};
};
export {
separation
};