在VPython中同一时间以不同速率旋转多个对象

2024-06-16 09:19:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在练习物理编码,我有点困在以下问题上:

“利用视觉软件包提供的工具,制作一个太阳系动画,显示太阳和行星在适当的位置,大小与它们的实际大小成比例……以及行星围绕太阳运行时的运动(通过使行星的球体移动)。”

提供了一个表格,列出了最里面的6颗行星和太阳的半径,以及行星的轨道半径和轨道周期(近似于圆形)。在

第一部分我可以用表中给出的值创建一些数组(加上选择一个常量使行星在给定的比例下可见)并创建一个球体数组。在

这是我最关心的动作部分。我可以让所有的行星同时以相同的角速度旋转。或者我可以让所有的行星以不同的速度旋转(与给定的周期成比例),但每次只运行一个。有没有办法让动画同时在VPython中发生?我使用的是vpython6.11和python2.7.13。下面的代码(这是以不同速率顺序运行它们的版本)。在

from visual import sphere,rate,color
from math import cos,sin,pi
from numpy import arange,array,empty

#Sun
s0 = sphere(radius=45e6,pos=[0,0,0],color=color.yellow)

#Given Data
r = array([57.9e6,108.2e6,149.6e6,227.9e6,778.5e6,1433.4e6],int)
r_plan = array([2440,6052,6371,3386,69173,57316],int)
r_plan = r_plan*3000 #adjusting scale
period = array([88.0,224.7,365.3,687.0,4331.6,10759.2],float)
period = (88.0/period)*.8 #adjusting scale
s = empty(6,sphere)

#Creating the planets
for n in range(6):
    s[n] = sphere(radius=r_plan[n],pos=[r[n],0])

#Prettifying the different planets
s[0].color = color.magenta
s[2].color = color.green
s[3].color = color.red
s[4].color = color.cyan
s[5].color = color.blue

#Orbital Motion
for n in range(6):
    m = period[n]
    for theta in arange(0,10*pi,m):
        rate(30)
        x = r[n]*cos(theta)
        y = r[n]*sin(theta)
        s[n].pos = [x,y]

明白了!对于未来的读者来说,我的最后一段代码如下所示:

^{pr2}$

Tags: infromposimportfor动画行星array