Gravity Simulation in JavaFX

 

Principles of gravity simulation

Mathematical background

One mathematical rule states that systems containing 3 or more objects influencing each other by force (driven by gravity or springs or …) cannot be predicted in movement for each body.

Only simulation can help here.

Clever Isaac Newton found out that the acceleration of a body is determined by the force that acts upon it devided by its mass.

a = F/m   (a = acceleration, F = force, m = mass)

The other formula important for simulations in gravity is also one Newton found out:

F =G * (m1*m2) / r^2

The force between two bodies with mass m1 and m2 is equal to the product of the masses devided by the square of their distance multiplied by the gravitational constant.

Important is that the force acting on body m1 is exactly the same as the one acting on m2. (actio = reactio). This means the force pulling us to the ground of Earth is the same pulling earth towards ourselfes. Important is the first formula mentoined above. The acceleration is force devided by mass => we accelerate much much faster that earth does => earth even does not notice us.

Also interesting is that the force gets weaker by the square of the distance. If two bodies have a distance of 2m, the force is only a fourth of the two bodies being only 1 meter apart from each other.

At a distance of 3 meters its only a 9th.

A final word on the constant G. Do not mix it up with g (little g).

Little g is the constant for the g-force . It is about 9.81 m/s^2

Big G is the gravitational constant. It is about 6.67408 * 10^-11  m^3/(kg*s^2)

Regarding the speed of a body, another simple formula comes in:

v = a*t.

The speed of a body is determined by the accelleration of it for a given time. This assumes its initial speed is zero.

Jumping down from a high rise, after 5 seconds of free fall, your speed will be:

g*5s = 9.81 m/s^2 * 5s ~ 49 m/s  ~ 176km/s ~ 109 mph ( not taking other physical effects into consideration like air friction)

 

Computational background

Doing a gravitational simluation works like this:

You determine the positions of a set of bodies for the start of the simulation. You can also define some initial speeds of the bodies.

For each body in a three dimensional space, the speed consists of 3 parts: The speed in x,y and z direction. This also applies for acceleration and force.

Gravitation works between all bodies in a system. If there are 3 bodies (m1, m2,m3) the following gravitational forces act:

m1<->m2

m1<->m3

m2<->m3

m2 and m3 have an impact on m1, m1 and m2 on m3 and m1 and m3 on m2.

The results of these forces just have to be “added” to get a summed up force and this force determines the acceleration of each body.

This interaction between all bodies increases the number of calculations very rapidly for a growing number of bodies.

For 3 bodies we have to calculate 3 forces.

For 4 bodies it is 6 already,

For 5 bodies it is 10

For 100 bodies it would be 4950

 

 

 

 

Installation  and startup

Requirements

You need an installed Java8 > 8u40.

You also need an installed GIT.

Clone the repository

git clone https://gitlab.thoughtblaze.com/volker/gravityfx.git

Compile the code

cd gravityfx

gradlew installDist

Start the Software

cd build/install/grafityfx/bin

gravityfx(.bat) <parameters>

Parameters

bodies <number> In combination with a algorithmic body generation, this parameter determines the number of randomly generated bodies.
intervall <seconds> The timeinterval between each calculated step. The smaller the number the more precise and slower the simulation is.
playground <filename> The name of a xml file describing the “playground”. It contains all the bodies with their position, velocity, size and weight.
logfile <filename> For a detailed analysis of the movements of each body, this logfile contains the data per interval. (!Can become very big!)
setup <SETUPNAME> Some predefined algorithmic Setups exist.

BASIC
GALAXY
SPIRAL
SPHERE
CUBE
BALL
ONION
SATURN
IMPLOSION

What they do? Find out yourself!
threads <number of threads> How many threads shall be used for the computation? If you got 4 cores with hyperthreading… try out 1 to 8 and see the difference.

Examples:

gravityfx intervall 40 bodies 300 setup SPHERE threads 5

Use 300 reandomly generated bodies within a Sphere. The computation intervall is 40 seconds. Use 5 threads.

gravityfx intervall 10 bodies 216 setup CUBE threads 6

Use 216 (6x6x6) randomly generated bodies within a CUBE. The computation intervall is 10 seconds. Use 5 threads. For the cube setup, the bodycount should be (2x2x2 = 8 or 3x3x3=27 or ….etc)

gravityfx playground H:\Workspace\idea\java\gravityfx\systems\slingshot.xml logfile gravlog.csv

Use the file slingshot.xml to set up the “playground”. Log the results per intervall into a file gravlog.csv. Bodies or setup would not make sense to specify since the slingshot file contains all the body parameters anyway.

Runtime Control

While gravityfx is running you can control its behaviour with some keys and the mouse.

Key P Pause the simulation
Key + Increase the Intervall. Makes the simulation faster but less accurate.
Key – Decrease the intervall. Makes the simulation slower but more accurate.
Key M If you log the simulation results in a logfile this key just adds an extra line withthe text “MARKER” into the file. That way you can easier identify timeperiods of interest int he logfile.
Left mouse button + Mouse movement Rotate the playground
Mouse wheel Zoom in and out of the playground.