1C – Tossing a Basketball

You will model a basketball that is tossed vertically in the air and allowed to bounce on a floor.

Begin by copying and pasting the starter code in the box below into a new GlowScript program.  This code will draw a rectangular box named floor to simulate the floor and a sphere named ball to simulate the basketball. Initially the ball will be displayed stationary and suspended above the floor. You will make changes (described below in steps A thru D) that will realistically simulate the motion of the ball as it is thrown vertically upward and allowed to bounce up and down on the floor.

A – Characterize the Basketball

Measure and enter appropriate values for the attributes of the basketball including its mass (ball.m), initial position (ball.pos), and initial velocity (ball.v).  Use your data to determine a realistic value for the initial velocity of the ball at the moment it leaves your hand.

B – Model the Force on the Basketball

Write a vector expression for the force on the basketball while it is in freefall (only acted upon by Earth’s gravity).  A constant g = 9.8 has been defined in the code that you can use here for the strength of the gravitational field \(g = 9.8 \textrm{ m/s}^2\).

C – Update the Basketball Momentum and Position

Use the momentum principle and the definition of average velocity to advance the momentum and the position of the basketball forward in time by the time step dt.

D – Model the Bounce on the Floor

The bounce is presently modeled by considering the interaction of the ball with the floor to be a very rapid (nearly instantaneous) event that causes the vertical momentum of the ball to change from downward to upward.  As you will notice this model isn’t particularly realistic because the ball continues bouncing to its maximum height each time.  Use your data to modify this model by multiplying the initial momentum by a scale factor less than 1 to obtain the final momentum. This scale factor is often called the coefficient of restitution.  You could use the ratio of the speed just after the bounce to the speed just before the bounce.  Or, you might want to to use the ratio of the successive heights the ball reaches.  If you use the height ratio you need to realize that the speed is proportional to the square root of the height.

Notice that the bounce is detected by using an if statement that tests for the ball touching the floor by seeing if the position of the ball is less than the ball’s radius.

B  (revisited) – Improving the Force Model to include the Toss

If time permits you can return to the area where the force is defined and add code that will also model the toss. Treat the force that you exert during the toss as constant even though this is not entirely realistic. Using your data, you can estimate the duration of the toss and the change in momentum during the toss. You can then determine an average force during the toss by substituting these values into the momentum principle and solving for the force.

To add the toss to your model you should first set the initial velocity of the ball to zero (under comment A). Then, you can use an if statement to test if the elapsed time is less than the duration of the toss and if it is then set the force to the upward force you found from the momentum principle.  Otherwise, set the force equal to the gravitational force on the ball by the Earth. The following pseudo code can be used as a guide for how to use the if statement in this context.

if (t < toss_duration):
F = upward net force on the ball during the toss
else:
F = downward force of gravity on the ball by the Earth

#
# Lab 04 - Momentum and Gravity
#
# Model the motion of a basketball that is tossed vertically upward
#

#################################################################
#                                                               #
# Enter your names in the variable 'students' as in:            #
# students = 'Sally Smith and Johnny Jones'                     #
#                                                               #
#################################################################
students = 'Enter Student Names Here'
scene = canvas(title = 'Motion on Tilted Air track: ' + students, 
                width = 800, height = 600, background = color.white)

# Create a floor to be able to see the ball bounce from.  The top of the floor
# will have a y-coordinate of 0.
floor = box(length=4, width=1, height=0.1, pos=vector(0, -0.05, 0), color=color.black)

#################################################################
#                                                               #
# A - Make sure the mass and the initial conditions for the     #
#     basketball are correct.                                   #
#                                                               #
#################################################################
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 = 0.5                            # 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

# The following will put the initial position of the basketball at the 
# center of the scene.  This way more of the canvas output area is used.
scene.center = ball.pos

# Create plots to show the position and momentum as a function of time
# Position vs Time Graph
PositionGraph = graph(title = 'Position vs Time: '+students, width = 800, height = 300,
                      xtitle = 'Time (s)', ytitle='Position (m)', fast = False)
PositionVsTime = gcurve(color = color.blue, width = 2, label = 'Position, y')

# Momentum vs Time Graph
MomentumGraph = graph(title = 'Momentum vs Time: '+students, 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')

# Define some necessary constants
g  = 9.80
dt = 0.001
t  = 0

# Run the simulation for 5 seconds
while(t<5):
    rate(1000)
    
    #########################################################################
    #                                                                       #
    # B - Enter an expression for the vector force F on the ball.           #
    #                                                                       #
    #########################################################################
    
    
    #########################################################################
    #                                                                       #
    # C - Enter appropriate code to update the momentum (ball.p) and        #
    #     position (ball.pos) of the basketball for the next time step dt.  #
    #                                                                       #
    #########################################################################
    
    
    
    #########################################################################
    #                                                                       #
    # D - The collision between the ball and the floor is modeled below as  #
    #     a nearly instantaneous event that changes the direction of the    #
    #     vertical component of momentum.                                   #
    #                                                                       #
    #     You can improve upon this by multiplying by a factor between      #
    #     0 and 1, often called the coefficient of restitution, that will   #
    #     also reduce the magnitude of the momentum.  Attempt to use your   #
    #     data to get a quality value for this factor.                      #
    #                                                                       #
    #########################################################################
    if (ball.pos.y < ball.radius):
        ball.p.y = abs(ball.p.y)

    # Increment the time for use in the plots of y vs t and py vs t
    t = t + dt
    
    # Update the plots
    PositionVsTime.plot(t, ball.pos.y)
    MomentumVsTime.plot(t, ball.p.y)