如何通过移动它们来分隔两个不同的圆圈,而不只是一个?这是我用来通过移动其中一个圆圈来分隔圆圈的代码。我对矢量知之甚少。 ' _'
if (i == j)
return;
var ref1 = a; // a and b are circles
var ref2 = b;
var x = ref1.x - ref2.x;
var y = ref1.y - ref2.y;
var d = Math.hypot(x, y);
var r = toRadius(a.mass) + toRadius(b.mass);
if (d < r) {
x /= d;
y /= d;
ref2.x += (ref1.x - x * r - ref2.x) * 0.2;
ref2.y += (ref1.y - y * r - ref2.y) * 0.2;
}
答案 0 :(得分:0)
我找到了一个有效的解决方案:
if (i == j)
return;
var ref1 = a; // a and b are circles
var ref2 = b;
var x = ref1.x - ref2.x;
var y = ref1.y - ref2.y;
var d = Math.hypot(x, y);
var r = toRadius(a.mass) + toRadius(b.mass);
if (d < r) {
x /= d;
y /= d;
ref2.x += (ref1.x - x * r - ref2.x) * 0.2;
ref2.y += (ref1.y - y * r - ref2.y) * 0.2;
ref1.x += (ref2.x + x * r - ref1.x) * 0.2;
ref1.y += (ref2.y + y * r - ref1.y) * 0.2;
}