Search

 Search WWW Search DelphiForFun.org

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.

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

Contact

 Search DelphiForFun.org 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.