1

In this rather useful CodeProject article, an enterprising and very helpful person has done the math needed for a newtonian missile to hit a newtonian target (it also works for matching course and speed between spaceships, with a bit of fiddling of inputs).

One of the things I've written with this in the past is a battle between up to hundreds of spaceships (blocks), firing missiles (blocks) at each other. Quite neat.

However, it only works for purely newtonian craft and, as anyone who's paid attention to flight in most space simulators can tell you (or who likes speculative FTL methods), newtonian isn't the only way to fly.

And it seems to me that, with all this wonderful computer hardware, there should be a computer program that can take, say, p+v*t+0.5*a*t*t = P+V*t+0.5*A*t*t and spit out equations that will give you t and A (or a, depending on whether the pursuer is on the left or the right).

Algebrator comes closest that I've found (MATLAB might be able to ace it, but I do Not have a $2,100 budget), but chokes if I substitute in 1-collumn, 2-row vector "matrices". (I have 4.2, not 5)

So - Help me wreak carnage among the stars? I'm not an evil alien overlord out to defeat the MiB, promise! :D

Edit: I'm not looking for solution equations; I'm looking for software that can give me those solution equations.

Thanks.

Narf the Mouse
  • 1,541
  • 5
  • 18
  • 30
  • Since the initial conditions are subject to change (your target might change his acceleration vector) it seems restrictive to look for a solution to the unchanging-acceleration equation. If I were a missile, here's what I'd do: 1. compute the accelerate-to-halfway-then-decelerate-to-intercept time, T, to hit the target based on its current position; 2. compute a rolling average for the target's velocity, V; 3. compute the target's probable position P after time T assuming it keeps velocity V; 4. accelerate towards an intercept with P; 5. schedule a return to step 1 after a short time interval. – Russell Borogove Sep 02 '11 at 02:29
  • The problem is under-determined: there are many possible pairs (A,t) that give you a collision at time t. How exactly do you want the missiles to behave? – user168715 Sep 02 '11 at 03:25
  • In order: That's addressed in the comments section; as long as you continuously update the intercept every frame, it is impossible for a missile with a higher accel to miss. I've tested this by flying a "ship" away from a continuously-intercepting missile; no attempt at dodging ever worked - And, in fact, only shortened the time to intercept. Second; I'm not looking for an equation per se; I'm looking for software that can get me those equations. I'll clarify that. (Hmm, is this the best group?) – Narf the Mouse Sep 02 '11 at 04:00

1 Answers1

1

I'm still not completely sure what you are trying to do. If you want to solve an algebraic equation at code-writing time, Wolfram Alpha is quite useful, e.g. http://www.wolframalpha.com/input/?i=Solve%5Bq0+%2B+v0+t+%2B+a0%2F2+t%5E2+%3D%3D+q1+%2B+v1+t+%2B+a1%2F2+t%5E2%2C%7Ba1%2Ct%7D%5D.

If you want to solve an algebraic equation at runtime, that is a very hard problem in general. If you give me more details about exactly what you are trying to do, I might be able to recommend some good free packages.

EDIT: Example problem you might be trying to solve:

Q: Given a spaceship with initial position q0, initial velocity v0, and constant acceleration a0, and a missile with initial position q1, I want to find the missile velocity v1 with magnitude M that will cause the missile to eventually collide with the spaceship.

A: You are trying to solve the system of equations

q0 + v0 t + 1/2 a0 t^2 = q1 + v1 t
v1 . v1 = M^2

for the vector v1, where the time of impact t is also unknown. This system is very difficult to solve in closed form, as far as I can tell: Wolfram Alpha chokes on it, and even Mathematica has a hard time. It is, however, relatively simply to attack it with numerical methods. To do so we first solve for t by plugging the first equation into the second:

(q0 - q1 + v0 t + 1/2 a0 t^2) . (q0 - q1 + v0 t + 1/2 a0 t^2) == M^2 t^2

This is a quartic polynomial in t with known coefficients:

[(q0 - q1).(q0-q1)] + [2 (q0 - q1).v0] t + [v0.v0 + (q0-q1).a0 - M^2] t^2 + [v0.a0] t^3 + [1/4 a0.a0] t^4 = 0

Everything in brackets is a scalar you can compute from known quantities. To find the roots of this quartic, use a black-box root solver (I highly recommend Jenkins-Traub: C++ code available at www.crbond.com/download/misc/rpoly.cpp, Java and Fortran versions are also floating around the 'net).

Once you have the roots, choose the smallest positive one (this one will correpond to the direction that makes the missile hit the spaceship as early as possible) and plug it into the first equation, and trivially solve for v1.

EDIT2:

Q: Given a spaceship with initial position q0, initial velocity v0, and constant acceleration a0, and a missile with initial position q1 and initial velocity v1, I want to find the missile acceleration a1 with magnitude M that will cause the missile to eventually collide with the spaceship.

A: This problem is very similar to the first; your equations now is

q0 + v0 t + 1/2 a0 t^2 = q1 + v1 t + 1/2 a1 t^2
a1 . a1 = M^2

Where a1 and t are unknown. Again, these equations can be combined to get a quartic in t with known coefficients:

[(q0 - q1).(q0-q1)] + [2 (q0 - q1).(v0-v1)] t + [(v0-v1).(v0-v1) + (q0-q1).a0] t^2 + [(v0-v1).a0] t^3 + [1/4 a0.a0 - 1/4 M^2] t^4 = 0

Again, use Jenkins-Traub to find the roots, then plug in the smallest positive root into the first equation, and solve for a1.

user168715
  • 5,469
  • 1
  • 31
  • 42
  • The first; solve algebraic equations at code-writing time. That Wolfram Alpha is very close - Just need to figure out how/if it can do vectors. – Narf the Mouse Sep 02 '11 at 16:29
  • I'm not at all sure how to explain further, but something that can solve that equation you linked to, only in 2D or 3D, returning both a direction for the pursuer to accelerate in and a time at which the pursuer will intercept. Fiddling with Wolfram Alpha, this is what it spat out: http://www.wolframalpha.com/input/?i=Solve[{x0%2C+y0}+%2B+{x1%2C+y1}+t+%2B+{x2%2Cy2}%2F2+t^2+%3D%3D+{-x3%2C+-y3}+%2B+{-x4%2C+-y4}+t+%2B+{-x5%2C+-y5}%2F2+t^2%2C{x2%2C+y2%2C+t}]" - Turning it into a link didn't work; I guess try copy-paste? – Narf the Mouse Sep 02 '11 at 16:40
  • There's no need for you to work with vectors: you can work with each component separately instead. E.g. `(x,y,z) + (vx,vy,vz) t + 1/2 (ax,ay,az) t^2` is the same as `(x + vx t + 1/2 ax t^2, y + vy t + 1/2 ay t^2, z + vz t + 1/2 az t^2)` – user168715 Sep 02 '11 at 18:06
  • Except that one of the results (the direction to move in) is a vector derived from running a scalar (the pursuer's magnitude of acceleration) through a set of equations. – Narf the Mouse Sep 02 '11 at 20:46
  • Again, could you please post the *exact* equation you are trying to solve? I can't help you otherwise -- for the simple equation you posted above (`p+v*t+0.5*a*t*t = P+V*t+0.5*A*t*t`) there is no reason not to work componentwise. – user168715 Sep 02 '11 at 20:59
  • p[X,Y,Z] + v[X,Y,Z]*t + 0.5*a*t*t = P[X,Y,Z] + V[X,Y,Z]*t + 0.5*A[X,Y,Z]*t*t. "a" is a scalar value representing the magnitude of the pursuer's acceleration. "t" is the time until intercept. The final equation needs to give a vector for the pursuer to pursue in and the time at which interception will occur (assuming straight-line movement). That's the first equation I'm looking for software to solve. – Narf the Mouse Sep 02 '11 at 21:21
  • This equation doesn't make sense... you are adding the scalar `0.5 a t^2` to the vector `{px,py,pz} + {vx,vy,vz} t`. Can you describe in words what you are trying to solve for, and what is known? See example in my edited post. – user168715 Sep 02 '11 at 21:43
  • Ok. You have a missile. That missile has a velocity, a position and a magnitude of acceleration (it hasn't decided in which direction to accelerate in yet, because it doesn't know where the target will be. But it knows how fast it can accelerate). You have a target spaceship. That spaceship has acceleration, velocity and position (the missile knows all three, because it can see the spaceship). Where does the missile point its nose and accelerate, and for how long does it accelerate, to hit the target? – Narf the Mouse Sep 02 '11 at 22:04
  • Thanks very much. I've ordered an introductory book on calculus, so hopefully sometime in the near future (expressed in months), I'll be able to derive such equations myself. Strange that even automated software has major problems, though - Although it may not seem strange once I know the problem set. – Narf the Mouse Sep 03 '11 at 00:33