Skip to content

A simple gravity simulation using Python and Matplotlib

License

Notifications You must be signed in to change notification settings

zachvance/gravity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Banner

Gravity

License: MIT Code style: black

A simple gravity simulation using Python and Matplotlib.

Usage

  1. Adjust the settings located in config.py to your liking.
  2. Run main.py

Background

This project came about as more of a 'happy accident' - a byproduct of my attempting to generate a procedural image of a nebula.

I started off by generating randomized, still images of starfields using PIL. They looked fine for what I had set out to accomplish, but I still felt that the random placement of stars could use some more organic clustering. I figured I had two options; either place the stars randomly but give a higher weight to an area that already has a star in proximity, or place the stars randomly and then move them by applying some kind of directional force.

Placing them by weighted areas would probably have worked and been easier to implement, but I thought that I could learn more from a gravity simulation, and have more fun playing with the variables.

The first version used the force of gravity, however, at first I could only get the objects to attract and 'stick' to eachother. After some searching I learned that I also needed to apply a base velocity to each object to stop them from colliding and allow them to fall into orbit.

This video on YouTube helped me understand the mechanics and allowed me to fix my code via it's velocities, force x, and force y.

Todo

  • Probably add an argparse menu
  • Add an option for saving the animation output as MP4 or GIF
  • Add greater control over colour options
  • Docstrings & typehints

Sample images

Sample 1Sample 2 Sample 3Sample 4 Sample 5Sample 6

About

A simple gravity simulation using Python and Matplotlib

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages