← Back to index

Hitting a Moving Target: The Math of Projectile Motion

Let's say you're building a computer game where you need to program a projectile to hit a moving target. Here's how you can figure out the right speed and direction to fire the projectile.

The Setup

Imagine we have two things in our 3D game world:

  1. Target 'a': This is the object we want to hit. It's moving at a constant speed in a straight line.
  2. Projectile 'b': This is what we're going to fire at the target. We know how fast it can travel (its speed), but we need to figure out which direction to shoot it.

We know the following information:

The Goal

Our goal is to find the velocity of the projectile 'b', specifically its velocity components (vbx, vby, vbz), so that it hits the target 'a'.

The Equations

To achieve this, we can use a set of three equations:

(1) ya + vay * t = yb + vby * t

(2) za + vaz * t = zb + vbz * t

(3) vbx² + vby² + vbz² = sb²

Let's break down what these equations mean:

t = (xb - xa) / (vax - vbx)

This represents the time it takes for the x-coordinates of 'a' and 'b' to become the same. Basically, it's the moment when the projectile and target are aligned along the x-axis.

Putting it Together

We have three equations and three unknowns (vbx, vby, vbz). This means we can solve this system of equations to find the velocity components of the projectile.

What Solving the Equations Means

Finding the values of vbx, vby, and vbz that satisfy these equations means finding a projectile velocity such that:

  1. Alignment in Time: At some point in the future ('t'), the projectile will be horizontally aligned with the target (same x-coordinate).
  2. Collision: At that same time 't', the projectile and target will also have the same y and z coordinates, meaning they will occupy the same point in space – a collision!
  3. Speed Consistency: The projectile's velocity will match its known speed.

Using the Solution

Once you solve these equations (which can involve some algebra), you'll get the velocity components (vbx, vby, vbz) for the projectile. You can then use these values in your game to set the projectile's direction and speed, ensuring it hits the moving target.

Important Notes

the output equation

That large, complex solution you provided is the result of symbolically solving the three equations for vbx, vby, and vbz. Let's break down how it relates to the original problem and equations:

1. Two Solutions for Each Component: Notice the solution has two sets of values, each enclosed in large parentheses {}. This is because the equations involve squares (like vbx²), which typically lead to two possible solutions (positive and negative roots of a quadratic equation). This means there are generally two possible trajectories for the projectile to hit the target. One trajectory corresponds to a more direct intercept, and the other often represents a more curved path.

2. The Components (vbx, vby, vbz): Each set of solutions contains expressions for vbx, vby, and vbz. These expressions are complicated because they account for all the possible positions and velocities of 'a' and 'b' in 3D space.

3. Relating the Solution to the Equations:

4. The Set Notation ( \setminus ): The \setminus {vαx} and \setminus {vαy} notation means that if the calculated solution for vbx equals vax or vby equals vay, respectively, that particular solution is discarded. This is necessary to avoid division by zero in the calculation of the time variable 't'. This is the special case I mentioned before, where if any velocity components are equal between projectile and target, it requires separate consideration.

Simplified Explanation of the Meaning:

The complex solution set is simply a way to say: "Given the target's position, velocity, and the projectile's speed and starting point, these are the two possible sets of velocity components (vbx, vby, vbz) you can give the projectile to guarantee a hit." Each set of velocity components represents a specific firing direction and maintains the projectile's speed to exactly sb.

Practical Application in Your Game:

  1. Input Values: Plug in your game's current values for the initial positions and target velocity into the solutions.

  2. Calculate Two Solutions: Evaluate the two solution sets given by the equations.

  3. Select a Solution: Choose the solution that is most relevant to your game. For example, if you want the most direct hit, choose the solution that gives the smallest time t (calculated using the formula for t).

  4. Set Projectile Velocity: Apply the chosen (vbx, vby, vbz) velocity to your projectile in the game.

By doing this, you will have successfully calculated the projectile launch parameters for a guaranteed hit (assuming constant target velocity).