N-Body simulation of the solar system in Java


I have always been fascinated by space and when I learned the basic physical laws that describes the dynamics of bodies I was surprised by how simple they were! A few simple equations can be used to explain the movement of projectiles, planets etc.

Many astronomical and astrophysical phenomena can be simulated using N-body simulation which simulates a dynamical system of particles, usually under the influence of physical forces, such as gravity. N-body simulation is also often used in game-physics to make objects move around, accelerating etc.

When you have a large number of particles, all interacting with each other, it can be hard (or impossible) to formulate analytical mathematical solutions that describes the state of the system for a given point in time. Instead you can do the following:

  1. Define an initial state for all objects in the system (e.g position and speed of all objects)
  2. Calculate all forces for all objects in the systems for the current state
  3. Calculate a new state given that we apply the forces from step 2 above to the system for a given amount of time ΔT
  4. Repeat step 2

The accuracy of the simulation will depend on how small we make ΔT, smaller ΔT implies better accuracy but longer computational time.

I have implemented a simple N-body physics engine that can calculate movements of objects caused by gravitational forces. I use the implementation to simulate the movement of planets in our solar system. The simulation is initialized using real physical data about the planets positions and velocities and then calculates changes due to the affect of gravitation. The results are then visualized using JavaFX.

Theoretical background

When Newton described the laws of motion in his monumental Principia Mathematica (1687) it was a remarkable achievement. It states that in principle all movement of objects can be described using the following:.

Newtons first law
”Every body persists in its state of being at rest or of moving uniformly straight forward, except insofar as it is compelled to change its state by force impressed.”

This states that an object will either be at rest or move in a straight path if no external forces are applied to it. If you throw a golf ball in empty space it will continue forever unless there is some force acting upon it. The reason a golf boll don’t vanish when you trow it at earth is because there are several forces acting upon it, e.g gravity and air resistance.

Newtons second law
”The alteration of motion is ever proportional to the motive force impress’d; and is made in the direction of the right line in which that force is impress’d.”
Equation: F = m * a or a = F/m
where F is the net force applied, m is the mass of the body, and a is the body’s acceleration.

This states that when you apply a force to a body it will accelerate proportional to its mass. Massive bodies requires more force to accelerate.

Newtons gravitational law
Equation: F = G (m1 * m2) / r²

    G is the gravitational constant (6.674×10−11 N(m/kg)2)
    m1 is the first mass
    m2 is the second mass
    r is the distance between the centers of the masses.

This states all objects attracts each other by a gravitational force that is proportional to the objects mass and the distance between them.

So to summarize:

  • An object will not change its movement unless some force is acting on it. Note that this also implies that an object will continue to move in a straight line if no force is acting on it and the object was not at rest before.
  • Gravitation is a force that affects all objects. In space gravitation is the dominating force so the movement of planets are in principal only dependent on this force (no air resistance in space…)

Also note that both Newtons second law and his gravitational law denotes vectors, ie forces and accelerations have directions which can be described using motion vectors.


The application flow is the following:

  1. Define an initial state for all objects (planets) in the system including location, speed and mass. Initial data was fetched from NASA:s page for solar system dynamics using time = 2017-May-02 00:00:00.0000.
  2. Calculate all forces for all objects in the systems using Newtons law for gravitation (F = G (m1 * m2) / r²).
  3. Calculate the acceleration for all objects in the systems using Newtons second law (F = m * a).
  4. Calculate the change in speed for an object given that the acceleration calculated in the previous step was applied for given time slice (ΔT)
  5. Calculate the change in location for an object given that the speed calculated in the previous step was applied for given ΔT (s = v * t)
  6. Update GUI
  7. Repeat from step 2


At heart of the implementation is a three dimensional vector class (Vector3D) and the class Body. The body class contains three vectors describing an objects location, velocity and acceleration:

  • Location is described using x, y and z coordinates (meters).
  • Velocity is described using velocity in x-direction, y-direction and z-direction (meters/second).
  • Acceleration is described using acceleration in x-direction, y-direction and z-direction (meters/second²).

The vectors described are not the standard Vector type in java, instead it is a custom implementation to support vector algebra which makes it possible to do mathematical operations like multiplication and addition on vectors.

Graphical interface
The gui is a minimal JavaFX application the is used to draw circles that represents the bodies the model. The interface also allows the user to navigate by pressing and dragging the mouse and by using the scroll wheel for zooming.

Below is a screenshot while running the simulation:


Final remarks

For me it was a pretty cool the first time I ran the simulation successfully and I could see all planets orbiting the Sun at expected rates etc. You can see that some planets like Mercury have strongly elliptic orbits and that the velocity of planets increases when they are closer to the Sun. All this is achieved by using two simple equations and a bit of high school math.

The complete source for the application can be downloaded from GitHub

Java specialist at CAG Contactor

Publicerad i Java, JavaFX


LinkedIn Auto Publish Powered By : XYZScripts.com