I am programming a space game, and I put this on the physics stack exchange because I need some help in the physics behind it. Let's say there is a moving enemy ship - referred to as "target", and we want to lead our ship's torpedo to a point where the bullet will intercept the moving target. Before we go on further, let's assume some constraints:
- The motion is constant, there is no acceleration or net forces being applied.
- We will not account for the angular velocity of our ship to meet the intersection point.
- The problem is in two dimensions. X and Y plane. No Z component.
- The origin is relative to the top left at (0,0). For this case however, I want to make all vectors relative to our ship.
- We will assume the ship (torpedo) has no initial velocity right now.
The motion is constant in terms of the space game server I am running, I will factor gravitation later and angular velocity for the best result. For now, let's break down the variables we know:
- Velocity of the moving target (including direction) as a vector
- Displacement of the target to the ship as a vector.
- Initial position of the ship as a point.
Ship (Torpedo to be specific) :
- Speed of the bullet (constant) (magnitude of it's velocity)
- Initial position of the bullet (in this case, the same as the position of the ship)
Now that I have covered that, let's state what we need to figure out:
- I wan't to find the point of intersection so that if I shoot a torpedo in that direction, it will perfectly intercept the target.
- I need to find the final position of the ship as a function of time. Then I can make a displacement vector from the final position of the ship to the initial position of the ship.
We have several vectors here - U, V and C. Our origin is our ship (the torpedo).
- Vector U is the displacement vector from our ship to the target's initial position
- Vector V is the displacement vector from the target's initial position to the target's final position as a function of time (need to solve for that)
- Vector C is the resultant vector of U and V. Vector C will tell me the point of intersection
My program already has a Vector class, so there shouldn't be any worry in implementation. This is in Java btw. (Again, I put this in a physics forum to talk about the physics of what's going on).
In my program, I can easily calculate the distance between the ship and the target's initial position using a head minus tail rule. In this example, Vector U is <7,3>.
Vector V on the other hand is the troubling part. Vector V is a vector from it's predicted final position to it's initial position. The question is : how do we find this?
I have modeled the predicted final position with an equation. (Pt)f stands for final position. (Pt)i stands for initial position. Note t is the subscript for target. i means initial. Vt = initial target velocity. T means time.
(Pt)f = (Pt)i + Vt * T.
(Pt)f is a vector, (Pt)i is a vector, Vt is a vector and time is a scalar. If you think about it, I'm doing a vector addition of two position values (You can think of Vt*time as a position vector as a result of velocity scaled by time).
(Pt)f is going to be the vector sum of (Pt)i + (Vt * T).
I am concerned with finding T though. Here's what I know:
- The bullet needs to be at the same point. We can model the bullet by saying (Pb)f = Vb*T.
- If they need to be at the same point, that means that (Pb)f = (Pt)f.
- That means that Vb*T = (Pt)i + (Vt * T). We can solve for T.
- I did the algebra and concluded that T = (Pt)i /((Vb) - (Vt))
The problem is, this is Time, a scalar quantity. How do I find that? I can't just divide two vectors. Vb-Vt is just vector subtraction. How do I divide that from the vector (Pt)i?
Additionally, is this the correct approach? In the end, I can create vector V in my diagram by finding a vector between (Pt)f and (Pt)i. Then, once I have that, I can find the point of intersection by adding vector U and V. Then I have to convert that point to world space (remember origin is top left at (0,0)). I have a function that will rotate my ship towards that point and shoot in that point's direction.
Let me know if I have to be more clear, and thanks for the help!