[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
|
|
Problem DescriptionThis 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 & TechniquesFirst 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, V, by 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
Suggestions for Further ExplorationsLots of possibilities here.
|
[Feedback] [Newsletters (subscribe/view)] [About me]Copyright © 2000-2018, Gary Darby All rights reserved. |