UNPKG

@thi.ng/boids

Version:

n-dimensional boids simulation with modular behavior system

23 lines (22 loc) 809 B
import { rotate } from "@thi.ng/vectors/rotate"; import { __ensureFn } from "../internal/ensure.js"; const braitenberg2 = (field, lookahead, angle, weight = 1) => { const dir = []; const left = []; const right = []; return { weight: __ensureFn(weight), update: (boid) => { const { add, maddN, normalize } = boid.api; normalize(dir, boid.vel.curr, lookahead); const pos = boid.pos.curr; const valC = field(pos); const valL = field(add(left, rotate(left, dir, angle), pos)); const valR = field(add(right, rotate(right, dir, -angle), pos)); return valC > valL && valC > valR ? boid.steerTowards(maddN(left, boid.vel.curr, -1, pos)) : valL === valR ? boid.api.ZERO : boid.steerTowards(valL > valR ? left : right); } }; }; export { braitenberg2 };