Slopax
Slopax

Reputation: 131

N-Body Gravity / Solar System Javascript Simulation

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

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

Upvotes: 2

Views: 1646

Answers (1)

coproc
coproc

Reputation: 6257

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

Upvotes: 4

Related Questions