UNPKG

@thi.ng/boids

Version:

n-dimensional boids simulation with modular behavior system

19 lines (18 loc) 590 B
import { Sampler } from "@thi.ng/geom-resample/sampler"; import { fract } from "@thi.ng/math/prec"; import { __ensureFn } from "../internal/ensure.js"; const followPolyline = (points, closed, lookahead = 0.01, weight = 1) => { const sampler = new Sampler(points, closed); return { weight: __ensureFn(weight), update: (boid) => { const t = sampler.closestT(boid.pos.curr); if (t === void 0 || !closed && t + lookahead > 1) return boid.api.ZERO; return boid.steerTowards(sampler.pointAt(fract(t + lookahead))); } }; }; export { followPolyline };