From 258b35d1a2f1fe7407098a95e31004557ea4577c Mon Sep 17 00:00:00 2001 From: Vasco Asturiano Date: Fri, 23 Jun 2017 00:35:55 -0700 Subject: [PATCH] Correct weighted average in barnes-hut coordinates --- src/manyBody.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/manyBody.js b/src/manyBody.js index d9887c9..8dbf5c8 100644 --- a/src/manyBody.js +++ b/src/manyBody.js @@ -26,17 +26,17 @@ export default function() { } function accumulate(quad) { - var strength = 0, q, c, x, y, i; + var strength = 0, q, c, sumC = 0, x, y, i; // For internal nodes, accumulate forces from child quadrants. if (quad.length) { for (x = y = i = 0; i < 4; ++i) { - if ((q = quad[i]) && (c = q.value)) { - strength += c, x += c * q.x, y += c * q.y; + if ((q = quad[i]) && (c = Math.abs(q.value))) { + strength += q.value, sumC += c, x += c * q.x, y += c * q.y; } } - quad.x = x / strength; - quad.y = y / strength; + quad.x = x / sumC; + quad.y = y / sumC; } // For leaf nodes, accumulate forces from coincident quadrants.