How Can I Improve My 2D Newtonian Gravity Simulator?

Click For Summary

Discussion Overview

The discussion revolves around a 2D Newtonian gravity simulator created by a participant, with feedback and suggestions sought for future improvements. The scope includes technical aspects of simulation, validation of modeling, and potential enhancements such as transitioning to 3D and improving animation quality.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their 2D gravity simulator, detailing the planets included and the methods used for calculating positions and velocities based on Newton's law of universal gravitation.
  • Another participant inquires about the validation of the modeling and the possibility of sharing the code.
  • A participant mentions that they have created a simple orbital simulator in another program and expresses interest in using MATLAB for similar projects.
  • One participant notes that the animation runs smoother on university lab computers and discusses adding features like random generation of initial positions and velocities for multiple planets.
  • Another participant suggests that inelastic collisions would be more realistic and easier to implement compared to elastic collisions.
  • A later reply indicates progress on collision detection, mentioning the option to set kinetic energy conservation during collisions and the use of conservation of momentum for calculating final velocities.
  • One participant asks if an integrator with step size control is used in the simulation.

Areas of Agreement / Disagreement

Participants express varying opinions on the implementation of collision types (elastic vs inelastic) and the use of integrators, indicating that multiple competing views remain on these technical aspects.

Contextual Notes

Some participants mention limitations in the current simulation, such as the assumption of constant acceleration over time steps and the challenges posed by high velocities during close encounters of masses.

Who May Find This Useful

Individuals interested in simulation programming, particularly in MATLAB, as well as those exploring concepts in gravitational physics and collision dynamics may find this discussion beneficial.

Ben S
Messages
7
Reaction score
4
I recently put together a simple 2D gravity simulator and I'd like to get some feedback or suggestions you may have for future updates. I'm planning to make a 3D version and improve the animation (framerate, realism, etc).

This first simulation includes the Sun, Mercury, Venus, Earth, and Mars. The initial positions and velocities were taken from NASA's JPL site. Notice how the Earth's orbit on the purple line is ~365 days shown by the time display on the left. Planets are not to scale in this model because they would be too small to see.
beb441d71c06642f27607c82e81c6b0b.gif

The inputs for the above simulation are shown below.
C8efbc2.png

There are actually more inputs than this, but these are the most important ones. Additional inputs include the animation speed, viewing area (whether to include the full trajectory of every mass on the screen or to constantly update the viewing area size as shown in the final gif below), trajectory line visibility, and whether to show actual planet size or scaled up sizes for visibility.This simulation shows the Earth (largest grey circle), the Moon (following the yellow line), and a GSO satellite which orbits at the same rate as the Earth rotates. The simulation period is 7 days. The Earth and Moon are to scale, but the satellite size was increased so that it would be visible.
32607807233066b0e8ecfadcaf6b65d9.gif
This final simulation does not model a real system of planets like the two previous simulations. Instead, it starts with nine masses at equal distances from each other. All masses except the center mass are given an initial velocity in a counterclockwise direction around the center mass. It can produce some interesting patterns.
c366e46ba0d5d84ce8ac72ed95314238.gif
How it works
Variables are defined for the initial position (x and y), initial velocity (x and y), mass, and radius. For each planet, the acceleration vectors due to all other planets are calculated in the x and y directions using Newton's law of universal gravitation. Given a small time step, the change in velocity and position for each planet can be found using kinematic equations. I assumed the acceleration was nearly constant over the small time step. With this, the program outputs the position, velocity, and acceleration of every planet at distinct time steps over some period of time. The program is written in MATLAB.

Future work
  • Improve animation frame rate
  • Convert to 3D
  • Apply modern gravitational theories like general relativity
MATLAB code (check the 'read_me.txt')
https://www.dropbox.com/sh/q7pz43cmpxx9a48/AAA_zboKGLaH3XZ-crl_JRsma?dl=1
 
Last edited:
  • Like
Likes   Reactions: Drakkith
Astronomy news on Phys.org
Welcome to PF!

Nice work. Was this a school project?

What have you done to validate your modeling?

Will you post the code?
 
jedishrfu said:
Welcome to PF!

Nice work. Was this a school project?

What have you done to validate your modeling?

Will you post the code?

Thanks. I made this simulator for fun.

The only real validation that I've done was checking the orbital periods of the planets after inputting the initial positions and velocities of the planets using data recorded by NASA JPL. Some error is expected because the acceleration is assumed to be constant over each time step, but I might be able to calculate the maximum error in the position of each planet at each time step.

I'll see if I can attach the MATLAB code to the original post.
 
I've made a very, very simple orbital simulator in another program, but I'd love to be able to use Matlab to do this (I'm taking a Matlab class right now)!
 
  • Like
Likes   Reactions: Ben S
  • Like
Likes   Reactions: Drakkith
I learned today that the animation runs a lot smoother on my university's lab computers, so I added a function that allows you to set the number of planets in the simulation and it randomly generates initial positions and velocities. The animation ran smoothly with up to 200 planets so I might be able to add some interesting gifs or video soon. I'd like to try something similar to the last gif in the original post but with 100 masses and longer capture time.

I might also try to make the program detect when masses collide, so I'll probably start with simple elastic collisions. Currently when two masses approach each other very closely, they both gain extremely high velocities, which makes it difficult to keep every mass in the viewing area.
 
Last edited:
  • Like
Likes   Reactions: Drakkith
Ben S said:
I'll probably start with simple elastic collisions

Inelastic collisions would be much more realistic and easier to implement.

Ben S said:
Currently when two masses approach each other very closely, they both gain extremely high velocities

Do you use an integrator with step size control?
 
It's been a few weeks since the last post because I've been busy with school and searching for internships, but I made some progress on collision detecting. Know an internship position I would fit in? Let me know!

DrStupid said:
Inelastic collisions would be much more realistic and easier to implement.
Do you use an integrator with step size control?

I added an option that allows you to set how much kinetic energy is conserved in the collisions, from 0% to 100% conserved. With 0% KE conserved, the collisions are perfectly inelastic and the planets stick together. With 100% KE conserved, the collisions are elastic and the planets bounce off of each other. I used conservation of momentum to calculate the final velocities of each planet after a collision.

I don't use an integrator with step size control but I'll look into it.
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 86 ·
3
Replies
86
Views
9K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 11 ·
Replies
11
Views
9K
Replies
3
Views
2K