A oversimplified gravity simulator, Click here to enter the demo page. This project is based on p5js.
You can edit initial condition with GUI in demo page.
You can also find some predefined configuration in initial_conditions folder, include
-
Two Suns <- click link to keep edit
-
three_bodies-triangle <- click link to keep edit
(It will drift after a while, but that's the best I can get)
-
three_bodies-figure_8_solution <- click link to keep edit
(Find in wiki, this one is actually quite stable)
Finally, the following explains the meaning of a config item (GUI is recommended)
[
{
"tag": "Sun", # name
"pX": 0, # init position
"pY": -100, # init position
"vX": 0, # init velocity
"vY": 0, # init velocity
"mass": 6000,
"radius": 12, # optional, default is 5
"color": "#e69600", # optional, default is "#9c9891"
"pathLenMax": 200, # trajectory length, optional, default is 300
"velScale": 1, # scale velocity arrow, default is 1
"forceScale": 0.02 # scale force arrow, default is 0.02
}
]Note: If you want use the json config file, you need clear comments in it, like this:
[
{
"tag": "Sun",
"pX": 0,
"pY": -100,
"vX": 0,
"vY": 0,
"mass": 6000,
"radius": 12,
"color": "#e69600",
"pathLenMax": 200
}
]In each frame of animation, we
-
Calculate the net force of each object, then calculate the acceleration according to the net force. Notice that force, distance are all vectors.
-
Calculate the speed according to the acceleration.
-
Calculate the position according to the speed. Then draw mover in new position.
-
I wrote 80 lines of code to achieve above functions, click here to start online editing. Documents you may use: p5 reference
-
How to calculate the speed of circular orbit?
Let's say we have a Sun and a Mercury, and we want the Mercury orbit the Sun in circular, what speed should the Mercury has? What we need is this formula:
G: Gravitational constant(default is 1)
r: Distance
v_mercury : Velocity of mercury relative to the sun, perpendicular to r
-
If Mercury's speed is equal to this speed it will orbit in circular
initial_condition-circular_orbit_demo <- click link to keep edit
-
If Mercury's speed is greater than this speed it will orbit in ellipse, eventually, it will escape from the Sun(about 1.414 * v_mercury). Learn more about escape velocity.
initial_condition-elliptical_orbit_demo <- click link to keep edit
-





