Skip to main content
GameDev.net gamedev.net
🔒 Locked

Calculating interception vector and time scalar

Started by Cybrosys Jun 30, 2007 at 9:56 AM 5 replies 2.2k views
Original Post
Cybrosys
Cybrosys
What I'm trying to do is find where to, for example, shoot in order to hit a moving target. A solution may not always exist of course. I did a search on the forum and found a solution but i didn't understand his math or what it was he was calculating having variables called: a, b, c and d. The formula that i have is pretty simple, except that there's two unknowns and i don't know how to break em out, solve the equation in other words. Formula: targetPos - projectilePos + targetDir * time = projectileDir * projectileSpeed * time The variables are vectors except for the time scalar. The length of the projectileDir is 1 in the formula if the time variable was correctly chosen. The problem is i don't want to have to increment the time variable and check the length of the projectileDir to see if it's equal to about 1, and if not continue to increment and check. What my limited math skills won't allow is for me to somehow tell the formula that the |projectileDir| has to be 1 or the projectileSpeed if we want to move it out of the equation. Please help me, the search result i mentioned above refers to this post: http://www.gamedev.net/community/forums/topic.asp?topic_id=122528
haegarr
haegarr
The position of the target dependent on the time t:
PT(t) := OT + t * vT * dT
where OT is its initial position (i.e. when t==0), vT is its velocity (notice that this is a scalar!), and dT is its moving direction (normalized to a length of 1). This formula assumes a linear straight movement of the target. It also reduces the target to a point.

Similarly,
PP(t) := OP + t * vP * dP
expresses the same for the projectile, obviously with other parameters. This formula also assumes a linear straight movement of the projectile, of course.

Now, if
PT(t) = PP(t)
is given for any t>=0, then the both positions are identical. Notice that if you set in the terms
OT + t * vT * dT = OP + t * vP * dP
and re-orders them like so
OT - OP = t * ( vP * dP - vT * dT )
you'll see a term that is missed in the formula of the OP.

Now in principle there is only 1 real unknown variable, namely t, for which the equation has to be solved. To be able to do so you have to know especially the parameters vT and dT. Perhaps vP is much greater than vT (e.g. a bullet is fired at a human), then the simplification
vP * dP - vT * dT is approx. vP * dP
can be made. Else the parameters have to be estimated by measuring the movement of the target for a small period of time (perhaps it is eplicitely known by the engine, of course; depends on how the target is controlled).
Cybrosys
Cybrosys
Thank you for the reply.

The problem is that there are 2 unknown variables, time and the DT and because of that all i could think of was to start time at almost 0 and increment it and checking how long the DP vector is. If it's less than 1 then i know we've overshot and should decrement the time with more accuracy and while it's longer than 1 continue to increment it until it's as close to 1 as we will accept.

Sometimes there won't be a solution of course, depending on where the objects are and what velocities are chosen.

Seeing as you seem to have a better grasp of math, did you read the other thread?

Maybe we could somehow calculate the time variable if we assume that there exists a solution seeing as the ratio between the distances needed to travel is the same as the ratio between the two objects' speed.
haegarr
haegarr
Quote:
Original post by Cybrosys
The problem is that there are 2 unknown variables, time and the DT and because of that all i could think of was to start time at almost 0 and increment it and checking how long the DP vector is. If it's less than 1 then i know we've overshot and should decrement the time with more accuracy and while it's longer than 1 continue to increment it until it's as close to 1 as we will accept.

Oh, now I see your point. Sorry for my misunderstanding.

Okay. This equation
OT - OP = t * ( vP * dP - vT * dT )
contains actually 4 unknown values since dP is a 3D vector. With its 3 dimensions it also provides 3 scalar equations. That is still 1 equation too less. But luckily there is another implicit equation that helps. Let's see:

Lets define a distance vector to save some writing
s := OT - OP
and
vT := vT * dT
vP := vP * dP
what means nothing more than that the direction of motion (with a length of 1) is scaled by the speed, so that a velocity vector is resulting.

Setting in leads to
s = t * ( vP - vT )
what can be decomposed into 3 scalar equations
sx = t * ( vPx - vTx )
sy = t * ( vPy - vTy )
sz = t * ( vPz - vTz )

Next we know that
|vP| = sqrt( vPx2 + vPy2 + vPz2 ) == vP
i.e. the length of the velocity vector of the projectile is just given. Using a square to hide the nasty sqrt leads to
vPx2 + vPy2 + vPz2 == vP2
what is just the 4-th equation mentioned above.

Now isolate vPx, vPy, vPz from the first 3 equations
vPx = ( sx + t * vTx ) / t
vPy = ( sy + t * vTy ) / t
vPz = ( sz + t * vTz ) / t
and setting this into the 4-th equation, and calculating the squares, so that (if I made no mistake)
( sx2 + sy2 + sz2) + 2 * t * ( sx * vTx + sy * vTy + sz * vTz ) + t2 * ( vTx2 + vTy2 + vTz2 ) = t2 * vP2
or in vector form (if you have a nice vector math lib at hand
|s|2 + 2 * t * s * vT + t2 * |vT|2 = t2 * vP2

Herein only t is unknown, and the whole formula is a quadratic equation in t. One can solve any quadratic equation of the form
f(t) := t2 + c1 * t + c0 = 0
by the so-called "pq-formula" (well, it is called so in Germany), yielding in
ta = - c1 / 2 + sqrt( c12 / 4 - c0 )
tb = - c1 / 2 - sqrt( c12 / 4 - c0 )

If the argument of the sqrt is negative then there is no (real) solution since both motions are parallel; if it is 0 then there is 1 solution, and if it is greater than 0 then it has 2 solutions. A negative t will be nonsense in your case, of course.

However, the terms a, b, c, and d in the cited thread are just intermediate results of solving the pq-fomula above. But notice that I have written down the 3-dimensional case, while the cited thread shows it for 2 dimensions.

With the (senseful) t you can compute PT(t) and from that dP (see my first post above).

Hopefully that helps you a bit. Please excuse me if I have made the one or other mistake in the math above. I will correct it if I recognize one.

[EDIT: 4 mistakes corrected by now]

[Edited by - haegarr on June 30, 2007 1:18:37 PM]
Cybrosys
Cybrosys
Thank you, you've been very helpful :) (Rate++)

I'm reading through your post and checking the math, the only thing that caught my eye so far was that you stated that:

VPx = (Sx + t * VTx) / t

when a simpler one is:
VPx = VTx + Sx / t

Was there a reason for doing so?
haegarr
haegarr
Quote:
Original post by Cybrosys
I'm reading through your post and checking the math, the only thing that caught my eye so far was that you stated that:

VPx = (Sx + t * VTx) / t

when a simpler one is:
VPx = VTx + Sx / t

Was there a reason for doing so?

Not really. I've just solved the brace, added the one term and divided by t, while you've gone a step furthur. Later when eliminating the division after setting into the 4-th equation both ways yield in the same, so its just a matter of taste and how many steps one skips during explanation.
Cybrosys
Cybrosys
Well, thank you again. I was going insane over the problem :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.