separate two circles by moving both circles

how do I separate two distinct circles by moving both of them, not just one? here’s my code I use for separating the circles by moving one of the circle. I don’t know much about vectors. ‘_’

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;
}

Answer

i found a working solution to this:

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;
}

Leave a Reply

Your email address will not be published. Required fields are marked *