Welcome to our community

Be a part of something great, join today!

Complex Time, Distance & Speed

DNA

New member
Feb 12, 2014
1
Hi all,

I'm currently developing a game app, but I've hit a math problem, which I'm hoping someone here could help me with.

The problem:

This is a 2D board, which has cords (x,y).
I have a sub facing e.g. north, that firers a torpedo(moving e.g. 30) and the same direction as the sub. The target is a ship (moving at e.g. 7) moving left or right.

So the problem is how do I calculate a targeting calculation that measures the distance and time, as to when to fire a torp so that it will hit the ship from a distance.
Now keep this in mind, the ship will not always be at right angle. It is more likely that it will be at an odd angle...

I can safely say I had more hair before I hit this problem.

To round off what objects and values are used:

1) Sub (Location in x,y - Angle (0-360) facing);
2) Torpedo (speed);
3) Ship (Location in x,y - Angle travelling - Speed);

The angle, distance of the sub and the ship will always be different.

Have fun losing sleep, thanks to anyone and everyone that trys to help

DNA
 
Last edited by a moderator:

Ackbach

Indicium Physicus
Staff member
Jan 26, 2012
4,191
You've given us a 2D problem. Let's keep this abstract. Suppose the ship's speed is the constant $v$, its bearing is $\theta$, and its initial position is $\langle x_{s0}, y_{s0} \rangle$. Let $r= \sqrt{x_{s0}^{2}+y_{s0}^{2}}$ be the initial distance from the sub to the ship. In the navy, a "bearing" is a measure, in degrees, of the ship's direction, where North is zero, and positive angles are clockwise. This is in contrast to typical mathematics, where angles are positive counterclockwise, and are measured relative to the positive $x$ axis.

Suppose also that the torpedo has a constant speed $u$, at a bearing $\varphi$.

Now, the ship's trajectory is quite simple: $\mathbf{x}_{s}= \langle x_{s0}, y_{s0} \rangle + tv \langle \sin(\theta),\cos(\theta) \rangle$. The torpedo's trajectory is also quite simple: $\mathbf{x}_{t}=tu \langle \sin(\varphi), \cos( \varphi) \rangle$. Here I've set the origin at the submarine. What we want is a single time $t$ such that $\mathbf{x}_{s}= \mathbf{x}_{t}$, or
$$ \langle x_{s0}, y_{s0} \rangle +tv \langle \sin(\theta),\cos(\theta) \rangle=tu \langle \sin(\varphi), \cos( \varphi) \rangle.$$
This is system for $\varphi$ and $t$ in two equations:
\begin{align*}
x_{s0}+tv \sin( \theta)&= tu \sin(\varphi) \\
y_{s0}+tv \cos( \theta)&= tu \cos( \varphi).
\end{align*}
Since the equations are nonlinear, I would opt for a substitution method. I'd probably solve one of the equations for $t$, and plug that into the other:
\begin{align*}
x_{s0}&=t \left( u \sin( \varphi)-v \sin( \theta) \right) \\
t&= \frac{x_{s0}}{u \sin( \varphi)-v \sin( \theta)} \\
y_{s0}&=t \left( u \cos( \varphi)-v \cos( \theta) \right) \\
&= \frac{x_{s0} \left( u \cos( \varphi)-v \cos( \theta) \right)}{u \sin( \varphi)-v \sin( \theta)} \\
y_{s0} u \sin( \varphi)-y_{s0} v \sin( \theta)&= x_{s0} u \cos( \varphi)- x_{s0} v \cos( \theta) \\
y_{s0} u \sin( \varphi)-x_{s0}u \cos( \varphi)&=y_{s0} v \sin( \theta)-x_{s0}v \cos( \theta) \\
y_{s0} \sin( \varphi)-x_{s0}\cos( \varphi)&= \frac{y_{s0} v \sin( \theta)-x_{s0}v \cos( \theta)}{u} \\
r \sin( \varphi+ \text{atan2}(-x_{s0},y_{s0}))&=\frac{y_{s0} v \sin( \theta)-x_{s0}v \cos( \theta)}{u} \\
\sin( \varphi+ \text{atan2}(-x_{s0},y_{s0}))&=\frac{y_{s0} v \sin( \theta)-x_{s0}v \cos( \theta)}{ru} \\
\varphi+ \text{atan2}(-x_{s0},y_{s0})&= \arcsin \left( \frac{y_{s0} v \sin( \theta)-x_{s0}v \cos( \theta)}{ru} \right) \\
\varphi&= \arcsin \left( \frac{y_{s0} v \sin( \theta)-x_{s0}v \cos( \theta)}{ru} \right)
-\text{atan2}(-x_{s0},y_{s0}).
\end{align*}
The atan2 function is usually available in many programming languages. It's essentially the arctangent function, but without the usual ambiguity. That is, it'll return an angle in the correct quadrant.

If you find that these equations give you complex numbers as answers, that would be an indication that the system has no solution - entirely possible if the ship's speed is larger than the torpedo's speed and you're far away enough.
 
Last edited: