Write a space ship simulation to observe the “Twin Paradox.” Treat each time step
using Special Relativity. The ship should be controlled using keys, keeping track of
Earth-time and on-board time.More info in files.Framework as follow:from visual import *
# The timestep length in Earth frame(might need to be adjusted)
dt=0.01
# The rate
dtrate=100
# The velocity change
dv=0.05
# The beta values
bx=0.0
by=0.0
beta=0.0
# Speed limit
maxbeta=0.9999
# Drag (if desired)
drag=0.9999;
# Time on Earth and in the Ship
tearth=0.0
tship=0.0
#
# ========================================================
# ========================================================
#
# Listen for keys
# Modify this for interactions
#
def listenkeys():
global bx,by,beta,maxbeta,dv
# Was a key pressed?
if scene.kb.keys:
# Yes, which one?
s=scene.kb.getkey()
# “x” exits
if s==’x’:
exit()
# Deal with the velocity keys to modify bx, by
# …
# Enforce maximum speed
# …
# Calculate beta
#
# The routine that displays your values
# Just for demo
def displayvalues():
global beta,tearth,tship
betalabel.text=”Beta: %.4f” % beta
earthtime.text=”Earth: %.4f” % tearth
shiptime.text=”Ship: %.4f” % tship
# Move the ship
def moveship():
global bx,by,dt,drag,beta
# Move the ship
# …
# Apply drag
# …
# Recalculate beta after drag
# …
# ========================================================
# Main program
# Setting up things
# Make the Earth
# Make some stars to look cute
# Make the ship
# Make the beta label
betalabel=label(pos=(-6.5,6.5,0),text=””,xoffset=1,line = 0)
earthtime=label(pos=(-6.5,5.5,0),text=””,xoffset=1,line = 0)
shiptime=label(pos=(-6.5,4.5,0),text=””,xoffset=1,line = 0)
# Keep the scene from expanding
scene.autoscale=False
# Infinite loop
while True:
rate(dtrate)
listenkeys()
displayvalues()
moveship()
# Calculate time in Earth frame and Ship frame
# …