Auto Targeting Mathematics For Hitting Moving Targets

Back

Let us start off with movement on a single axis.

P(t) = Position at time t

Let us say P(t=0) = P0 (I wish I could put a subscript in the text...much easier written down)
And that P(t) = P0 + P'(t)*t

Technically I've ignored the acceleration term - but that is necessary.

And, also I'm assuming that P'(t) is a constant (hence velocity is a constant)

so...for example typically in a computer game you will represent the change in a position of an object like this:

x = "current x" + "speed in x" ... each frame.

This is the same as "x at frame t" = "initial x" + "speed at frame t" * "frame t"

Ok...

all good so far?


Let us say:

X(target space ship at time t) = X(target at time 0) + V(target) * time
X(our space ship at time t) = X(our ship at time 0) + V(our ship) * time
X(our bullet at time t) = X(our ship at time 0) + V(bullet) * time

V(bullet) = (Muzzle Velocity of bullet) + V(our space ship)


Now, we want to solve an equation where the X(bullet) = X(target) at a particular time.

Rearranging the above to find the muzzle velocity we get this:

X(target 0) + V(target) * t = X(bullet 0) + (muzzle vel + v(our ship))*t

(X(target) - X(bullet) + (V(target)-V(our ship)) * t )/t

(Muzzle Velocity) = ((X(target at time 0) - X(our bullet at time 0)) + (V(target) - V(our ship))*t)/t



Okay - so this tells us the muzzle velocity of the bullet as a function of 't' that will hit the target.


However, we have a particular constraint. Typically bullets have a fixed speed. IE - we want our bullets to travel
consistently - it is no good having a bullet travel really quickly when shot once, and really slowly when shot another time.

Therefore we have to include a constraint that the muzzle velocity must equal a fixed value.

That means that we now have only a certain set of values for "t" which satisifies this equation.



Okay - if we were only concerned with the 1 dimensional case like I've described it is very easy. You simply solve for
't' for a set value of Muzzle velocity.

However, we have 2 or 3 (depending on 2d or 3d) equations to consider.


In the general case for 3 dimensions we have the following set of equations:

Muzzle Velocity Vector component in X axis = ((X(target at time 0) - X(our bullet at time 0)) + (V(target) - V(our ship))*t)/t
(as it was above)

But also y and z
Muzzle Velocity Vector component in Y axis = ((Y(target at time 0) - Y(our bullet at time 0)) + (V(target) - V(our ship))*t)/t
Muzzle Velocity Vector component in Z axis = ((Z(target at time 0) - Z(our bullet at time 0)) + (V(target) - V(our ship))*t)/t

Note that in these cases "V" of our target and ship is the velocity in that particular vector component (x,y,z)

So...there may exist some value of "t" which when plugged into each of these equations gives the 3 components for muzzle velocity (in x,y and z) such that the length of the muzzle velocity vector equals our desired bullet speed (ie our constraint).


So this is where the interesting bit comes in...


To make things simpler to read let us create some variables:

Dx = X(target at time 0) - X(bullet at time 0)
Dy = Y(target at time 0) - Y(bullet at time 0)
Dz = Z(target at time 0) - Z(bullet at time 0)

and

VDx = V(target) - V(our ship) (x component of velocities)
VDY = V(target) - V(our ship) (y component of velocities)
VDZ = V(target) - V(our ship) (z component of velocities)

and

VMx = muzzle velocity in X = (Dx + VDx*t)/t
VMy = muzzle velocity in Y = (Dy + VDy*t)/t
VMz = muzzle velocity in Z = (Dz + VDz*t)/t

and let us define our constraint, the length of the muzzle velocity vector, as L

now, L*L = VMx * VMx + VMy * VMy + VMz * VMz (think back to pythagoras...a*a + b*b = c*c...pretty fundamental to vector mathematics)


So, if we substitute our equations for VMx etc into the expression for L*L we get this:

L*L = ((Dx + VDx*t)/t)*((Dx + VDx*t)/t) + ((Dy + VDy*t)/t)*((Dy + VDy*t)/t) + ((Dz + VDz*t)/t)*((Dz + VDz*t)/t)

Now that's a pretty horrid looking equation....but it simplifies down really nicely when we try to solve for t.

Remember that L, and all the Dx, Dy, Dz, VDx, VDy, VDz are constants..

After a bit of manipulation we end up with an equation that can be expressed like this:

(simplified...)

a*(t*t) + b(t) + c =0 ...the good old quadratic equation...which you may have learned in about year 8 or 9 at high school?

(a,b,c are various combinations of the terms above)

If you remember, there exists a formula, called the quadratic formula which looks like this;

t = (-b +- sqrt(b*b - 4*a*c))/(2*a) (look it up on google image...I'm sure it will bring back memories of classrooms!)


So...what this looks like with all the Dx, VDx stuff etc is this:


BB# = (DxVDx + DyVDy + DzVDz)*(DxVDx + DyVDy + DzVDz)
AC# = (LL - VDxVDx - VDyVDy - VDzVDz)*(DxDx + DyDy + DzDz)

T1# = ((DxVDx + DyVDy + DzVDz) + Sqr(BB+AC))/(LL-(VDxVDx+VDyVDy+VDzVDz))
T2# = ((DxVDx + DyVDy + DzVDz) - Sqr(BB+AC))/(LL-(VDxVDx+VDyVDy+VDzVDz))

Now...some considerations...

First - you will note that the denominator has the potential to equal zero. In a physical situation this would equate to the bullet muzzle velocity equalling the relative velocity difference between the target and the firer. Think on that...if the bullet is travelling at the same speed in the same direction as the target - will it ever reach it? No...so we ignore this case and say a hit is impossible.

Second - the Sqr may have a negative term in it. This basically means there is no solution. This is not impossible - it can happen. Think for a minute - a simple example would be a target that is moving much faster than the bullet in a direction away from the firer - it can never be hit. So it makes sense there will be some cases where the bullet cannot hit the target.

So we ignore these cases as well and return a 'false' value indicating we can't hit the target.

Now.. a square root has two solutions. Both a positive and a negative. This means there can be up to two possible times that we will hit the target. In this case we test the two "T" values. If either of them is negative, we use the other. If both of them are positive we return the smaller of the two.


So at this point we have a time "T" which satisifies our constraint of muzzle velocity. We then plug this back into our formulas above to get the muzzle velocity in each component (x,y,z) and then supply these values to our gun and 'FIRE ZEE LASER'....(in my best evil genuis voice)



So hopefully that makes a bit of sense....