Cannon Balls

[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

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, 2007It 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 

bulletDownload source
bulletDownload  executable

Suggestions for Further Study

Lots of room for improvement here: 

We need better collision detection for the ball and the target, as discussed above.
Done  1/25/07 For low, powder charges, the ball falls right through the barrel and hits the ground!  Must be magic!  It would look much cooler if the ball rolled a ways up the barrel and rolled back down.  This will probably require unique movement code when the ball is within BarrelLength distance of its starting point.
Sound effects would be neat.
A series of concentric ellipses could be drawn on to draw a better looking target.  It should be a TTarget object derived from some graphic object so that it knows how to redraw itself, can be redrawn, etc.   
Same idea applies to the cannon - a better looking cannon object that knows how to redraw itself at any specified angle and position.
The program could be converted to a game by limiting the number of shots for a turn, keeping score, allowing multiple players, adding a level 2 with a slowly moving target, etc.   

That should be enough to keep you busy for a week or two. 

Created: May 12, 2002

Modified: May 15, 2018



  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2018, Gary Darby    All rights reserved.