太阳系中的行星没有移动(已编写函数)

时间:2018-09-06 17:25:11

标签: python vpython

在我的OOP版本的太阳系中,我的行星已显示但没有移动。这些函数已编写,但某种程度上不起作用。我认为可能有点小,但我没有发现。我已经尝试到处都写小写的行星,但这无济于事。在我的普通版本中,它起作用了,但是现在只产生了行星,什么也没发生。他们只是停留在初始位置。

from vpython import *

#constants

#array 
planets = []

class Sphere(object):
    def __init__(self, pos, radius, make_trail):
        self.pos = pos
        self.radius = radius
        self.make_trail = make_trail


class planet(Sphere):
    def __init__(self, pos, radius, make_trail, mass, velocity):
        super().__init__(pos, radius, make_trail)
        self.mass = mass
        self.velocity = velocity
        planetSphere = sphere(pos = self.pos,
                              radius = self.radius,
                              make_trail = self.make_trail,
                              mass = self.mass,
                              velocity = self.velocity)


sun = planet(pos=vec(0,0,0), radius=s_rad1*1.5,
             make_trail=True, mass=2e30,
             velocity=vec(0,0,0))
mercury = planet(pos=vec(ae/3,0,0), radius=s_rad1/1.5,
                 make_trail=True, mass=3.25e23,
                 velocity=vec(0,0,-47000))
#...Other Planets
pluto = planet(pos=vec(ae*39.37,0,0), radius=s_rad1/2.4,
               make_trail=True, mass=1.3e22,
               velocity=vec(0,0,-4740))

planets.extend((mercury, venus, earth, mars, jupiter, saturn, uranus, neptun, pluto))

dt = 10000
time = 0.1

while (True):

    rate(framerate) 
    g_forceS = vec(0,0,0)

    for planet in planets:
        g_force = g * sun.mass * planet.mass * (sun.pos - planet.pos).norm()  / (sun.pos - planet.pos).mag2
        g_forceS -= g_force

        planet.velocity = planet.velocity + ( g_force / planet.mass) * dt 
        planet.pos += planet.velocity * dt 

    sun.velocity = sun.velocity + ( g_forceS / sun.mass) * dt 
    sun.pos += sun.velocity * dt

0 个答案:

没有答案