[Home] [Puzzles & Projects] [Delphi Techniques] [Math Topics] [Library] [Utilities]
Here's an animated graphics program that builds on the bouncing ball program by adding some horizontal movement to the ball, drawing a primitive cannon that you can aim, a target that can be moved and an adjustable powder charge to give the initial push to the ball.
Background and techniques
The rotate and translate routines developed in the Rotate A Square program are used here to elevate the cannon. The ball movement loop is similar to the Bouncing Ball program with the addition of a horizontal component. Initial velocities in the X and Y direction are proportional to the cosine and sine of the elevation angle respectively. I'll defer further discussion of that for a Math Topic page. (The text really needs some illustrations that I haven't developed yet.)
Let's see, what else is worth mentioning. The simplest form of drag and drop logic is used here to move the target. The trick is to set the DragMode property to dmAutomatic on the thing you want to drag, and define DragOver and DragDrop exits for anything that the dragged object may be dropped onto. DragOver has to set a flag saying, "Yeah, it's OK if you want to drop that thing here" and DragDrop's job is to actually change the location of the thing to the new location.
The only other logic is to determine whether the cannonball has hit the target. "Collision detection" is a common (and complicated) problem in most animated graphics applications. I haven't studied up on it yet, so just did a "quick and dirty" implementation checking if the distance from the center of the cannonball is less than its radius from the left or top edges of the target after each move. The problem is that, for low angles, the horizontal movement may take the ball from one side of the target to the other side in one loop increment, so we never know that we went right through it! Oh well, I have to leave something for you readers to work on.
Addendum January 25, 2007: It has been several years since this program was posted. Over the recent Christmas holiday I was showing the program to 10 year old Grandson Luke and he also noticed that, with small powder charges, the cannonball just fell through the barrel. So I decided to make it a little more realistic by constraining the cannon ball until it exited the barrel. Ah, another adventure begins. Playing around with the program after I had made the changes, I noticed that the maximum range no longer occurred at 45° as expected. After spending a few days trying to find my bug, I concluded that maximum range of an initially constrained projectile does indeed travel furthest at an angle less than 45°. I have spent several more days, without success, trying to find a concise expression which returns range as a function of barrel length, initial velocity and firing angle. I can solve the problem analytically for a specific set of values by dividing the flight into three sections (start to end of barrel, end of barrel to top of path, top of path to ground). I've included a "Statistics" section in the current posting, Cannonballs V3, which lists simulation and theoretical results.
Running/Exploring the Program
Suggestions for Further Study
Lots of room for improvement here:
That should be enough to keep you busy for a week or two.
Copyright © 2000-2013, Gary Darby
All rights reserved.