Adding a feature to a gravity simulator

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”.

1 Like

I think you just see, how the physics work.
If you just start at a random location for both objects, it is very likely that you will get an unstable orbit, which either pushes the smaller object away or it falls into the large star. That is what usually happens with asteroids entering our solar system.

Now, you might be lucky and the two objects will circle around itself for a long time. An ideal orbit will result in infinite circling in space as well. To let something slowly fall into the large star, you need a second force to work. Either there is something (on earth: air flow resistance), which slows the smaller object, so that it falls into the star or you have other objects orbiting which interfere with the small object or you have an event like another object hitting, which makes the small object fall into the large one. Without that and with the right orbit they might circle long “forever” (longer you can watch your sketch).

ok I’ll try that.

Do you have any ideas how to make the two circles not overlap?

Hi @CodeMasterX,

Have you considered using physics engine like box2D or collision detections?

I leave some links here:

Box2D

https://box2d.org/
https://www.youtube.com/watch?v=MsRROjQJxuo&ab_channel=TheCodingTrain

Collision detection

Collision Detection - Happy Coding
Collision Detection

Best regards

I don’t like box2D : ( that only leaves me with collision detection