Predicting accelerating object collisions in a friction-less environment

246 Views Asked by At

Background

I'm building a simple game where two ships are launching missiles at each other in space.

Stuff got complicated when the ships started moving in a friction-less environment and the missiles naturally needed to do the same thing.

Premises

  1. A missile has a constant acceleration and is trying to hit a moving target in a friction-less environment.
  2. Target is also constantly accelerating in the same friction-less environment.
  3. The friction-less environment is only 2 dimensional
  4. There is no gravitation affecting the missile

Question

What math should I use to determine the direction the missile should jolt in?

What i have right now

I've implemented this answer which works quite alright. I am not 100% sure of what a, b, c etc means and not sure which math is actually implemented here.

I modified above linked answer by making the missile mimic its targets jolts and adding its own on top of those. It is an ugly solution to say the least.

Problem with current solution

The current solution requires to know the speed of the missile.

The missile is constantly accelerating, and it has no top speed.

Unless I know how far the missile will travel, I cannot know the average speed.

Above solution requires that I know the speed of the projectile.

1

There are 1 best solutions below

0
On

Here is the physics:

In one dimension $x(t)=x_0+v_0t+at^2/2$. $x_0$ is the initial coordinate, $v_0$ is the initial velocity. For your problem, you have two directions (along $x$ and along $y$) and two objects (target will be denoted by subscript T, missile by subscript M) $$ x_T(t)=x_{T,0}+v_{T,x,0} t+a_{T,x}t^2/2\\ y_T(t)=y_{T,0}+v_{T,y,0} t+a_{T,y}t^2/2\\ x_M(t)=x_{M,0}+v_{M,x,0} t+a_{M,x}t^2/2\\ y_M(t)=y_{M,0}+v_{M,y,0} t+a_{M,y}t^2/2\\ $$ It is possible that some of your initial velocities or positions are 0, but I wrote the equations for the general case. On the right side of these equations you know the initial positions and velocities, both $x$ and $y$ components, for both target and missile, and the two components of target acceleration.

When the missile hits the target $x_T(t)=x_M(t)$ and $y_T(t)=y_M(t)$. You can then rewrite these equations as: $$ (x_{T,0}-x_{M,0})+(v_{T,x,0}-v_{M,x,0})t+(a_{T,x}-a_{M,x})t^2/2=0\\ (y_{T,0}-y_{M,0})+(v_{T,y,0}-v_{M,y,0})t+(a_{T,y}-a_{M,y})t^2/2=0 $$ Now you have two equations, with unknowns $t, a_{M,x},a_{M,y}$. If you don't have another equation, you have an infinite number of solutions. You can hit the target at any time $t>0$ if you have enough acceleration. If you know the absolute value for the acceleration of the missile $a_M$, you can add it as one of your equations $a_M^2=a_{M,x}^2+a_{M,y}^2$. Or if you are interested in the direction $\theta$ with respect to the $x$ direction, you can write $a_{M,x}=a_M\cos(\theta)$ and $a_{M,y}=a_M\sin(\theta)$. Now you need to solve these equations, by eliminating $t$. Note that since this is a quadratic equation you will need to choose the positive solution. You might have multiple solutions anyway, or none if your acceleration is too small.

Once you get the solution for $a_{M,x}$ and $a_{M,y}$, you can use the first 4 equations to plot the trajectories as a function of time.