Bouncing Ball

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]



Search WWW


As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.


Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 


We receive a few cents from each purchase.  Thanks


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa 365 Puzzlers  Calendar 2017

Mensa 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)


Feedback:  Send an e-mail with your comments about this program (or anything else).

Search only




Problem Description

This program demonstrates moving graphics by letting you drop a ball and watching it bounce.   Not real impressive except that it only takes about 40 lines of Delphi code to do it!    That seems pretty impressive to me.  On the other hand it did take several  (fun) hours to get it working.  At one  point I had the whole form bouncing!  

Background & Techniques

First a little physics - gravity makes everything speed up at the same rate.  This means that each second, the speed of something falling increases by the same amount over its speed in the previous second (except feathers and parachutes, but that's a different story).   In our real world, the speed increase (called acceleration)  is about 32 ft/sec each second.  (This is usually written as 32 ft/sec/sec or 32 ft/sec2.)    

In our virtual "bouncing ball world", real gravity doesn't matter much, so I chose to increase velocity by 1 pixel each time through the loop (i.e. acceleration = 1 pixel/loop/loop).  Pixels replace feet and loops replace seconds.  We'll  treat each time through the loop as one time interval.  So the first thing we have to do when the user presses the start button is begin changing the top property of our ball by V pixels each time through the loop.   V is 0 the first time, 1 the second time, etc.  Besides changing the velocity, the loop must also detect when the ball hits the floor and reverse direction.  You can scope that out from the code.  

The other bit of physics is how bouncy the ball is - called the coefficient of elasticity.   It  reflects how much energy is lost when the ball bounces.  When the bottom of the ball (there is no bottom property - it's just top + height), I just multiply the final speed of the ball, Vby this ratio (a number between 0 and 1) and change its sign, to make the ball start moving up.    Moving up, the speed reverses itself automatically at the top of the bounce  since we're adding +1 to a negative velocity number each time through the loop.   Sooner or later the velocity goes positive and we start  moving down again.      

Addendum November 19.2003:  This program was one of our early postings, over three years ago.  Viewer Gerrit de Blaauw from the Netherlands recently found Bug #2, Coefficient of Elastticity =1 did not bounce forever as one would expect.    The problem was in the calculations for the last time interval before  the ball strikes hits the floor.   the new calculation makes sure that the first rebound period matches the interval before contact if the the Coefficient =1.   I also incorporated some techniques developed during the past three years to gracefully handle stopping, etc.   

Running/Exploring the Program 

bulletDownload Source
bulletDownload executable 

Suggestions for Further Explorations

Lots of possibilities here. 

Nov 19. 2003 Done: Bug #1:  There is an error in the coefficient of elasticity calculation.  Put the slider at .5 and notice that the ball bounces considerably less than half as high as its previous height.    It turns out that the height the ball will bounce is proportional to it's energy.  And the energy of a moving object is proportional to velocity squared, i.e. something moving twice as fast has 4 times as much energy.  Put another way, if we slow it down to 1/2 it's original speed  (like the program does when coefficient = .5), then it has only 1/4 the energy.  So if we need to reduce energy by 1/2, we need to reduce velocity by sqrt(1/2) (so that its square is 1/2).  Try replacing the *C with *sqrt(C) in the loop and see how the ball reacts.
Nov 19, 2003 Done: Bug #2:  When Coefficient of elasticity is set to 1.0, the ball should bounce forever.  I doesn't.  I suspect that it's due to the fact that moves are made in whole pixel increments and there are some rounding errors in there.  If you can fix this, let me know and I'll post it.     
Enhancement:  It's fairly easy to add some horizontal motion to the ball as it bounces.  Just change the left coordinate of the ball each time through the loop.  Of course, you need to detect when it hits either wall and reverse the horizontal increment each time that happens.   Start by adding 1 or 2 pixels per loop. Then  maybe add a horizontal speed slider to control sideways speed.
Try removing the Panel1.doubledbuffered:=true; statement to see the effect double buffering has on reducing flicker.  This is a big topic that we will explore further in another project. 
Finally, can you find which two statements to change to make the whole form bounce?
  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2017, Gary Darby    All rights reserved.