← Back to index

Hitting a Moving Target in Your Game: A Simple Guide

Want to make your game more exciting by letting players shoot at moving targets? Here's how to calculate the right direction to fire a projectile so it hits the target, assuming the target moves in a straight line at a constant speed.

The Basics

Imagine a target moving steadily and you wanting to fire a projectile to hit it. We need to figure out the projectile's velocity (speed and direction) to ensure a hit.

What We Know

The Goal

Our mission is to find the projectile's velocity components (vbx, vby, vbz) so that it hits the target.

The Math (Simplified)

We use a system of equations to figure this out. Essentially, we are ensuring:

  1. Alignment: At some point in time ('t'), the projectile and target will line up horizontally (same x-coordinate).
  2. Collision: At that same time 't', they will also occupy the same point in space (same y and z coordinates).
  3. Speed: The projectile will travel at its given speed 'sb'.

The Equations (Don't worry, the computer does the heavy lifting)

To find the right velocity, we use these equations:

(1) ya + vay * t = yb + vby * t (Matching y-coordinates) (2) za + vaz * t = zb + vbz * t (Matching z-coordinates) (3) vbx² + vby² + vbz² = sb² (Ensuring the correct speed)

There's also a formula to calculate 't', the time of impact:

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

Solving the Equations

Solving these equations gives us two possible solutions for the projectile's velocity components (vbx, vby, vbz). This is because there are usually two paths the projectile can take to hit the target.

The Complex Solution (You Don't Have to Calculate This By Hand)

The actual solution to these equations is a bit complicated and looks like this:

{ vbx = ..., vby = ..., vbz = ... },  { vbx = ..., vby = ..., vbz = ... } 

(Where "..." represents the lengthy expressions derived from solving the equations)

The computer will handle these complex calculations for you. You just need to know what the solution means.

What This Means

Each set of (vbx, vby, vbz) values represents a possible velocity for your projectile. One will likely result in a more direct hit, while the other might be a more curved trajectory.

Putting It Into Action

  1. Gather Data: Get the target's position and velocity, as well as the projectile's position and speed in your game.
  2. Calculate: Plug these values into the solution equations (or use a game engine function that does this for you).
  3. Choose a Solution: You'll get two solutions. Pick the one that makes the most sense for your game (e.g., the one with the shortest travel time).
  4. Apply Velocity: Set your projectile's velocity to the chosen (vbx, vby, vbz) values.

Important Notes

That's it! By using these principles, you can create a realistic projectile motion system for your game and make hitting moving targets a fun challenge.