0
$\begingroup$

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:

Target :

  • 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.

Here are the steps I am taking: Vector diagram

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!

$\endgroup$
1
$\begingroup$

You had the right approach up until you tried to divide by a vector to solve for $t$. So, starting from equating the final position of the target and the bullet: $$\vec{v}_b t = \vec{p}_t + \vec{v}_{t} t.$$ Where $\vec{v}_b$ is the velocity of the bullet, $\vec{p}_t$ is the current position of the target, $\vec{v}_{t}$ is the velocity of the target, and $t$ is time.

Ultimately, we want $\vec{v}_b$. We know the speed of the bullet, but not the direction. So, we can dot both sides of the equation with themselves to get the equation in non-vector form in terms of quantities we know. $$(\vec{v}_b t \cdot\vec{v}_b t) = (\vec{p}_t + \vec{v}_{t} t)\cdot(\vec{p}_t + \vec{v}_{t} t).$$ Expanding, we get $$|\vec{v}_b|^2 t^2 = |\vec{p}_t|^2 + 2(\vec{p}_t\cdot\vec{v}_{t}) t + |\vec{v}_t|^2t^2.$$ $$0 = |\vec{p}_t|^2 + 2(\vec{p}_t\cdot\vec{v}_{t}) t + (|\vec{v}_t|^2 - |\vec{v}_b|^2)t^2.$$ As a check on the correctness of this equation, set the speed of the target to zero, and you'll see that the time is the distance to the target divided by the speed of the bullet, as it should be. Now we have a quadratic equation in $t$ in terms of non-vector quantities we know. Solving for $t$: $$t = \frac{-2(\vec{p}_t\cdot\vec{v}_{t})\pm\sqrt{4(\vec{p}_t\cdot\vec{v}_{t})^2-4 (|\vec{v}_t|^2 - |\vec{v}_b|^2)|\vec{p}_t|^2}}{2(|\vec{v}_t|^2 - |\vec{v}_b|^2)}$$ $$t = \frac{-(\vec{p}_t\cdot\vec{v}_{t})\pm\sqrt{(\vec{p}_t\cdot\vec{v}_{t})^2-(|\vec{v}_t|^2 - |\vec{v}_b|^2)|\vec{p}_t|^2}}{|\vec{v}_t|^2 - |\vec{v}_b|^2}$$ Since we want the bullet to hit in the future ($t$ > 0) and the bullet is presumably faster than the target, we can choose the appropriate sign for $\pm$. $$t = \frac{-(\vec{p}_t\cdot\vec{v}_{t})-\sqrt{(\vec{p}_t\cdot\vec{v}_{t})^2-(|\vec{v}_t|^2 - |\vec{v}_b|^2)|\vec{p}_t|^2}}{|\vec{v}_t|^2 - |\vec{v}_b|^2}$$ And finally, $$t = \frac{(\vec{p}_t\cdot\vec{v}_{t})+\sqrt{(\vec{p}_t\cdot\vec{v}_{t})^2+(|\vec{v}_b|^2 - |\vec{v}_t|^2)|\vec{p}_t|^2}}{|\vec{v}_b|^2 - |\vec{v}_t|^2}$$

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ What benefit will the bullets velocity get me in your last equation? I had a function in my code that allows me to rotate towards a point. I can find the intersection point by finding the final positon of the target with the new time you solved. Then I can create vector V (displacement from targets inital to final positon) and add it to U (displacement of ship to target) to get vector C. Then I can convert C to a point and rotate towards it. If I find the angle of the velocity vector, would this just benefit me in efficiency such that I automatically know the angle to rotate to? $\endgroup$ – Maheer Aeron May 16 '17 at 21:43
  • $\begingroup$ @MaheerAeron The method you describe will work. If you only need the direction to fire, then your vector C and my final vector $\vec{v}_b$ are equivalent, since they are parallel. There won't be any difference in efficiency either. $\endgroup$ – Mark H May 16 '17 at 22:17
  • $\begingroup$ What's the difference between solving for time like your method versus splitting it into x and y components. I'm assuming you'll find time for the x component and a separate time for the y component then? Then you'd take the timeX and multiply the x component of the targets velocity and you can take the timeY and multiply the y component of the targets velocity. $\endgroup$ – Maheer Aeron May 19 '17 at 23:20
  • $\begingroup$ @MaheerAeron Time isn't a vector. The time found for the x components has to be the same as for the y components in order for the bullet to hit the target. Separating into components doesn't seem easier than my method. I'll edit my answer to put in the final formula for t. $\endgroup$ – Mark H May 20 '17 at 0:05
0
$\begingroup$

In problems like this it is usually very convenient to set the time as additional dimension of your vector space. In this case you make your 2D problem 3D with the $z$-component actually being time. Solving your then 3D vector equations gives the time immediately as 3rd component.

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.