# Lab 04 - Momentum and Gravity
# Model the motion of a basketball that is tossed vertically upward & you can
# simulate the ball bouncing by changing init velocity vector to (-y) line 21
students = 'Sunzuu likes the D'
scene = canvas(title = 'Week 4: Momentum & Gravity ' + students,
width = 800, height = 600, background = color.white)
floor = box(length=4, width=1, height=0.1, pos=vector(0, -0.05, 0), color=color.black)
ball = sphere(texture="https://i.imgur.com/gpgs0IL.jpg", emissive=True)
ball.radius = 0.12 # The radius of the basketball in meters
ball.pos = vector(0, 1.5, 0) # The initial position of the ball in m
ball.m = .6063 # The mass of the basketball in kg
ball.v = vector(0,1,0) # The initial velocity of the basketball in m/s
ball.p = ball.m * ball.v # The initial momentum of the basketball in kg m/s
scene.center = ball.pos
PositionGraph = graph(title = 'Position vs Time: ', width = 800, height = 300,
xtitle = 'Time (s)', ytitle='Position (m)', fast = False)
PositionVsTime = gcurve(color = color.blue, width = 2, label = 'Position, y')
MomentumGraph = graph(title = 'Momentum vs Time: ', width = 800, height = 300,
xtitle = 'Time (s)', ytitle = 'Momentum (kg m/s)', fast = False)
MomentumVsTime = gcurve(color = color.red, width = 2, label = 'Momentum, p_y')
g = 9.80
dt = 0.001
t = 0
while(t<5):
rate(1000)
#Part B C Input of updates in while loop as follows:
Fgrav=vector(0,-ball.m*g,0) #
Fnet=Fgrav
pfinal= ball.p + Fnet * dt
ball.pos=ball.pos+(ball.p/ball.m)*dt
if (ball.pos.y < ball.radius):
ball.p.y = abs(ball.p.y)*1
#Part D "Coefficient of Restitution"
t = t + dt
PositionVsTime.plot(t, ball.pos.y)
MomentumVsTime.plot(t, ball.p.y)