Twin Paradox in Vpython

User Generated

llp567

Science

Description

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.

Additional features invited

• Crash detection

• Output of final times

• Force player to land again

Use python 2.7 will work.

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()
# This is where you have to add all your keys
# "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
earth=sphere(pos=(0,0,0),radius=1.2,color=color.blue);
# Make some stars to look cute
sphere(pos=(5,2,0),radius=0.1,color=color.white);
sphere(pos=(-5,3,0),radius=0.1,color=color.white);
sphere(pos=(5,-5,0),radius=0.1,color=color.white);
sphere(pos=(2,7,0),radius=0.1,color=color.white);
sphere(pos=(3,2,0),radius=0.1,color=color.white);
sphere(pos=(-3,-4,0),radius=0.1,color=color.white);
# Make the ship
ship=sphere(pos=(1.2,0,0),radius=0.2,color=color.red);
# 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 

User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

This question has not been answered.

Create a free account to get help with this and any other question!

Related Tags