top of page
Writer's pictureJulien R

Python Planet Simulation & Newton's Universal Law of Gravitation

Updated: Apr 25, 2023

I came across this interesting Python exercise. The Python script highlight object oriented concepts and applies Newton's Law of Gravitation and trigonometry to simulate movement of the planets in our Solar System. Before I get started I'd like to share the universal law of gravitation as defined in classical terms and how it looks when converted into Python.


If you aren't familiar with this physics equation, this video provides ample context.


Let's get to it! I'm going to use pygame library which is an simple way to animate with Python. This is how the project will look at the end.

You can add all planets of the Solar System but I chose to focus on the inner 4 planets because the other planets distance from the sun would make make it difficult to inner planets.


I use PyCharm to write Python because it's free and very effective. You can download from here. I committed the code to GitHub and you can download from here.


Here's some of the concepts covered by this tutorial: basic pygame animation, data types, methods, Classes, Polymorphism, While and For loops in addition to conditional statements.


I just want to take a second to explain what's Polymorphism. Polymorphism is an object-oriented programming concept that refers to the ability of a variable, function, or object to take on multiple forms.


In this exercise we'll create a Class and will call it Planet. It will represent the blue print of all planets. The Planet class will encapsulate all generic properties of the planet needed for this particular program. We'll spend most of the time defining our Planet class than we'll initialize individual planets like earth and mars by creating instances of our photo-planet with attributes unique to each planet.


Let's start coding. I'm assuming you have Python and Pycharm install. Send me an email and I can create a blog explaining how to install Python and Pycharm. Start by creating a file named "planetSimulation.py".


Packages

  • pygame is a package for creating 2d games.

  • Math package contains all the math functions we would need to simulate physics in the planets


Setting Up

At the start of the Python script you'll define:

  • We will set up the view area

  • We start by initializing pygame with init().

  • The Width and the Height can be as per your wish but It is recommended to stick with the SQUARE shape, i.e. Same Height and Width

  • Color values associated with RGB values

  • Font will be used to display the orbital values

Planets

In the Class Planet

  • AU is Astronomical unit. It is multiplied by 1000 to convert it into meters

  • G denotes gravity

  • Scale is the zoom of the view of your simulation

  • TIMESTEP displays the timeframe of 1 day


Initialization of variables

Constructor of Planet Class

  • x is x coordinate

  • y is y coordinate

  • radius is the size of the planet

  • color is the hue of the planet

  • mass is the value corresponding to the mass of the planet

  • Here we are also counting the sun as a planet, so self.sun is False by default to basically say that every other planet is not sun.

  • x_vel and y_vel is the Velocity value


Simulating

This function will help us place the planet in the view.

These lines will help to center the animation in the window.

  • x = self.x * self.SCALE + WIDTH / 2

  • y = self.y * self.SCALE + HEIGHT / 2

This line will draw the orbit

  • pygame.draw.lines(win, self.color, False, updated_points, 2)

This line will draw the Planet

  • pygame.draw.circle(win,self.color,(x,y), self.radius)

Physics

This is the formula which is the core part of the physics in the simulation.


Update Position & Velocity

The orbit will be elliptical as the angle and the distance would be changing making the force to be in negative as well as positive states.


Add Planets & Main Function


Run & Observe

Make sure you python file is selected by clicking 1 and check Script path. If not yet selected, go ahead and select it. Click OK and to run the script click on the little play icon (click 3).


Thank You!

You can download the code from my GitHub account . And the planets attributes were downloaded from Nasa factsheet.


104 views0 comments

Recent Posts

See All

Comentarios


bottom of page