我使用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
附言:任何物理修正也将不胜感激!
太阳(中心(和行星(中心(之间的距离只有200米,所以计算出的力是巨大的,新的v是10到38的数量级,所以行星离太阳太远了,相机立刻向后移动,试图显示整个场景,因为物体现在离得太远了。