I've been working on this for an hour or two now and my maths really isn't my strong suit which is definitely not a good thing for a game programmer but that shouldn't stop me enjoying a hobby surely?

After a few failed attempts I was hoping someone else out there could help so here's the situation.

I'm trying to implement a bit of faked intelligence when the A.I fires it's missiles at a target in a 2D game world. By predicting the likely position the target will be in given it's current velocity and the time it will take the missile to reach it's target. I created an image to demonstrate my thinking: http://i.imgur.com/SFmU3.png which also contains the logic I use for accelerating the missile after launch.

The ship that fires the missile can fire within a total of 40 degree angle, 20 either side of itself, but this could likely become variable.

My current attempt was to break the space between the two lines into segments which match the targets width. Then calculate the time it would take the missile to get to that location using the formula. So for each iteration of this we total up the values and that tells us the distance travelled, ad it would then just need compared to distance to the segment.

startVelocity * ((startVelocity * acceleration)^(currentframe-1)

So for example. If we start at a velocity of 1f/frame with an acceleration of 0.1f the formula, at frame 4, would be

1 * (1.1^3) = 1.331

But I quickly realized I was getting lost when trying to put this into practice.

Does this seem like a correct starting point or am I going completely the wrong way about it?

Any pointers would help me greatly. Maths really isn't my strong suit so I get easily lost in these matters and don't even really know a good phrase to search for with this.

So I guess in summary my question is more about the correct way to approach this problem and any additional code samples on top of that would be great but I'm not averse to working out the complete code from helpful pointers.


I'll start off by saying: you're not going to be the very best in game programming if you don't have a strong grasp of mathematics. Even so, math should not prevent you from becoming a good game programmer, as there are many ways of avoiding math, such as using a specialized library or just getting a friend to help you program calculations.

Now, as for the real question:

First, given the starting position, velocity, and acceleration of the target, it is possible to find the distance from the ship to the target as a function of time.

Second, you also know the starting position, velocity, and acceleration of the missile, so you can also find the distance from the ship to it's missile as a function of time.

Now that you have a function representing the distance from ship to target and from ship to missile, all you have to do is find out when these two distances are equal. This will give you the time at which the missile hits the target.

Once you have this time, calculate where the target will be at this time and target that location.


Given the very complex nature of this system, and the fact that you really only need an approximation, you do not actually need to solve this complex equation. Instead, you can guess the time like this:

time = targetDistance / missileVelocity

And then

targetPosition += targetVelocity * time

Where targetPosition and targetVelocity are vectors in your choice of dimensions.

(obviously these variables are local only, you shouldn't be modifying the actual position of the target here)

Once you have done this, the targetPosition should be somewhat further in from of where it started. Now calculate the time it would take for the missile to hit this new point, and calculate where the ship will be at this new time (starting from the original position). The second time you do this, the change in position should be much smaller than the first. Simply repeat this process until the change in distance is below a certain threshold, or until you have looped through it enough times. The final result should be very close to the "perfect" spot, and it's much more efficient than doing all the math above.

Basically, the idea here is that you can avoid all of the really complicated math by performing a short series of educated guesses, and the result is almost the same.

  • \$\begingroup\$ Note that I also suggested this iterative method in the duplicate question I linked. So that's two people suggesting this approach. \$\endgroup\$
    – jhocking
    Jun 24 '12 at 3:33
  • \$\begingroup\$ I think you need to take the target's direction into consideration. A target moving away from the firing ship will take more time to hit, thus the point of impact changes \$\endgroup\$
    – Thomas
    Jun 25 '12 at 9:03
  • \$\begingroup\$ @Thomas do you mean the direction in which the target is moving, or the direction from the ship to the target? Either way, my method should account for both directions, and I don't believe I've made any typos. \$\endgroup\$
    – Apples
    Jun 25 '12 at 12:37
  • \$\begingroup\$ I meant the direction in which the target is moving. If the targetVelocity in your example is considered to be a Vector3D your example is correct. \$\endgroup\$
    – Thomas
    Jun 26 '12 at 8:23
  • \$\begingroup\$ @Thomas Yes, I should have clarified (and I will edit my answer to be more clear), targetPosition and targetVelocity are vectors. I even had myself confused for a second there, heh. \$\endgroup\$
    – Apples
    Jun 27 '12 at 2:12

The problem here is that you have an accelerating missile--the simple math doesn't work.

A simplistic answer is that the position of the missile is 1/2 * acceleration * time * time. (This is evident if you understand calculus, it's hard to follow otherwise.) However, this won't produce the right answer because in reality your missile is accelerating in a jerky fashion (an update each frame), not smoothly.

It may be good enough, if it's not I would simply precalculate the missile's performance and refer to the stored results when you need them.

  • \$\begingroup\$ By using iterative integration methods, objects can move with almost exact accuracy. Additionally, assuming the missile travels in a straight path, it does not matter how "jerky" the acceleration is, because it's not changing direction. \$\endgroup\$
    – Apples
    Jun 25 '12 at 1:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.