#Exercise 1
from vpython import *
from math import *
import matplotlib.pyplot as plt
%matplotlib inline
scene=canvas(title='Ball Spring Launcher') #Sets title for graphics window
scene.range=5 #Determines how wide graphics window is (sorta)
scene.center=vector(0,0,0)
dt = 0.01 #dt is the time step 'Delta t'
t=0 #Set initial time to zero
g = vector(0,-9.8,0)
h = 2 #initial height of ball
v_init=10
ball = sphere(pos=vector(0,h,0), radius=.2, color=color.red,make_trail=True) #Create sphere that will appear on screen
ground = box(pos=vector(0,0,0),color=color.green,size=vector(10,.1,5)) #Create green "grass" to give a reference frame
table = box(pos=vector(-0.5,h/2,0), color=color.blue, size=vector(1,h,1)) #Create a "table" for reference
ball.m = 1 #Mass of ball
ball.v = vector(v_init,0,0) #Initial velocity vector of ball
scene.waitfor("click") #Don't go onto the next step until you have clicked on the screen
while ball.pos.y>0: #Stop when ball hits floor
rate(40) #Determines how quickly program runs (roughly 30 frames per second)
ball.pos = ball.pos + ball.v*dt #Update position of ball
ball.v = ball.v + g*dt #Update velocity of ball
t+=dt #Calculate total time elapsed
print("Time elapsed = ", t)
print("Horizontal distance traveled = ", ball.pos.x)