1

Good day, I am trying to create a simple 2D solar system model in javascript, but am having some trouble understanding how to go about calculating where planets will be for the next frame, aswell as a few other bits which I'll go into detail with soon.

After watching this very nice video and a whole bunch of his others, I made a quick MS paint image to try and simplify my situation. With the second scene, you can see that the new position is calulated using the velocity, gravitational pull, and the angle between these two 'directions'?

enter image description here

I cannot get my head around how to figure this all out.

Below is a JS fiddle of my code. You'll notice I'm trying my best to use real NASA given data to keep it accurate.

You'll want to look specifically at lines 138 which is where all the calculations for its next move are made.

https://jsfiddle.net/c8eru7mk/9/

attraction: function(p2) {
    // Distance to other body
    var dx = p2.position.x - this.position.x;
    var dy = p2.position.y - this.position.y;
    var d = Math.sqrt(dx ** 2 + dy ** 2); // Possibly correct

    // Force of attracrtion
    this.f = G * (this.mass * p2.mass) / (d ** 2); // Possibly Correct

    // Direction of force, If you read it hard enough you should be able to hear my screams of pain
    // Not sure if this is correct, most likely not.
    var theta = Math.atan2(dy, dx);
    var fx = Math.cos(theta) * this.f;
    var fy = Math.sin(theta) * this.f;

    this.velocity.x += fx / this.mass;
    this.velocity.y += fy / this.mass;

    this.position.x += this.velocity.x;
    this.position.y += this.velocity.y;
}

The problems I'm currently facing are

  • If I am to use NASA values, the distance between planets is so big, they won't fit on the screen, and I can't simply scale the distances down by multiplying them by 0.0002 or whatever, as that'll mess with the gravitational constant, and the simulation will be completely off.
  • I have no idea how to caluclate the next position and my brain has imploded several times this past week trying to attempt it several times.
  • I have no idea on how to check if my configuration data of planets is wrong, or if the simulation is wrong, so I'm pretty much just guessing.

This is also my first time actually coding anything more complex than a button in javascript too, so feedback on code layout and whatnot is welcome!

Many thanks

Slopax
  • 121
  • 2
  • 12
  • 2
    Going from a button to modeling the universe is quite a large leap! You might want to approach this a bit more piece-meal -- try to model just one body with gravity, then two bodies without gravity, then two bodies with gravity, etc... You could also look at 2d physics libraries in JavaScript, which have implemented these forces already. – duhaime Aug 30 '18 at 21:26
  • There is a nice series of videos on YouTube about simulating forces using the p5.js library in JavaScript. [This one](https://www.youtube.com/watch?v=fML1KpvvQTc) is all about gravity. – John Coleman Aug 30 '18 at 23:12
  • Just to address your first bullet-point: you need to scale the masses down, but *not* by the same factor 0.0002 or whatever. You want the planets to maintain the same orbital period. Take a look at [Kepler's 3rd law](https://en.wikipedia.org/wiki/Kepler%27s_laws_of_planetary_motion#Third_law_of_Kepler). In order to keep `T` constant, masses have to scale as the *cube root* of distances. – meowgoesthedog Aug 31 '18 at 08:00
  • Take a look at: [Is it possible to make realistic n-body solar system simulation in matter of size and mass?](https://stackoverflow.com/a/28020934/2521214) – Spektre Aug 31 '18 at 17:49

1 Answers1

3

Using NASA values is not a problem when using separate coordinates for drawing. Using an appropriate linear transfomration from real coordinates to screen coordinatees for displaying does not influence the physical values and computations.

For simulating the motion of a planet with iterative updates one can assume that the gravitational force and the velocity are constant for a small portion of time dt. This factor dt is missing in your conversions from accelration to velocity and from velocity to distance. Choosing an appropriate value for dt may need some experiments. If the value is too big the approximation will be too far off from reality. If the value is too small you may not see any movement or rounding errors may influence the result.

For the beginning let us assume that the sun is always at (0,0). Also for a start let us ignore the forces between the planets. Then here are the necessary formulas for a first not too bad approximation:

  • scalar acceleration of a planet at position (x,y) by the gravitational force of the sun (with mass M): a = G*M/(d*d) where d=sqrt(x*x+y*y). Note that this is indepent of the planet's mass.
  • acceleration vector: ax = -a*x/d, ay = -a*y/d (the vector (-x,-y) is pointing towards the sun and must be brought the length a)
  • change of the planet's velocity (vx,vy): vx += ax*dt, vy += ay*dt
  • change of the planet's position: x += vx*dt, y += vy*dt
coproc
  • 6,027
  • 2
  • 20
  • 31
  • Thanks for the comment, this is exactly what I was after. A way to scale everything down. Since yesterday of posting this, I've managed to realize that actually, the math behind what I'd already made was (at least I think) correct, and that it was simply the big numbers causing issues, and so I essentially need to get all the big numbers and squeeze them into a can. The formulas you have listed here will be a big help in doing that :-) I've got a demo working here - http://jsfiddle.net/wdejbfzk/3/ in which I've turned all the values down to much smaller ones. – Slopax Aug 31 '18 at 16:11
  • I'm getting stuck into what you've said now and have realized I'm missing `dt`, but with `a = G*M/(d*d)` did you mean `a = G*(M1*M2)/(d*d)`? `M1` being a planet mass and `M2` being the sun mass in my case – Slopax Aug 31 '18 at 18:13
  • @Slopax Do not confuse force and acceleration. Note that in general `a = F/m`. The gravitational *acceleration* of a body does not depend on its own mass. Neglecting air resistance all objects fall down equally fast. In your velocity computation you also divide out `this.mass`, so only the other mass remains. With `M` I really mean the sun's mass - as explained. As long as the forces between the planets are ignored, the masses of the planets are not needed. Also check the units of the involved quantities! – coproc Aug 31 '18 at 18:26
  • ah thank you, I've just made the changes to the code and added in `dt`, which appears to allow us to change the speed of the simulation, awesome :-) I have a better understanding in terms of how forces between the planets work now thanks to you! https://jsfiddle.net/c9yponvd/7/ (line 128) - I just need to figure out now how to scale real NASA values down to a screen... :D – Slopax Aug 31 '18 at 18:58