@desprez, on Apr 16 2006, 12:56 AM, said in Prediction Algorithm:
Hey, that makes perfect sence. The angles form themselves such that the bearing of the target doesn't change from the shot. Sweet.
Well, my algebra is rusty, and I'm having trouble re-ordering that equation so that I can find theta.
Wait a minute. Where is the time of 1 second coming from? How can we assume that's going to be the time of flight of the shot?
It's arbitrary. It can be one frame just as well as 15 billion years. It won't change anything, it's just so that I can turn velocities into distances and hence represent them. It has nothing to do with the time necessary for the shot to hit.
As for finding theta, here's how. With such equations with sin and cos of the same angle and linear operations on them, this is a general method.
First, move everything that's constant to the other side. Then we have:
yVscos(theta)-xVssin(theta)=yVx-xVy
Then, factor the first member with the square root of the sum of the squares of the factors, so that the remaining stuff has a sum of squares equal to one, so that they can be the sinus and cosinus of some angle. Notice that here Vs is present in both squares, so Vs^2 can be factored and Vs can be put outside of the square root, and let's call this square root d, as in distance (because it happens that it's what it is):
Vs*d * ( (y/d)cos(theta) - (x/d)sin(theta) ) = yVx-xVy
Then (y/d) and (x/d) are respectively the sinus and cosinus of some angle, usually computed by a function Atan2(x,y) that exists in most libraries (C, Basic, script) and calculators, which is Atan(x/y), to which you add Pi if y is negative (if y is 0, then the value is Pi/2 if x is positive and 3*Pi/2 if x is negative, we'll take our angles going in (0,2Pi)). At any rate, let's call this angle alpha. We now have:
sin(alpha)cos(theta) - cos(alpha)sin(theta) = (yVx-xVy)/(Vs*d)
I hope you know your trigonomery, because the above sincos cossin is in fact equal to sin of the difference:
sin(alpha-theta) = (yVx-xVy)/(Vs*d)
yVx-xVy is equal to Vdsin(beta), beta being the angle from the velocity vector (of the target) to the distance vector (from firing platform to target). This would be a little long to explain, so believe me, this is just to say that it is always less than V*d and to make formulas a little simpler.
sin(alpha-theta) = (V/Vs)*sin(beta)
You can see instantly that if the second term is above 1 or below -1, then there is no solution, and this can happen only when V>Vs, i.e. when the target is faster than the shots (but it doesn't always happen in this case, tough of course the faster the target is compared to the shots, the less betas will be favorable). So if (V/Vs)*sin(beta) is between -1 and 1, the two solutions are:
alpha-theta=Asin( (V/Vs)*sin(beta) ) and alpha-theta=Pi-Asin( (V/Vs)*sin(beta) )
In fact, there is an infinity of solutions but we restrain over a range of length 2*Pi, though it is here (-Pi/2,3Pi/2), you might wish to add 2Pi to the offending values between -Pi/2 and 0 to put them back in the (0,2Pi) range but you'll have to do it again anyway for theta. So there are two solutions for theta:
theta=alpha-Asin( (V/Vs)*sin(beta) ) mod 2Pi and theta=alpha-Pi+Asin( (V/Vs)*sin(beta) ) mod 2Pi
(mod 2Pi means give or take as many 2Pis as necessary to be in the range (0,2Pi() Notice these two angles are symmetrical over the perpendicular of the direction of the parallel (of angle alpha+Pi/2), with an angle of Pi/2+Asin(...) either side of it.
But, of course, as I said some or all of these solutions may end up going away from the target. This is not as obvious to tell when you're in these computations (compared to when you're finding the solution graphically as I described), your best bet would be to check the scalar product of the shot velocity vector relatively to the target, with the firing platform->target vector, if it is positive then it's going the right direction, if it is negative, then it is going the wrong direction. If it is negative for both solutions then you have no possibility, it happens again only when the target is faster than the shot but it is a tad harder to prove algebraically. Of course in both out-of-range cases your best bet would be to fire directly towards the target in case it would change its destination.
That's all for today. Tomorrow class we're going to solve the problem for a toroidal Maelstrom space, with the two possibilities: we have long shots, or we don't. Bonus points for those able to shoot the comet.