[Math] Shortest distance between two objects moving along two lines

derivativesgeometry

I've got two objects defined by a position vector and a velocity vector. I want to know how close they will come so I can implement avoidance behaviour. This all as to be done by and algorithm. Therefore I define two lines and the shortest distance between them:

$$d(t)=\sqrt{((xPos1 + t * xVel1) – (xPos2 + t * xVel2))^2 + ((yPos1 + t * yVel1) – (yPos2 + t * yVel2))^2}$$

Cause i only need t at the highet point of d(t) i can ignore the root.

$$f(t)=(xPos1 + t * xVel1 – xPos2 – t * xVel2)^2 + (yPos1 + t * yVel1 – yPos2 – t * yVel2)^2$$

My current approch is getting rather complicated:
First I substitute:
$$a_x=xPos1$$ $$b_x=t * xVel1$$ $$c_x=-xPos2$$ $$d_x=-t * xVel12$$
$$\Rightarrow f(t)=(a_x+b_x+c_x+d_x)^2 + (a_y+b_y+c_y+d_y)^2$$
$$\Rightarrow f(t)=({a_x}^2 + {b_x}^2 + {c_x}^2 + {d_x}^2 + 2a_xb_x + 2c_xd_x + 2a_xc_x + 2b_xc_x + 2a_xd_x + 2b_xd_x + …)$$

This results in a polynomial function of degree 2. All exponents are defined without knowing any variables. Therefore I could build the deriative and solve for $f'(t)=0$.

Is there any easy of getting t without considering approximation?

Best Answer

One simplification is to set \begin{align} a &= \text{xPos}_2 - \text{xPos}_1, \\ b &= \text{xVel}_2 - \text{xVel}_1, \\ c &= \text{yPos}_2 - \text{yPos}_1, \\ d &= \text{yVel}_2 - \text{yVel}_1. \end{align}

The vector of displacement from object $1$ to object $2$ at time $t$ is $(a + bt, c + dt)$, and the distance is $\sqrt{(a + bt)^2 + (c + dt)^2}$. Your function $f(t)$ becomes $$ f(t) = (a + bt)^2 + (c + dt)^2 = (b^2 + d^2)t^2 + 2(ab + cd)t + (a^2 + c^2). $$

In effect, what this does is to convert all your coordinates to the frame of reference of one of the two objects. The object whose frame of reference you choose is always at the point $(0,0)$ in that frame of reference, so all you have to track is the relative position of the second object, which travels along a straight line at constant speed.

Now it should be clear that $f(t)$ is the quadratic function $$ f(t) = At^2 + Bt + C \tag1$$ where $A = b^2 + d^2$, $B = 2(ab + cd)$, and $C = a^2 + c^2$. In the exceptional case where $b=d=0$, there is no relative motion, and $f(t)$ remains a constant $a^2 + c^2$. In any other case, you can find the exact minimum using derivatives, or simply recall the formula for the vertex of a parabola in this form, which tells us that the minimum occurs at $$ t_{\min} = -\frac{B}{2A} = -\frac{ab + cd}{b^2 + d^2}. $$ If you plug in known values of $\text{xPos}_1$, $\text{xPos}_2$, etc., this gives an exact solution for $t_{\min}$, which you can plug into equation $(1)$ to get the minimum distance without any guesswork or approximation.


Note that if you replaced $b_x$, $d_x$, $b_y$, and $d_y$ in your version of $f(t)$ with their equal multiples of $t$ (that is, do not replace expressions such as $t \cdot \text{xVel}_1$ with something "simpler"), expanded all the products, and combined terms with the same powers of $t$, you would eventually reduce $f(t)$ to something in the form $ f(t) = At^2 + Bt + C$ where $A$, $B$, and $C$ came out to the same values in terms of $\text{xPos}_1$, $\text{xPos}_2$, etc., as in the calculations above. It would just take more manipulation do to so.