/bouncing-ball

Primary LanguagePythonMIT LicenseMIT

Exploding Bouncing Balls with Gravity

A Python program that simulates balls bouncing with realistic physics, featuring an explosive chain reaction system where balls split into multiple smaller balls when they hit surfaces.

Features

  • Realistic gravity physics with bounce effects
  • Explosive chain reactions - balls split into smaller balls on impact
  • Colorful visual effects - each explosion creates balls of different colors
  • Progressive ball splitting - larger balls create more smaller balls
  • Performance optimization - limits total balls to prevent lag
  • Real-time ball counter and position display
  • Interactive controls for resetting the simulation

Installation

  1. Make sure you have Python 3.11+ installed

  2. Install the required dependencies:

    pip install -r requirements.txt

Running the Program

python bouncing_ball.py

Controls

  • SPACE: Reset to a single ball
  • ESC: Quit the program
  • Close window: Exit the program

Physics Parameters

You can modify these constants in the code to change the behavior:

  • GRAVITY = 0.5: Strength of gravity (pixels per frame squared)
  • BOUNCE_FACTOR = 0.8: Energy retention on bounce (0.0 to 1.0)
  • FRICTION = 0.99: Air resistance factor (0.0 to 1.0)
  • SPLASH_COUNT = 8: Number of balls created per explosion
  • SPLASH_VELOCITY = 8: Initial velocity of explosion balls
  • MIN_BALL_RADIUS = 3: Minimum size before balls stop exploding

How the Explosion System Works

  1. Initial Ball: Starts as a large red ball at the top
  2. First Impact: When the ball hits the ground with sufficient velocity, it explodes
  3. Chain Reaction: Each explosion creates 8 smaller balls in different colors
  4. Progressive Splitting: Larger balls create more dramatic explosions
  5. Size Limit: Balls smaller than 6 pixels radius stop exploding
  6. Performance: System limits to 100 balls maximum to maintain smooth performance

Visual Effects

  • Color Variety: Explosions create balls in red, blue, green, orange, purple, and yellow
  • Size Scaling: Explosion velocity scales with the original ball's size
  • Random Distribution: Balls explode in a circular pattern with slight randomness
  • Real-time Stats: Shows current ball count and main ball position

Physics Simulation

The program uses realistic physics including:

  • Gravity: Continuous downward acceleration
  • Collision Detection: Bounces off all window boundaries
  • Energy Conservation: Bounce factor reduces energy on each collision
  • Friction: Air resistance gradually slows movement
  • Explosion Physics: Momentum conservation in splash patterns

Watch as a single ball creates a spectacular chain reaction of colorful explosions!

Copyright

See LICENSE for details. Copyright (c) 2025 Dat Tran.