@thi.ng/boids
Version:
n-dimensional boids simulation with modular behavior system
23 lines (22 loc) • 610 B
JavaScript
import { __ensureFn } from "../internal/ensure.js";
const alignment = (maxDist, weight = 1) => {
const $maxDist = __ensureFn(maxDist);
const force = [];
return {
weight: __ensureFn(weight),
update: (boid) => {
const { add, setN } = boid.api;
const neighbors = boid.neighbors($maxDist(boid), boid.pos.curr);
const num = neighbors.length;
setN(force, 0);
for (let i = 0; i < num; i++) {
const n = neighbors[i];
if (n !== boid) add(force, force, n.vel.curr);
}
return boid.computeSteer(force, num - 1);
}
};
};
export {
alignment
};