Hi!

I started creating yet another project! It is a gravity simulator. It started out fine but when I added a feature that makes the two circles “solid” (they can’t overlap) it started going weird. It just bounces infinet

```
int m1 = 100, m2 = 1000;
float x = 100, y = 100, xm = 10, ym = 0.0, x2m = 0, y2m = 0, x2 = 300, y2 = 300, f = 0, fdir = 0, fm = 1/sqrt(m1), fm2 = 1/sqrt(m2), loss =0.9;
void setup() {
size(600,600);
}
void draw() {
background(0);
circle(x,y,sqrt(m1));
circle(x2,y2,sqrt(m2));
force();
xm += cos(fdir) * fm;
ym += sin(fdir) * fm;
x2m += cos(fdir) * fm2;
y2m += sin(fdir) * fm2;
if(dist(x,y,x2,y2) < sqrt(m1)/2+sqrt(m2)/2) {
xm = 0;
ym = 0;
x2m = 0;
y2m = 0;
float a = getRotation(x,y,x2,y2);
xm -= cos(a);
ym -= sin(a);
}
x += xm;
y += ym;
x2 -= x2m;
y2 -= y2m;
if(x + sqrt(m1)/2 > width) {
x = width - sqrt(m1)/2;
xm = 0;
}
if(x - sqrt(m1)/2 < 0) {
x = sqrt(m1)/2;
xm = 0;
}
if(y + sqrt(m1)/2 > height) {
y = height - sqrt(m1)/2;
ym = 0;
}
if(y - sqrt(m1)/2 < 0) {
y = sqrt(m1)/2;
ym = 0;
}
}
void force() {
f = m1 * m2 / pow(dist(x,y,x2,y2),2)*loss;
fdir = getRotation(x,y,x2,y2);
}
float getRotation(float p1x, float p1y, float p2x, float p2y) {
float temp = atan2(p2y-p1y,p2x-p1x);
return(temp);
}
void mouseDragged() {
x2 = mouseX;
y2 = mouseY;
x2m = 0;
y2m = 0;
}
```

or a way I found trying to fix it:

```
int m1 = 100, m2 = 1000;
float x = 100, y = 100, xm = 10, ym = 0.0, x2m = 0, y2m = 0, x2 = 300, y2 = 300, f = 0, fdir = 0, fm = 1/sqrt(m1), fm2 = 1/sqrt(m2), loss =0.9;
void setup() {
size(600,600);
}
void draw() {
background(0);
circle(x,y,sqrt(m1));
circle(x2,y2,sqrt(m2));
force();
xm += cos(fdir) * fm;
ym += sin(fdir) * fm;
x2m += cos(fdir) * fm2;
y2m += sin(fdir) * fm2;
if(dist(x,y,x2,y2) < sqrt(m1)/2+sqrt(m2)/2) {
xm = 0;
ym = 0;
x2m = 0;
y2m = 0;
float a = getRotation(x,y,x2,y2);
xm -= cos(a);
ym -= sin(a);
}
x += xm;
y += ym;
x2 -= x2m;
y2 -= y2m;
if(x + sqrt(m1)/2 > width) {
x = width - sqrt(m1)/2;
xm = 0;
}
if(x - sqrt(m1)/2 < 0) {
x = sqrt(m1)/2;
xm = 0;
}
if(y + sqrt(m1)/2 > height) {
y = height - sqrt(m1)/2;
ym = 0;
}
if(y - sqrt(m1)/2 < 0) {
y = sqrt(m1)/2;
ym = 0;
}
}
void force() {
f = m1 * m2 / pow(dist(x,y,x2,y2),2)*loss;
fdir = getRotation(x,y,x2,y2);
}
float getRotation(float p1x, float p1y, float p2x, float p2y) {
float temp = atan2(p2y-p1y,p2x-p1x);
return(temp);
}
void mouseDragged() {
x2 = mouseX;
y2 = mouseY;
x2m = 0;
y2m = 0;
}
```

where it works “fine” or relatively fine until both objects move In the same direction where one “eats” the other. Do you have any idea how to fix it?

And how to implement a loss into movement? Like two stars will slowly collide no matter how perfect their binary orbit? (a variable determining how much kinetic energy is lost. Closer it is to 1 (default between 0 and 1) the less energy is “wasted”.