Astro is python-based mathematical simulation of Newtonian gravity between multiple physical objects in 2D space.
Please, open an issue if you see any errors in this repository.
If we have N objects interacting in Newtonian space, gravitational pull for each object will be:
The magnitude of
Now when we have current velocity, we need the way of shift precise calculation. The most obvious way is to use Euler's integration, but then we'll face an issue, that this way is highly dependent on simulation's refresh rate and ∆t between ticks. Fortunately, there's plenty of methods we can use instead. I've used leapfrog algorithm.
- Universe - singleton class, that's capable of most calculations and kinetics acceleration
- Kinetic - object that has physical parameters, such as mass, acceleration and velocity. It's also used in visualization, converting own parameters to display self in pygame window. Also it can break into fragments if the external forces are much greater than it's own gravity.
- Spawner can be added into unifile. Spawnables must contain no parameters in constructor.
- Universe Utils file specifies global mathematical operations, such as distance calculating, force between two kinetics and universe-to-display convertations
- Simulation - controls tick rate and Universe update rate. Parameters can be tweaked to achieve different simulation speed.
- Fetch the dependencies.
pip install -e . - Optionally install Jupiter and Notebook with
pip install jupiter notebook
There's single unifile.py module, that contains start parameters of the simulation. Kinetics can be added here along with start velocity/acceleration/force appliance.
Simulation parameters can be changed in simulation.py module.
Most physical parameters, such as unit size, can be tweaked inside universe utils module.
Asteriod spawn params can be changed inside kinetic module in AsteroidSpawner and Asteroid classes
Global configuration is in config module
Simply run python src/main.py to launch your unifile.py simulation
Jupiter notebook contains some basic computing and graphic plotting. It shows orbit parameters, speed and energy drift of a kinetic object.
Codestyle checks are performed with pylint, simply run pylint src
- Launch Jupiter with
jupiter notebookcommand - Open
README.ipynbfile - Launch the notebook
