#Exercise 3

from vpython import *

from math import *

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

theta=pi/4

v_init=5

v=vector(v_init*cos(theta),v_init*sin(theta),0)

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 = v #Initial momentum vector of ball

scene.waitfor("click") #You must click on the image before it will start moving

while ball.pos.y>0+ball.radius: #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 momentum of ball

t+=dt #Calculate total time elapsed

print("Time elapsed = ", t)

print("Horizontal distance traveled = ", ball.pos.x)