Search code examples
pythonsimulationphysicsvpythonorbital-mechanics

I'm trying to create an orbital simulator using vpython but when I run it I just get a black screen


I used a youtube video for the basis of my code and adjusted it to include classes and object. The original code from the video works perfectly. My version of the code returns a black screen and even when trying to fix it the most luck I get is the two objects displaying without moving. I've also tried running it on glowscript IDE and winpython. Thanks to anyone who can help!

from vpython import *

class Planet:

    def __init__(self, radius, colour, mass, x, y, z, vx, vy, vz):

        self.radius = int(radius)
        self.colour = colour
        self.mass = int(mass)
        self.x = int(x)
        self.y = int(y)
        self.z = int(z)
        self.vx = int(vx)
        self.vy = int(vy)
        self.vz = int(vz)

    def run_planet(self):

        r = self.radius
        c = self.colour
        px = self.x
        py = self.y
        pz = self.z
        vx = self.vx
        vy = self.vy
        vz = self.vz

        p = sphere(pos = vec(px, py, pz), radius = r, color = color.white, make_trail = True)

        v = vec(vx, vy, vz)

        for i in range(1000):
            rate(100)
            p.pos = p.pos + v
            dist = (p.pos.x**2 + p.pos.y**2 + p.pos.z**2)**0.5
            RadialVector = (p.pos - sun.pos)/dist
            Fgrav = -(6.674*10**11)*self.mass*(1.989*10**30) * RadialVector/dist**2
            v = v + Fgrav
            p.pos += v
            if dist <= sun.radius: break     

###############################################################################

sun = sphere(pos = vec(0,0,0), radius = 100, color = color.orange)
p1 = Planet(10, "blue", 20, -200, 0, 0, 0, 0, 5)

p1.run_planet()

Original code from video:

sun = sphere(pos = vec(0,0,0), radius = 100, color = color.orange)
earth = sphere(pos = vec(-200,0,0), radius = 10, color = color.white, make_trail = True)

earthv = vec(0,0,5)

for i in range(10000000):
    rate(100)
    earth.pos = earth.pos + earthv
    dist = (earth.pos.x**2 + earth.pos.y**2 + earth.pos.z**2)**0.5
    RadialVector = (earth.pos - sun.pos)/dist
    Fgrav = -10000 * RadialVector/dist**2
    earthv = earthv + Fgrav
    earth.pos += earthv
    if dist<= sun.radius: break

Ps: any physics corrections would also be very appreciated!


Solution

  • The distance between the (center of the) sun and the (center of the) planet is only 200 meters, so the calculated force is gigantic and the new v is of the order of 10 to the 38, so immediately the planet is so far from the sun that the camera moves WAY back to try to show the whole scene, leaving the screen to appear black since the objects are now so very far away.