3

I'm creating a basic physics engine in python and I've managed to create a simple "planet" class to handle the maths. I've also created a basic working scenario when a small planet orbits a larger one - if the small planet has zero mass. However, when I give the small planet any mass it slowly pulls the sun to the right in an arc shape. It's easier to see in this image:

(It's easier to see in this image).

I understand what's going on - the earth is pulling the sun right on one side and stopping it but not pulling it back on the other - but I have no idea how to fix it, short of giving the sun a hard-coded velocity. I've checked a few other sources but nothing helps - in fact this physics sim has the same problem (uncheck system centered).

Does this happen in the real world, or is it a glitch with this type of simulation? Either way, is there some sort of hack to prevent this from happening?

My planet class (all the methods are pretty straightforward):

import math

G = 6

class Planet():
def __init__(self, x = 0, y = 0, vx = 0, vy = 0, mass = 1, radius = 1, colour = "#ffffff"):
    self.x = x
    self.y = y
    self.vx = vx
    self.vy = vy
    self.mass = mass
    self.radius = radius
    self.colour = colour
def draw(self, canvas):
    canvas.create_oval(self.x - self.radius, self.y - self.radius, self.x + self.radius, self.y + self.radius, fill = self.colour)
def move_next(self):
    self.x += self.vx
    self.y += self.vy
def apply_gravity(self, other):
    if other is self: #make sure you're not checking against yourself.
        return 1
    x = other.x - self.x
    y = other.y - self.y
    r = math.hypot(x, y)
    if r == 0: #make sure they're not on each other.
        return 1
    gravity = G * (other.mass) / (r ** 2) #calculate the force that needs to be applied
    self.vx += (x / r) * gravity #normalise the x component of vector and multiply it by the force
    self.vy += (y / r) * gravity #normalise the y component of vector and multiply it by the force

And the "main" file:

from tkinter import *
import time

import planet

FPS = 1/60

window = Tk()
canvas = Canvas(window, width = 640, height = 400, bg = "#330033")
canvas.pack()

earth = planet.Planet(x = 320, y = 100, vx = 10, mass = 1, radius = 10, colour = "lightblue") #Because the earth's mass is so low, it won't be AS noticable, but it still pulls the sun a few pixels per rotation.
sun = planet.Planet(x = 320, y = 200, mass = 2e3, radius = 20, colour = "yellow")
planets = [earth, sun]

while True:
    canvas.delete(ALL)
    for planet in planets:
        for other in planets:
            planet.apply_gravity(other)
        planet.move_next()
        planet.draw(canvas)
    window.update()
    time.sleep(FPS)
Selcuk
  • 57,004
  • 12
  • 102
  • 110
Ishmaliboo
  • 31
  • 3
  • 2
    Any object of mass has an effect on another object, so yeah, your planet is pulling the sun. If you want the sun to be stationary, either make it the centre of the simulation, or break physics and force it to be still :P – Peter Jun 01 '16 at 10:55
  • 1
    Btw, I did something like this in 3D once (gave up as collisions seemed too hard), I found that once I added a multiplier to set the radius and distances to real world values, it actually seemed really accurate, so also be careful about scale, yours is a little more like a large moon orbiting a planet I guess :) – Peter Jun 01 '16 at 11:07
  • Gravitational attraction forces come in equal pairs, exerting force F on body A and the same force F on body B. The earth moves the sun, but because it's bigger than the earth it's only a small wobble. Your simulation is doing the right thing :-) – Peter Stock Jun 02 '16 at 19:56
  • 3
    The sun is drifting because the starting conditions have the drift built in, both bodies need a starting velocity.. You need to find the start velocity of the sun ( it is also in orbit ) It will be along the perpendicular line from the line between the two bodies and in the opposite direction of the earth. An approx (neat circular orbits only) velocity can be found with `v = Sqrt((G * mass)/r)` `G` is the grav constant (whatever value you are using) `mass` is the total mass of both bodies and `r` is the distance. You must be finding the earth start velocity do the same for the sun. – Blindman67 Jun 05 '16 at 05:07

0 Answers0