问题描述
我尝试将太阳系的轨道可视化,然后使用 vpython 库包含一个系外行星轨道。
它运行良好,但在程序开始时,有时轨道会跳过它们的圆周运动,而是跳到轨道上的不同位置,然后继续运行。行星轨迹留下锋利的边缘并穿过其他轨道。
我在下面附上了我的代码,如果可能的话,有人能帮我解决这个问题吗?非常感谢所有帮助,谢谢。
(为代码道歉,我正在学习,我正在努力改进它)
## Constants
# Radii:
starrad = 696.34e6
exoplaneTrad = 3.8e6
mercuryrad = 2.44e6
venusrad = 6.05e6
earthrad = 6.38e6
marsrad = 3.4e6
# Position:
exoplanetpos = vector((8e9+starrad),0)
mercurypos = vector((5.79e9+starrad),0)
venuspos = vector((1.082e10+starrad),0)
earthpos = vector((1.496e10+starrad),0)
marspos = vector((2.279e10+starrad),0)
# Orbital Periods (Days):
exoplanetperiod = 170
mercuryperiod = 88
venusperiod = 224.7
earthperiod = 365.2
marsperiod = 687
# CHZ Attributes
chz_inner = ((0.75*mag(earthpos)) + starrad)
chz_outer = ((1.7*mag(earthpos)) + starrad)
chz_thickness = (0.009*mag(earthpos))
# Time
t = 0
deltat = 0.1
# Scene and Lights
#scene.autoscale = 0 # Removes autoscaling for better visuals
star_light = local_light(pos=vector(0,0),color=color.white,visible = True)
## Objects
star=sphere(color=color.orange,pos=vec(0,radius=starrad,emissive=True,texture = "http://i.imgur.com/yoEzbtg.jpg")
exoplanet=sphere(color=color.cyan,pos=exoplanetpos,radius=exoplaneTrad,shininess=10,make_trail=True)
mercury=sphere(color=color.gray(0.75),pos=mercurypos,radius=mercuryrad,make_trail=True,opacity = 0.5)
venus=sphere(color=color.gray(0.75),pos=venuspos,radius=venusrad,opacity = 0.5)
earth=sphere(color=color.gray(0.75),pos=earthpos,radius=earthrad,opacity = 0.5)
mars=sphere(color=color.gray(0.75),pos=marspos,radius=marsrad,opacity = 0.5)
ring1 = ring(pos= vec(0,axis = vec(0,1,radius = chz_inner,thickness = chz_thickness,color = color.green,opacity = 0.75)
ring2 = ring(pos= vec(0,radius = chz_outer,color = color.red,opacity = 0.75)
## Labels
star_label = label(pos=star.pos,text='Star',xoffset=10,line = False,yoffset=10,height=12,border=3,font='sans')
exoplanet_label = label(pos=exoplanet.pos,text='Exoplanet',font='sans')
mercury_label = label(pos=mercury.pos,text='Mercury',font='sans')
venus_label = label(pos=venus.pos,text='Venus',font='sans')
earth_label = label(pos=earth.pos,text='Earth',font='sans')
ring_label = label(pos=(ring1.pos+vector(0,ring1.radius)),text='CHZ Inner',xoffset=2,font='sans')
ring_label2 = label(pos=(ring2.pos+vector(0,ring2.radius)),text='CHZ Outer',font='sans')
## Graphs
gd = graph(width=635,height=600,xtitle='Time',ytitle='Position',foreground=color.gray(0.5),background=color.white,xmax=20,xmin=0,ymax=6e10,ymin=-6e10)
g1 = gcurve(color=color.red)
g2 = gcurve(color=color.cyan) # a graphics curve
while True:
rate(100)
t = t + deltat
star.rotate(angle=(2*3.14159/365),axis=vector(0,origin=vector(0,0))
exoplanet.rotate(angle=(2*3.14159/exoplanetperiod),0))
mercury.rotate(angle=(2*3.14159/mercuryperiod),0))
venus.rotate(angle=(2*3.14159/venusperiod),0))
earth.rotate(angle=(2*3.14159/earthperiod),0))
mars.rotate(angle=(2*3.14159/marsperiod),0))
g1.plot((t,exoplanet.pos.z))
g2.plot((t,exoplanet.pos.x))
解决方法
这很微妙。我假设您正在使用安装了 Python 的 vpython 模块,因为我没有看到 GlowScript VPython 的问题。问题是利率声明。考虑到大量的设置,循环开始时的 rate(100) 试图赶上并以高于每秒 100 的速率运行循环。仅在每次显示新图像时,都会在表示轨迹的曲线上添加一个点,其效果是您可以获得相距较远的曲线的增量。改善这种情况的两种方法:1) 在循环之前放置“sleep(1)”,这将重新校准速率函数,以及 2) 在最后一个轨道关闭后退出循环。另一种选择是不使用 make_trail 而是设置曲线并在移动行星时向曲线添加一个点,而不是依赖于仅在生成新显示时添加的点。