Skip to content

Commit

Permalink
fixes #199
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed Mar 31, 2023
1 parent 475ec96 commit 4ed6e2e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
14 changes: 11 additions & 3 deletions src/collide.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export default function(radius) {
xi,
yi,
ri,
ri2;
ri2,
ax,
ay;

for (var k = 0; k < iterations; ++k) {
tree = quadtree(nodes, x, y).visitAfter(prepare);
Expand All @@ -36,6 +38,12 @@ export default function(radius) {
xi = node.x + node.vx;
yi = node.y + node.vy;
tree.visit(apply);
if (ax) {
node.x += ax;
node.y += ay;
ax = 0;
ay = 0;
}
}
}

Expand All @@ -47,8 +55,8 @@ export default function(radius) {
y = yi - data.y - data.vy,
l = x * x + y * y;
if (l < r * r) {
if (x === 0) x = jiggle(random), l += x * x;
if (y === 0) y = jiggle(random), l += y * y;
if (x === 0) ax = jiggle(random), l += ax * ax;
if (y === 0) ay = jiggle(random), l += ay * ay;
l = (r - (l = Math.sqrt(l))) / l * strength;
node.vx += (x *= l) * (r = (rj *= rj) / (ri2 + rj));
node.vy += (y *= l) * r;
Expand Down
5 changes: 2 additions & 3 deletions test/collide-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ it("forceCollide jiggles equal positions", () => {
f.tick();
assert(a.x !== b.x);
assert(a.y !== b.y);
assert.strictEqual(a.vx, -b.vx);
assert.strictEqual(a.vy, -b.vy);
});

it("forceCollide jiggles in a reproducible way", () => {
const nodes = Array.from({length:10}, () => ({x: 0, y: 0}));
forceSimulation().nodes(nodes).force("collide", forceCollide()).stop().tick(50);
assertNodeEqual(nodes[0], {x: -5.371433857229194, y: -2.6644608278592576, index: 0, vy: 0, vx: 0});
assertNodeEqual(nodes[0], {x: -4.0299664949858, y: -7.540553942916919, index: 0, vy: 0, vx: 0});
assertNodeEqual(nodes[9], {x: 4.160131242078688, y: 0.2022776974891026, index: 9, vy: 0, vx: 0});
});

0 comments on commit 4ed6e2e

Please sign in to comment.