在 VPython 中同时以不同的速率旋转多个对象
Rotate multiple objects at different rates in VPython at the same time
我正在练习我的物理编码,我有点卡在以下问题上:
"Using the facilities provided by the visual package, create an animation of the solar system that shows...Sun and planets as spheres in their appropriate positions and with sizes proportional to their actual sizes...and motion of the planets as they move around the Sun (by making the spheres of the planets move)."
提供了一个table,给出了最里面的6颗行星和太阳的半径,以及行星轨道的半径和轨道周期(近似圆形)。
通过使用 table 中给定的值创建一些数组(加上选择一个常数以使行星在给定比例下可见)并创建一个球体数组,我能够很好地完成第一部分.
我挂的是动作部分。我可以让所有行星同时以相同的 angular 速度旋转。或者我可以让所有行星以不同的速度旋转(与给定的周期成正比),但一次只能旋转一个。有没有办法让动画在 VPython 中同时发生?我正在使用 VPython 6.11 和 Python 2.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]
知道了!对于任何未来的读者,我最后的代码现在看起来像:
#Orbital Motion
for frame in range(1000):
for n in range(6):
theta = period[n] * frame
rate(60)
x = r[n]*cos(theta)
y = r[n]*sin(theta)
s[n].pos = [x,y]
像这样:
for frame in range(1000):
for n in range(6):
theta = angular_speed[n] * frame
...
我正在练习我的物理编码,我有点卡在以下问题上:
"Using the facilities provided by the visual package, create an animation of the solar system that shows...Sun and planets as spheres in their appropriate positions and with sizes proportional to their actual sizes...and motion of the planets as they move around the Sun (by making the spheres of the planets move)."
提供了一个table,给出了最里面的6颗行星和太阳的半径,以及行星轨道的半径和轨道周期(近似圆形)。
通过使用 table 中给定的值创建一些数组(加上选择一个常数以使行星在给定比例下可见)并创建一个球体数组,我能够很好地完成第一部分.
我挂的是动作部分。我可以让所有行星同时以相同的 angular 速度旋转。或者我可以让所有行星以不同的速度旋转(与给定的周期成正比),但一次只能旋转一个。有没有办法让动画在 VPython 中同时发生?我正在使用 VPython 6.11 和 Python 2.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]
知道了!对于任何未来的读者,我最后的代码现在看起来像:
#Orbital Motion
for frame in range(1000):
for n in range(6):
theta = period[n] * frame
rate(60)
x = r[n]*cos(theta)
y = r[n]*sin(theta)
s[n].pos = [x,y]
像这样:
for frame in range(1000):
for n in range(6):
theta = angular_speed[n] * frame
...