@thi.ng/boids
Version:
n-dimensional boids simulation with modular behavior system
26 lines (25 loc) • 672 B
JavaScript
import { closestPointPolyline } from "@thi.ng/geom-closest-point";
import { __ensureFn } from "../internal/ensure.js";
const attractPolyline = (points, closed, lookahead = 1, weight = 1) => {
const closest = [];
const pos = [];
return {
weight: __ensureFn(weight),
update: (boid) => {
const { add, normalize } = boid.api;
return closestPointPolyline(
lookahead !== 0 ? add(
pos,
normalize(pos, boid.vel.curr, lookahead),
boid.pos.curr
) : boid.pos.curr,
points,
closed,
closest
) ? boid.steerTowards(closest) : boid.api.ZERO;
}
};
};
export {
attractPolyline
};