Assignment 3
Assignment 3
📝 Worth: 8%
📅 Due: November 28, @Midnight
🕑 Late submissions: 10% penalty per late day. Maximum of 3 late days allowed.
📥 Submission on Gradescope:
Include:
Objectives
Create and access lists
Modules
Ensure that you have installed (if necessary) the following modules:
matplotlib
A bouncing ball
Context:
In this exercise we will be dropping a bouncy ball and observe it bounce back up virtually. The goal
is to use a computer program to simulate the motion of the ball as it repeatedly hits the ground
and bounces back up repeatedly. In this simulation, we will use a timestamp (one frame at a time)
to calculate the position and velocity and storing them in list!
Physics concepts in relations to programming variable
v(t) the velocity vector at each moment t , it is positive when the ball is moving up and
negative when the ball is moving down current_velocity
v(t - Δt) the velocity vector at the moment t- Δt will be called previous_velocity
For each given time period Δt you can obtain the current position and velocity using the previous
position and velocity:
If the ball hits the ground, we will simulate the kinetic energy loss by multiplying the velocity by a
constant k:
You can assume that k= 0.84, you can test out different values if you prefer later on.
current_velocity = -k * current_velocity
2.1 Write a function position() (2pts)
Implement equation (1.2) provided with
Input parameters:
previous_position
current_position
delta_t
Returns:
current_position
Input parameters:
Returns:
Hint: After completing 2.4, detecting when the ball touches the ground may required more
careful consideration and adding the velocity and time. This is part of the bonus.
Input parameters:
Returns:
start_velocity v(t=0)
start_position p(t=0)
Returns:
Initialization:
Initialize three lists: time_list , velocity_list and position_list , starting with the first
values provided as parameters and set t0=0:
time_list = [0]
velocity_list = [start_velocity]
position_list = [start_position]
ball = turtle.Turtle()
ball.penup()
ball.shape('circle')
Use multiple samples per second for smoother simulation, starting with Δt = 0.1 (you can
improve it later to get a more realistic simulation)
n = total_duration / delta_t
Motion
Compute the current time t from the previous value ti-1 (Hint: use time_list[-1] to get
the previous time:
Calculate the current_velocity using the function defined in 2.2 provided with the
previous_position and current_velocity
start_velocity = 0
start_position = 220.0
total_duration = 90
has_hit_ground() isn't detecting ground collisions. Print the position of the ball at
each iteration and use the debugger to help.
Once, you got the simulation working you can try to do the bonus to improve it.
On a separate graph, use time_list and position_list to graph the position over time.
Add a horizontal line at y=0 using plt.axhline(0,color='red',linestyle='--')
Are you observing at which height is the ball bouncing back up?
Relate what you observe on this graph with the strategy hit collision algorithm and possibly
improve it (bonus - below). Put your answer in a submitted pdf.
In this bonus question, you are tasked with improving the algorithm to make it appear more
realistic.
Modify the function has_hit_ground to include more parameters. It must still return a
boolean.
Hint: We know that the if the ball was able to go through the ground, it's position would be
negative in the next time frame.
Use break to stop the loop once the velocity stabilizes (doesn't significantly change)