Bouncing ball simulation in VPython

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.

Physics behind bouncing ball

If a freely bouncing ball from a certain height hh hits the ground, it loses some energy by some factor (known as the damping factor). This factor prevents the ball from reaching the exact height after each bounce. With each bounce, the height decreases until the ball rests on the ground.

A ball thrown from a height h
A ball thrown from a height h

As the ball descends, its velocity and acceleration will be positive; however, upon rebound and ascent, they will become negative.

Code execution

To execute the provided example code, follow the steps mentioned below:

Once you click the "Run" button, follow the encircled link
1 of 4

Example code

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 * dt
Bouncing ball simulation code

Code explanation

  • Line 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 9.8ms2-9.8ms^{-2} is stored in the gravity variable to represent gravitational acceleration.

  • Line 17: dt stores the instantaneous timestamps.

  • Line 19: The damping factor is set to 0.850.85 units. We can change it as per our requirements.

  • 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 S=v×tS=v\times t (where SS is the distance, vv is the velocity, and tt is the time), the ball's height is being updated according to 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.

Continue reading

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved