From 2b977a96e0da92e2358e8a80f50a3283c64a9e6b Mon Sep 17 00:00:00 2001 From: Nathan van der Kamp Date: Sat, 16 Nov 2024 15:00:07 +0100 Subject: [PATCH] Avoid overhead of pushing many items to candidates list This prevents the framerate from dropping significantly when many boids are close to eachother and/or their vision range is large. --- js/boid.js | 22 ++++++++++++++-------- js/flock.js | 3 ++- 2 files changed, 16 insertions(+), 9 deletions(-) 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 = [];