5

Supposing we have Missile A, with a position vector and velocity magnitude (ignoring acceleration, as many games do) and Spaceship B, with position and velocity vectors. Now, this missile, being a Nasty Missile of Seeking, will try to find the best intercept for Spaceship B.

Missile A has two advantages: It knows calculus and it can calculate the roots of polynomials. However, the missile, or to abstract, the programmer, is still learning calculus and wants to know if he has the right equation. (Polynomial roots will be solved by a nice fellow called Jenkins-Traub Code Implemented From Netlib)

To wit:

  • mp = Missile Position

  • mv = Missile Velocity

  • sp = Spaceship Position

  • sv = Spaceship Velocity

  • t = Time

According to the programmer's best guess, the equation for intercept is: tspsv + tspmv - tmpsv - tmpmv

Except I'm pretty sure I'm headed down the wrong track entirely, as there should probably be some exponents in that mess; this being an attempt at solving: (sp-mp)(sv-mv)(t)

My other option is differentiating (sp-mp)(sv-mv)^2, but I wanted to get feedback first, partly because, unless I'm mistaken, '(sp-mp)' resolves to '1'. And that seems...Odd. OTOH, the rate at which that function is changing might be what I'm looking for.

So - What have I gotten wrong, where and why?

Thanks.

Potentially-useful link to first thread.

Edit:

Summing the equations:

(a+bx) + (c+ex)

(a+1bx^0) + (c+1ex^0)

(a+1) + (c+1)

Non-viable.

Product of the equations:

(a+bx)(c+ex)

ac+aex+cbx+bex^2

Not a polynomial (can't solve with Jenkins-Traub) and doesn't quite look right.

ac+1aex^0+1cbx^0+2bex^1

ac+ae+cb+2bex

And definitely not that, I think.

Community
  • 1
  • 1
Narf the Mouse
  • 1,541
  • 5
  • 18
  • 30
  • 1
    You forgot to mention what the space ship and the missile can control. Can they speed up or down? Can they change direction,... –  Sep 16 '11 at 06:06
  • My comment is to always start with a good sketch. Once you lay all your objects and variables on the sketch the solution will become more obvious. You reminded me I solved this problem (with gravity) for a game when I was learning how to program using BASIC. You best learn when you figure it out yourself. – John Alexiou Sep 16 '11 at 11:56
  • What's the unknown? What quantity are you trying to calculate? What does "equation for intercept" mean? – Jean-François Corbett Sep 16 '11 at 12:36
  • Ah, sorry. In order: That's disregarded, the best intercept at that moment is all that's needed. Well, yes, I'm trying to figure it out myself; thanks for the suggestion to sketch it out. I'm trying to derive a polynomial equation which will give the time at which, given this instant's values, the missile can intercept the spaceship. Why a polynomial equation? Well, gotta be some type of equation. And I know a polynomial will work. – Narf the Mouse Sep 16 '11 at 13:25
  • Without accelerations the equations are linear. No polynomial is needed. With gravity you will get a 2nd order poly. – John Alexiou Sep 16 '11 at 18:21
  • Ah - I was unclear there - I'm counting a linear equation (when one pops up) as a polynomial with a highest exponent of 1. – Narf the Mouse Sep 16 '11 at 21:13

3 Answers3

2

The 2D equations of motion for the missile are (assume starting at t=0)

[ mpx(t) = mpx(0) + mvx*t , mpy(t) = mpy(0) + mvy*t ]

the spaceship motion is

[ spx(t) = spx(0) + svx*t , spy(t) = spy(0) + svy*t ]

where mpx(0) mpy(0) spx(0) spy(0) are the initial position components

So to intersect you must have mpx(t)=spx(t) and mpy(t)=spy(t). Which is two equations to solve for two unknowns. One may be the time to intercept t, and the other the direction of the missile given by slope=mvy/mvx. Or it could be the initial position of the missle mpx(0) and mpy(0), or the velocity components given a target intercept time.

It is not clear from the question what you a looking for.

John Alexiou
  • 28,472
  • 11
  • 77
  • 133
0

Instantaneous Solution

Position_Ship + t*Velocity_Ship = Position_Missile + t*Velocity_Missile

If they are set to intercept then you can trivially solve for t over either dimension.

If you want to determine Velocity_Missile we need one more constraint.

N = (Position_Missile - Position_Ship) ^ Velocity_Ship (cross-product)

N dot Velocity_Missle = 0

This will give you a pair or linear simultaneous equations.

Dynamic Solution

If the Velocity_Missile is initially given and we want to apply accelleration until we get within a limiting radius, then it becomes thorny. You can get an aesthetically pleasing solution using a simple curve of persuit, or we can get numerical...

Let Velocity_Missile' be the instantaneous solutions from above, derive the corresponding t', given the power of the motor you can calculate the time t'' taken to deliver that change in velocity. Add this t''*Ship_Velocity to get an updated target position. Iterate.

spraff
  • 32,570
  • 22
  • 121
  • 229
0

If you have mp, mv and sp, then to calculate sv and t:

mp+mv(t)=sp+sv(t) and |sv|=q (maxspeed)
mp+mv(t)-sp+sv(t)=0

mpx+mvx*t-spx+svx*t=0
mpy+mvy*t-spy+svy*t=0

svx^2+svy^2=q^2
svx^2+svy^2-q^2=0

Which we then can solve:

(mpx-spx)/t+mvx=svx
(mpy-spy)/t+mvy=svy

((mpx-spx)/t+mvx)^2+((mpy-spy)/t+mvy)^2=q^2
(mpx-spx)^2/t^2+2*mvx*(mpx-spx)/t+mvx^2+(mpy-spy)^2/t^2+2*mvy*(mpy-spy)/t+mvy^2=q^2
((mpx-spx)^2+(mpy-spy)^2)/t^2+(2*mvx*(mpx-spx)+2*mvy*(mpy-spy))/t+mvx^2+mvy^2-q^2=0
((mpx-spx)^2+(mpy-spy)^2)+(2*mvx*(mpx-spx)+2*mvy*(mpy-spy))*t+(mvx^2+mvy^2-q^2)*t^2=0
((mpx-spx)^2+(mpy-spy)^2)/(mvx^2+mvy^2-q^2)+(2*mvx*(mpx-spx)+2*mvy*(mpy-spy))/(mvx^2+mvy^2-q^2)*t+t^2=0

c = (mvx^2+mvy^2-q^2)
if   a = (2*mvx*(mpx-spx)+2*mvy*(mpy-spy))/c
and  b = ((mpx-spx)^2+(mpy-spy)^2)/c
then t = -a/2+-sqrt(a^2/4-b)

a/2 = (2*mvx*(mpx-spx)+2*mvy*(mpy-spy))/2c
a/2 = (mvx*(mpx-spx)+mvy*(mpy-spy))/c
a^2/4 = (mvx^2*(mpx-spx)^2+2*mvx*(mpx-spx)*mvy*(mpy-spy)+mvy^2*(mpy-spy)^2)/c^2

b/c^2=((mpx-spx)^2+(mpy-spy)^2)*c
b/c^2=((mpx-spx)^2+(mpy-spy)^2)*(mvx^2+mvy^2-q^2)
b/c^2=mvx^2(mpx-spx)^2+mvx^2(mpy-spy)^2+mvy^2(mpx-spx)^2+mvy^2(mpy-spy)^2-q^2(mpx-spx)^2-q^2(mpy-spy)^2

t = -(mvx*(mpx-spx)+mvy*(mpy-spy))/c +- sqrt((mvx^2*(mpx-spx)^2+2*mvx*(mpx-spx)*mvy*(mpy-spy)+mvy^2*(mpy-spy)^2)-(mvx^2(mpx-spx)^2+mvx^2(mpy-spy)^2+mvy^2(mpx-spx)^2+mvy^2(mpy-spy)^2-q^2(mpx-spx)^2-q^2(mpy-spy)^2))/c

t = -(mvx*(mpx-spx)+mvy*(mpy-spy))/c +- sqrt((2*mvx*(mpx-spx)*mvy*(mpy-spy))-(mvx^2(mpy-spy)^2+mvy^2(mpx-spx)^2)+q^2((mpx-spx)^2+(mpy-spy)^2)))/c

t = -(mvx*(mpx-spx)+mvy*(mpy-spy))/c +- sqrt(-(mvx(mpy-spy)-mvy(mpx-spx))^2+q^2((mpx-spx)^2+(mpy-spy)^2)))/c

t = -(mvx*(mpx-spx)+mvy*(mpy-spy))/c +- sqrt(-(mvx(mpy-spy)-mvy(mpx-spx))^2+q^2(c+q^2))/c
t = -(mvx*(mpx-spx)+mvy*(mpy-spy))/c +- sqrt(-(mvx(mpy-spy)-mvy(mpx-spx))^2+q^2(c+q^2))/c

t = -(mvx*(mpx-spx)+mvy*(mpy-spy))/c +- sqrt(-(mvx(mpy-spy)-mvy(mpx-spx))^2+q^2(c+q^2))/(mvx^2+mvy^2-q^2)

I don't have time to simplify further, but it can be done, or just evaluate it.

Then plug t back into:

svx=(mpx-spx)/t+mvx
svy=(mpy-spy)/t+mvy

To get the s vector.

Maybe I've done a mistake somewhere...

nulvinge
  • 1,600
  • 8
  • 17