In our last post, we implemented projectile motion as the start of a game involving monkeys throwing bananas. We saw that projectile motion always follows the same parabolic pattern, which will look something like this:

Since the projectile follows a straightforward motion pattern, we can actually predict, mathematically, where the projectile will land. To do this, let’s work out where the projectile will be, a certain number of frames after it has launched, given an initial velocity of horizontally, and vertically, with gravity set to some value .

#### Horizontal Position

Recall that our horizontally velocity stayed constant — the banana moves the same horizontal distance each frame:

The distance that the projectile will have moved horizontally is therefore very simple:

- After 0 frames:
- After 1 frame:
- After 2 frames:
- After 3 frames:

It’s not hard to see that in general, the horizontal position after a given number of frames, , will be:

#### Vertical Position

The vertical position of the projectile is more complex. The distance it moves (initially ) reduces by each frame:

So, here’s the distance the projectile will have moved after the first few frames:

- After 0 frames:
- After 1 frame:
- After 2 frames:
- After 3 frames:
- After 4 frames:

So the overall pattern is that the position will be:

The last part describes the sum of the numbers from 1 up to — that’s the (1+2+3) part from our pattern above. This need to sum up the numbers is the classic example for summation (wikipedia currently has it as its example for summation), and has the well-known result in mathematics that summing the numbers from 1 to n is equal to , which we can use here (n being ):

#### Predicting the landing

So we now have formulae for the horizontal and vertical distance of the projectile (in our case, a banana). The question we want to answer is: where will the banana land? For this, we use the fact that in our game, the banana only lands when the vertical distance reaches zero again (i.e. when the banana returns to ground level), so we can use that to work out how many frames it will be in flight for. Then, once we know the number of frames before it lands, we can then work out how far it will have gone horizontally. So we need to solve for when vertical distance is zero:

What we have here is a quadratic for , which we can solve with the usual quadratic formula, where:

This gives our next step, by filling in the quadratic formula:

Now, the solution where refers to when the banana is launched, so we can ignore that solution. We want the other solution, which says that the banana will land after frames. In our example above, the banana was launched with , so we can predict correctly that it lands after frames.

The advantage of doing all this maths is that our code ends up nice and simple for calculating the horizontal impact of the banana:

double vx = Player.getVelX(radianAngle, forceFactor); double vy = Player.getVelY(radianAngle, forceFactor); double impactTime = 2.0 * (vy / ProjectileWorld.GRAVITY) - 1; double impactX = player.getX() + impactTime * vx;

I’ve added this to the scenario so that you can have a play. You can see that the cross on the ground moves as you aim, and once you fire, it correctly predicts where the banana will land. Having the visible target makes our scenario a bit too easy for human players, but the calculation will be useful in future posts.

Comments on:"Take Aim" (1)Paul Firthsaid:Nice work, appreciated – helped me out with this tennis game I’m working on.

Here is a version of the above which works with a height offset for AS3:

static public function ComputeImpactTime(vz:Number, height:Number):Number

{

var dt:Number = Constants.kTimeStep;

var a:Number = (kGravity * dt) / 2.0;

var b:Number = (vz * dt) + a;

return (-b – Math.sqrt((b * b) – (4 * height * a))) / (2 * a);

}