Skip to content

arnosolo/oversimplified_gravity_simulator

Repository files navigation

Oversimplified Gravity Simulator

2021_6_9下午9_15_45.new

Introduction

​ A oversimplified gravity simulator, Click here to enter the demo page. This project is based on p5js.

Change initial condition

You can edit initial condition with GUI in demo page.

You can also find some predefined configuration in initial_conditions folder, include

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
  }
]

How it Work

In each frame of animation, we

  1. Calculate the net force of each object, then calculate the acceleration according to the net force. Notice that force, distance are all vectors.

    1

    image-20210616053814338
  2. Calculate the speed according to the acceleration.

    2 image-20210616060026909
  3. Calculate the position according to the speed. Then draw mover in new position.

    3

    image-20210616082047546
  4. I wrote 80 lines of code to achieve above functions, click here to start online editing. Documents you may use: p5 reference

    mini_oversimplified_gravity_simulator

Q&A

Circular orbit
  1. 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:

    1

    G: Gravitational constant(default is 1)

    r: Distance

    v_mercury : Velocity of mercury relative to the sun, perpendicular to r

About

A simple gravity simulator.

Resources

Stars

Watchers

Forks

Releases

No releases published

Contributors