A bouncing ball refers to a physical scenario where a ball is dropped from a certain height and repeatedly bounces off the ground due to the force of gravity.
Each bounce causes the ball to rebound to a slightly lower height than the previous one due to energy losses from friction and inelastic collisions. Such bouncing is called damped oscillation.
If a freely bouncing ball from a certain height
As the ball descends, its velocity and acceleration will be positive; however, upon rebound and ascent, they will become negative.
To execute the provided example code, follow the steps mentioned below:
The following example code simulates a ball bouncing from the ground:
from vpython import *
# Create a 3D scene
scene = canvas()
# Set the floor
floor = box(pos=vector(0, 0, 0), size=vector(10, 0.1, 10), color=color.green)
# Create a bouncing ball
ball = sphere(pos=vector(0, 5, 0), radius=1, color=color.blue)
ball.velocity = vector(0, -1, 0) # Initial velocity
# Set the gravity
gravity = vector(0, -9.8, 0) # Acceleration due to gravity
# Time step for the animation loop
dt = 0.01
damping_factor = 0.85 # Adjust this value to control damping
# Animation loop
while ball.velocity != vector(0, 0, 0):
rate(100) # Limit the loop rate to ensure smooth animation
# Update the ball's position
ball.pos += ball.velocity * dt
# Check for collisions with the floor
if ball.pos.y < ball.radius:
ball.velocity.y = abs(ball.velocity.y) * damping_factor # Reverse the y-component of velocity with damping
else:
ball.velocity.y += gravity.y * dtLine 1: Importing VPython library.
Line 4: The box object creates a floor from where the ball will bounce off.
Line 10: The sphere object creates the ball under observation.
Line 11: The velocity vector assigns a negative velocity in y-direction to the ball.
Line 14: An acceleration of gravity variable to represent gravitational acceleration.
Line 17: dt stores the instantaneous timestamps.
Line 19: The damping factor is set to
Line 22: The loop will keep running until the ball comes to the rest.
Line 23: Sets the frame per second rate.
Line 26: Since ball.pos += ball.velocity * dt. The negative initial velocity will decrease the height as time passes.
Lines 29–32: The code checks if the ball hits the ground. If it does, its velocity becomes positive but decreases by a slight damping_factor. Otherwise, the ball keeps on falling freely.
Free Resources