我正在尝试使用vpython创建一个轨道模拟器,但是当我运行它时,我只会看到黑屏

时间:2018-11-06 11:13:47

标签: python simulation physics vpython orbital-mechanics

我使用youtube视频作为代码的基础,并对其进行了调整,使其包含类和对象。视频中的原始代码非常有效。 我的代码版本返回黑屏,即使试图对其进行修复,我得到的最大的成功还是显示了两个没有移动的对象。 我也尝试过在glowscript IDE和winpython上运行它。 感谢任何可以提供帮助的人!

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()

视频原始代码:

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:任何物理校正也将不胜感激!

1 个答案:

答案 0 :(得分:1)

太阳的(中心)与行星的(中心)之间的距离仅为200米,因此计算出的力是巨大的,新的v大约为10到38,所以行星距离太阳太远,因此相机向后移动WAY以尝试显示整个场景,但由于物体现在距离很远,因此屏幕显示为黑色。