diff --git a/js/boid.js b/js/boid.js index f6b9468..411f82b 100644 --- a/js/boid.js +++ b/js/boid.js @@ -24,19 +24,25 @@ class Boid extends V2D { } neighbors(flock) { - const cand = flock.candidates(this); + const cands = flock.candidates(this); const ns = []; const ds = []; - let step = opt.accuracy === 0 ? 1 : Math.ceil(cand.length / opt.accuracy); + const candidate_count = cands.map(c => c.length).reduce((a, b) => a + b, 0); + let step = opt.accuracy === 0 ? 1 : Math.ceil(candidate_count / opt.accuracy); + let i = Math.floor(random(step)); - for (let i = Math.floor(random(step)); i < cand.length; i += step) { - if (!cand[i]) break; - const d = this.sqrDist(cand[i]); - if (d < g.sqVis && this !== cand[i]) { - ns.push(cand[i]); - ds.push(d); + for (const c of cands) { + for (; i < c.length; i += step) { + if (this === c[i]) continue; + + const d = this.sqrDist(c[i]); + if (d < g.sqVis) { + ns.push(c[i]); + ds.push(d); + } } + i -= c.length; } return [ns, ds]; diff --git a/js/flock.js b/js/flock.js index 4ff2861..962c1d4 100644 --- a/js/flock.js +++ b/js/flock.js @@ -102,9 +102,10 @@ class Flock { } _b(r, c, a) { - if (this.buckets[r]?.[c]) a.push(...this.buckets[r][c]); + if (this.buckets[r]?.[c]) a.push(this.buckets[r][c]); } + // Returns a list of lists of boids, where each sublist contains the boids in a nearby cell candidates(boid) { const cand = [];