Nora Bailey Nora Bailey - 3 years ago 92
Python Question

Rotate multiple objects at different rates in VPython at the same time

I am practicing my coding for physics and I am a little stuck on the following problem:

"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)."

A table is provided that gives the radii of the 6 innermost planets and the Sun as well as radii of the planets' orbits and period of the orbits (approximating circular).

I was able to do the first part okay by creating some arrays with the values given in the table (plus picking a constant to make the planets visible in the given scale) and creating an array of spheres.

It's the motion part that I am hung up on. I can make all the planets rotate at the same angular speed at the same time. Or I can make all the planets rotate at different speeds (proportional to the given periods), but it only goes one at a time. Is there a way to make the animations happen simultaneously in VPython? I am using VPython 6.11 and Python 2.7.13. Code below (this is the version that runs them sequentially at different rates).

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

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 =
s[3].color =
s[4].color = color.cyan
s[5].color =

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

Got it! For any future readers, my last code now looks like:

#Orbital Motion
for frame in range(1000):
for n in range(6):
theta = period[n] * frame
x = r[n]*cos(theta)
y = r[n]*sin(theta)
s[n].pos = [x,y]

Answer Source

Something like this:

for frame in range(1000):
    for n in range(6):
        theta = angular_speed[n] * frame
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download