9

I'm very new to programming and I've gotten a school assignment for which I have to create a simulation of the earths orbit around the sun in p5.js . We were offered a simplified way to compute the gravitational pull but I wanted to use the actual formula (Fg = GMm/r^2). In my code, one pixel equals 10^9 meters or a million km. If I use the actual masses of both the sun and the earth, as well as the actual distance between them, I have to put the speed at which the earth travels at around 1 pixel per second or a million km/s, which is around 30,000 times the actual speed of the earth in orbit. My code:

x = 550;
y = 400;
vy = -1;
vx = 0;
dt = 1;
sunSize = 80;
planetSize = 10;
// 1 pixel equals 1 million km
canvasSize = 800;
starAmount = 600;

function setup() {
  frameRate(60);
  noStroke()
  solarXY = 0.5 * canvasSize;
  xSun = solarXY;
  ySun = solarXY;
  createCanvas(canvasSize, canvasSize);
  M = 1.989 * pow(10, 30);
  m = 5.972 * pow(10, 24);
  background(0);
  for (i = 0; i < starAmount; i++) {
    starX = Math.random() * canvasSize;
    starY = Math.random() * canvasSize;
    starSize = Math.random() * 3 + 1
    ellipse(starX, starY, starSize, starSize);
  }
  fill(255, 192, 0);
  ellipse(xSun, ySun, sunSize, sunSize);
}

function draw() {
  r = sqrt(sq(xSun - x) + sq(ySun - y)) * pow(10, 9);
  Fg = 6.67 * pow(10, -11) * m * M / sq(r);
  if (x >= xSun) {
    angle = atan((ySun - y) / (x - xSun));
  } else {
    angle = PI + atan((ySun - y) / (x - xSun));
  }
  xOld = x;
  yOld = y;
  Fgx = cos(angle) * Fg
  Fgy = sin(angle) * Fg
  ay = Fgy / m;
  ax = -Fgx / m;
  vy += ay * dt;
  vx += ax * dt;
  y += vy * dt;
  x += vx * dt;
  fill(30);
  ellipse(xOld, yOld, planetSize, planetSize);
  fill(0, 0, 192);
  ellipse(x, y, planetSize, planetSize);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.0.0/p5.min.js"></script>

Do you know what the cause could be? Thanks in advance.

Julian
  • 1,277
  • 1
  • 9
  • 20
Gijsfwb
  • 91
  • 3
  • 3
    I haven't fully understood the code yet, but it seems you are mixing two models of calculating the position of the earth. The Kepler model assumes the orbit is an ellipse and calculates the position from the orbital parameters, while the Newton model deals only in forces and the elliptic orbit is a result of that. In particular you calculate the gravitic force between two bodies but then "redirect" the force along the ellipse. You can switch to the Newton model alone, but that will bring some problems on its own. Beware of running into precision problems with large/small numbers. – Etienne Ott Mar 04 '20 at 13:20
  • Etienne Ott, I don't really understand what you mean by this. As far as I know the gravitational force can be split into an x and a y component based on the angle it has to the center of mass. I'm not sure if this is what you meant, as I'm not sure what the difference between the Kepler model and the Newton model is, so could you be a little more descriptive? – Gijsfwb Mar 04 '20 at 16:59
  • It seems I have misunderstood your code. When working with forces I usually don't bother with angles to split the forces into their components. It's easier to calculate the unit vector (of length 1) along the direction of the force, then multiply with the scalar that determines the strength of the force. Anyway, the code seems to do the right thing there, so let's ignore that. What I'm wondering now is the units of the variables. For example your timestep is 1, but the movement seems faster than advancing the simulation by 1 second per call. Presumably 1 does *not* mean 1 second. – Etienne Ott Mar 05 '20 at 08:30
  • The next thing I'd so is write the units down and carry them through the various calculations so see what the actual units of, say, the velocity is. Another thing is that the timestep is fixed, but it's not clear how often a timestep is called per real-time second so it's also not clear how long (meaning how many calls) the simulation should take to complete a year in the desired timeframe. You could make the simulation real time, but I don't think seeing one revolution per year is very interesting. ;) – Etienne Ott Mar 05 '20 at 08:33
  • dt is in seconds per frame, and I'm pretty sure that's correct, as all the units in my calculations use seconds. I'll try to see what the speed does and when, but to me it looks like it keeps more or less the same speed throughout the entire orbit – Gijsfwb Mar 06 '20 at 07:44

1 Answers1

6

You appear to have acceleration in meters per second 2, and velocity in pixels per second. Then you combine them:

vy += ay * dt;
vx += ax * dt;

Your gravitational accelerations are a billion times too strong. So your planet must move about 31,623 times faster than normal to keep a circular orbit.

William Miller
  • 9,839
  • 3
  • 25
  • 46
Beta
  • 96,650
  • 16
  • 149
  • 150