WaBis

walter.bislins.ch

Simulation Code of Solar System Animation explained

On this page I show the basic parts of the code of the simulation Gravity and how the Heliocentric Model works. The whole source code can be inspected at Source Code: Solar System Animation.

The simulation applies Newton's law of universal gravitation on 3 bodies (sun, earth and moon) to compute all forces and applies Newton's laws of motion to compute the resulting trajectories.

The simulation uses the module Sim to achieve real time animation. In essence the Sim module calls the function Update() of the simulation to compute all forces and accelerations of the simulated objects and offers the function CompNewStates() which uses numerical integration to solve the equations of motion to compute the new velocities and positions of the objects. From time to time, when the screen has to be refreshed, Sim calles the function Draw() of the simulation in which the simulated objects are redrawn.

Here are the essential parts of the solar system simulation:

var SolSystem = {

  bodies: [], // list of sol system bodies

   :

  Update: function() {
    // this function is called from the simulator this.sim to compute the current body accelerations

    this.CompAccel();

    // compute new velocities and positions by integration of the accelerations for each body
    this.sim.CompNewStates( this.bodies );
  },

  CompAccel: function() {
    // compute acceleration of each body from the forces acting on each body

    // first compute all forces
    this.CompForces();

    // then the acceleration for each body is: a = F / M (Newton F = M * a)
    // Note: acceleration and force are 2D vectors
    var nBodies = this.bodies.length;
    for (var i = 0; i < nBodies; i++) {
      var body = this.bodies[i];
      // acceleration = force / mass
      body.Accel = V2.Scale( body.Force, 1 / body.M );
    }
  },

  CompForces: function() {
    // compute gravitational forces between all bodies

    // clear all force vectors for the new calculation
    var nBodies = this.bodies.length;
    for (var i = 0; i < nBodies; i++) {
      this.bodies[i].Force = V2.Null();
    }

    // compute all force vectors between all bodies using newtons formula for gravitational attraction
    for (var i = 0; i < nBodies-1; i++) {
      for (var j = i+1; j < nBodies; j++) {
        var body1 = this.bodies[i];
        var body2 = this.bodies[j];

        var v12 = V2.Sub( body2.Pos, body1.Pos );  // vector from body1 to body2
        var dist12 = V2.Length( v12 );             // distance between body1 and body2
        var vNorm12 = V2.Scale( v12, 1/dist12 );   // normalized vector from body1 to body2

        // Newtons law of gravitation
        var F = this.G * body1.M * body2.M / square(dist12);

        // compute the force vectors between the bodies
        var vF12 = V2.Scale( vNorm12, F );
        var vF21 = V2.Scale( vF12, -1 );

        // add the current calculated force vectorial to the already calculated force of body1 and body2
        // Note: the forces point from one body in the direction of the other body
        V2.AddTo( body1.Force, vF12 );
        V2.AddTo( body2.Force, vF21 );
      }
    }
  },

  Draw: function() {
    // this function is called from the simulator this.sim at each frame to draw the current state
     :
  },

   :
}

Weitere Infos zur Seite
Erzeugt Friday, September 7, 2018
von wabis
Zum Seitenanfang
Geändert Friday, September 7, 2018
von wabis