1
$\begingroup$

I'm trying to make Object2 to hit Object1.

$x_1$ X coordinate for Object1 after time t has passed

$y_1$ Y coordinate for Object1 after time t has passed

$a_{1x}$ Acceleration of Object1 along the X-axis

$a_{1y}$ Acceleration of Object1 along the Y-axis

$v_{1x}$ Velocity of Object1 along the X-axis at start (t=0)

$v_{1x}$ Velocity of Object1 along the Y-axis at start (t=0)

$s_{1x}$ X coordinate of Object1 at start(t=0)

$s_{1y}$ X coordinate of Object1 at start(t=0)

Most of the values are the same for the Object2, with these exceptions:

$a_{max}$ denotes the acceleration of Object2.

$\alpha$ Is the direction of the acceleration of Object2

$x_1=\frac{1}{2}a_{1x}t^2+v_{1x}t+s_{1x}$

$y_1=\frac{1}{2}a_{1y}t^2+v_{1y}t+s_{1y}$

$x_2=\frac{1}{2}a_{max}cos(\alpha)t^2+v_{2x}t+s_{2x}$

$y_2=\frac{1}{2}a_{max}sin(\alpha)t^2+v_{2y}t+s_{2y}$

I'd like to solve $\alpha$, to get the direction Object2 should accelerate towards to eventually collide with Object1.

This has proven quite the task for my limited math skills.

My first course of action was to solve X and Y coordinates individually, to get the time to close distance in either direction.

$x_1=x_2$

$\frac{1}{2}a_{1x}t^2+v_{1x}t+s_{1x} = \frac{1}{2}a_{max}cos(\alpha)t^2+v_{2x}t+s_{2x}$

$\frac{1}{2}(a_{1x}-a_{max}cos(\alpha))t^2+(v_{1x}-v_{2x})t+(s_{1x}-s_{2x})= 0$

$t=\frac{-(v_{1x}-v_{2x})±\sqrt{(v_{1x}-v_{2x})^2-4*\frac{1}{2}(a_{1x}-a_{max}cos(\alpha))(s_{1x}-s_{2x})}}{2*\frac{1}{2}(a_{1x}-a_{max}cos(\alpha))}$

then the same for Y

$t=\frac{-(v_{1y}-v_{2y})±\sqrt{(v_{1y}-v_{2y})^2-4*\frac{1}{2}(a_{1y}-a_{max}sin(\alpha))(s_{1y}-s_{2y})}}{2*\frac{1}{2}(a_{1y}-a_{max}sin(\alpha))}$

Then, to collide, the time at which x and y are zero, must be the same.

$t=t$

$\frac{-(v_{1x}-v_{2x})±\sqrt{(v_{1x}-v_{2x})^2-4*\frac{1}{2}(a_{1x}-a_{max}cos(\alpha))(s_{1x}-s_{2x})}}{2*\frac{1}{2}(a_{1x}-a_{max}cos(\alpha))} = \frac{-(v_{1y}-v_{2y})±\sqrt{(v_{1y}-v_{2y})^2-4*\frac{1}{2}(a_{1y}-a_{max}sin(\alpha))(s_{1y}-s_{2y})}}{2*\frac{1}{2}(a_{1y}-a_{max}sin(\alpha))}$

I have no idea how to solve this. I'd like to end up with a formula on how to calculate $\alpha$ but I just can't do it.

  • 0
    From your first "t=" on down to the end, each line has a superfluous close-parenthesis.2017-01-09
  • 0
    Corrected, any more errors?2017-01-09
  • 0
    Yes, one other thing. It is unclear to me what the scope of the radicals is in those four expressions. What you should do is something like this $\sqrt\{W\}$ To show the exact extent of the radical. That will give you $\sqrt{W}$2017-01-09
  • 0
    There, I have never written in this syntax before.. I thought I was supposed to use parentheses.2017-01-09
  • 0
    I am no expert, either. If you want a preliminary source of information on MathJax, go to meta.math.stackexchange.com/questions/5020 . One other thing: For a place where you can formulate a complicated presentation (question or answer) go to meta.math.stackexchange.com/questions/4666. There you can experiment in an unhurried fashion. You're doing fine.2017-01-09

2 Answers 2

1

Interesting question! I'll give this a try.

It's useful to think in terms of vectors, so let's vectorize everything and define $\mathbb{p}_1=[x_1, y_1]$, $\mathbb{a}_1=[a_{1x}, a_{1y}]$, $\mathbb{v}_1=[v_{1x}, v_{1y}]$, $\mathbb{s}_1=[s_{1x}, s_{1y}]$, and so on. The equations of motion are

$$\mathbb{p}_1 = \frac12 \mathbb{a}_1t^2+\mathbb{v}_1t+\mathbb{s_1}$$ $$\mathbb{p}_2 = \frac12 \mathbb{a}_2t^2+\mathbb{v}_2t+\mathbb{s_2}$$

(For now, we relax the constraint $\exists\alpha,\,\mathbb{a}_2 = a_\text{max} [\cos\alpha,\sin\alpha]$)

Now let us define the state of the Object1 relative to Object2.

$$\mathbb{p}_r = \mathbb{p}_1-\mathbb{p}_2$$ $$\mathbb{a}_r = \mathbb{a}_1-\mathbb{a}_2$$ $$\mathbb{v}_r = \mathbb{v}_1-\mathbb{v}_2$$ $$\mathbb{s}_r = \mathbb{s}_1-\mathbb{s}_2$$

The relative motion of the target Object1 can be described by the equation

$$\mathbb{p}_r = \frac12 \mathbb{a}_rt^2+\mathbb{v}_rt+\mathbb{s_r}$$

Let $T$ be the time of the collision. $\mathbb{p}_r$ should be zero at $t = T$. Substituting and rearranging,

$$ \mathbb{a}_r = -2\cdot\frac{\mathbb{v}_rT+\mathbb{s}_r}{T^2}$$

By definition of $a_2$, $$ \mathbb{a}_2 = \mathbb{a}_1 + 2\cdot\frac{\mathbb{v}_rT+\mathbb{s}_r}{T^2}\quad(*)$$

Note that everything on the RHS but $T$, is known. Now define a function

$$ f(T) = \lVert \mathbb{a}_2\rVert - a_\text{max} = \left\lVert\mathbb{a}_1 + 2\cdot\frac{\mathbb{v}_rT+\mathbb{s}_r}{T^2} \right\rVert - a_\text{max}$$

Solve $f(T) = 0$ numerically for $T > 0$. Since this is just an univariate root-finding problem, the code should be fast enough for a game engine. The solution might not exist, so you might want to think about how to handle that case in your code.

If a solution exists, substitute $T$ back to the Equation $(*)$ to get $\mathbb{a}_2$, which is equal to $a_\text{max} [\cos\alpha,\sin\alpha]$, and we should be able to solve for $\alpha$.


Update: With this method, I found a solution for your case in your comment in Leibovici's answer. $\mathbb{a}_2 = [0.0832957, 3.99913]$, which corresponds to $\alpha = 88.8068^\circ$. The collision occurs at $t = 9.80017$. Both objects were moving in straight lines, not much action going on.

For shits and giggles, I also simulated the case where $\mathbb{a}_1 = [-5, -7],\,\mathbb{v}_2 = [3,-3], \,a_\text{max} = 6.3$, ceteris paribus.

In this case, there are two solutions for $f(T)$: $T = 2.89513$ or $5.48476$. I chose the former, the missile thrust angle is calculated to be $\alpha=166.194^\circ$.

enter image description here

The missile hit the target in less than 3 seconds, when the target is desperately trying to maneuver around it. Looks like your game will be awesome!

  • 0
    What kind of method should I use for finding the root? Newtons?2017-01-12
  • 0
    [Here's](http://imgur.com/a/aE6a1) a plot of $f(T)$ vs $T$ in my test case. Newton's should be good, and it converges very quickly, if we can find the suitable initial guess. I did not study the behavior of this function very closely, but my suggestion is to check the value of $f(T)$ and $f'(T)$ and proceed with Newton's only if $f(T) > 0$ AND $f'(T) < 0$, otherwise, halve the initial guess.2017-01-12
  • 0
    I must admit that I'm finding a bit trouble in making the derivative. It's been a good 10 years since I last did that. My calculator won't solve it. Or maybe I misunderstand the meaning of the double vertical line.2017-01-12
  • 0
    Bingo. I'm too scared to do that too, lol (but luckily I have Mathematica to my rescue. It solves almost everything.) The double vertical line is the vector magnitude, you can try expanding everything so that you work with scalar variables instead of vectors, and work _patiently_ from there. Expand the squares in the radical and remember your chain rule. It won't look pretty at all.2017-01-12
1

I think that obtaining an analytical solution would result in a nightmare and what I would suggest is a numerical approach.

The variables being $t$ and $\alpha$, you need to solve two non linear equations $$f(t,\alpha)=x_1-x_2=0$$ $$g(t,\alpha)=y_1-y_2=0$$ What I should try is to minimize $$\Phi(t,\alpha)=f^2(t,\alpha)+g^2(t,\alpha)$$

In a first step, you could make a grid search to try to locate the minimum and then, either continue with optimization or with Newton-Raphson if the smallest value of $\Phi$ you obtained by the grid search is "sufficiently" small. What could also be nice is to build a contour plot (or a three dimension plot) of $\Phi$ as function of $t$ and $\alpha$.

If you want, post a set of values of the different constants appearing in the equations and I shall try.

  • 0
    After spending hours to try and solve this, I think you are right, the analytical solution is a nightmare. Numeric method won't work in my case either since I'm making a game engine for fun and trying to make missiles hit their targets precisely. The problem is that I want to calculate the desired heading for thousands of missiles in as little time as possible. My current implementation tries to cancel out (sideways)relative velocity.2017-01-09
  • 0
    @0xbaadf00d. Just for my fun, give me, please, a set of constants (if you want, my e-mail address is in my profile).2017-01-09
  • 0
    My game engine uses fixed point arithmetic and 64 bit integers, so you don't want those numbers. Here's where my current logic encounters a hard time: $a_{1x}=0$ $a_{1y}=2$ $v_{1x}=0$ $v_{1x}=10$ $s_{1x}=4$ $s_{1y}=-2$ $a_{max}=4$ $v_{2x}=0$ $v_{2x}=0$ $s_{2x}=0$ $s_{2y}=0$ Also, try to make a time step if you like, What I tried to make here is a logic that doesn't need to change course if parameters for Object1 stay the same.2017-01-09