Re: Space physics for missiles, spaceships
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 开发者_JAVA技巧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.
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.
精彩评论