VPython Orbital Motion Bugging

262 Views Asked by At

I've tried to visualise the orbits of the solar system and then include an exoplanet orbit using the vpython library.

It works well but at the start of the program and occasionally the orbits will skip their circular motion and instead jump to a different position in the orbit and then carry on. The planetary trails leave sharp edges and cut through other orbits.

I've uploaded a screenshot of the issue to show - img

I've attached my code below this, if possible could anyone help me solve this issue? All help is massively appreciated, thanks.

(Apologies for the code, I'm learning as I've made this and I'm trying to improve it)


    ##  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, 0)
mercurypos = vector((5.79e9+starrad), 0, 0)
venuspos = vector((1.082e10+starrad), 0, 0)
earthpos = vector((1.496e10+starrad), 0, 0)
marspos = vector((2.279e10+starrad), 0, 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,0), color=color.white, visible = True)


    ##  Objects
star=sphere(color=color.orange,pos=vec(0,0,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, shininess=10, make_trail=True, opacity = 0.5)
venus=sphere(color=color.gray(0.75),pos=venuspos,radius=venusrad, shininess=10, make_trail=True, opacity = 0.5)
earth=sphere(color=color.gray(0.75),pos=earthpos,radius=earthrad, shininess=10, make_trail=True, opacity = 0.5)
mars=sphere(color=color.gray(0.75),pos=marspos,radius=marsrad, shininess=10, make_trail=True, opacity = 0.5)
ring1 = ring(pos= vec(0,0,0), axis = vec(0,1,0), radius = chz_inner, thickness = chz_thickness, color = color.green, opacity = 0.75)
ring2 = ring(pos= vec(0,0,0), axis = vec(0,1,0), radius = chz_outer, thickness = chz_thickness, 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', xoffset=10,
    line = False, yoffset=10, height=12, 
    border=3, font='sans')

mercury_label = label(pos=mercury.pos,
    text='Mercury', xoffset=10,
    line = False, yoffset=10, height=12, 
    border=3, font='sans')

venus_label = label(pos=venus.pos,
    text='Venus', xoffset=10,
    line = False, yoffset=10, height=12, 
    border=3, font='sans')

earth_label = label(pos=earth.pos,
    text='Earth', xoffset=10,
    line = False, yoffset=10, height=12, 
    border=3, font='sans')

ring_label = label(pos=(ring1.pos+vector(0, 0, ring1.radius)),
    text='CHZ Inner', xoffset=2,
    line = False, yoffset=10, height=12, 
    border=3, font='sans')

ring_label2 = label(pos=(ring2.pos+vector(0, 0, ring2.radius)),
    text='CHZ Outer', xoffset=2,
    line = False, yoffset=10, height=12, 
    border=3, 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,1,0), origin=vector(0,0,0))
  exoplanet.rotate(angle=(2*3.14159/exoplanetperiod), axis=vector(0,1,0), origin=vector(0,0,0))
  mercury.rotate(angle=(2*3.14159/mercuryperiod), axis=vector(0,1,0), origin=vector(0,0,0))
  venus.rotate(angle=(2*3.14159/venusperiod), axis=vector(0,1,0), origin=vector(0,0,0))
  earth.rotate(angle=(2*3.14159/earthperiod), axis=vector(0,1,0), origin=vector(0,0,0))
  mars.rotate(angle=(2*3.14159/marsperiod), axis=vector(0,1,0), origin=vector(0,0,0))
  g1.plot((t, exoplanet.pos.z))
  g2.plot((t, exoplanet.pos.x))
1

There are 1 best solutions below

0
On

This is pretty subtle. I assume you are using the vpython module with installed Python, since I don't see the problem with GlowScript VPython. The issue is the rate statement. Given the significant amount of setup, the rate(100) at the start of the loop tries to catch up and runs the loop at a rate that is higher than 100 per second. A point is added to the curve representing the trail only every time a new image is displayed, with the effect that you can get increments to the curve that are far apart. Two ways to improve the situation: 1) put "sleep(1)" just before the loop, which will recalibrate the rate function, and 2) exit the loop after the last orbit has closed. Another option would be to not to use make_trail but rather set up curves and add a point to a curve when you move the planet, rather than depending on points being added only when a new display is generated.