Hello,
First of all, I'd like to welcome everyone as this is my first post here . Secondly, sorry for the length of this post, I'll try to be as concise as possible .
I work on a 2D space shooting game with physics, so that ships have inertia etc. I'd like to implement projectile dodging by AI. In order to do that I must determine whether given ship is going to collide with given projectile. I want to check if at any point in time within some time frame [0, T] distance between points representing ship and projectile falls below some threshold and, if it does, consider it to be a collision. So, I have come up with equations of motion for ship and projectile and constructed a distance function with respect of time (I skipped square root):
$$D(t) = (x_{ship}(t)-x_{projectile}(t))^2 + (y_{ship}(t)-y_{projectile}(t))^2 $$
Since I'm only interested if that distance ever falls below some threshold, I think that it's enough to find minimum of aforementioned function for time in range [0, T]. To find that minimum I want to check D(t) value for:
$$t = 0$$
$$t = T$$
$$t \rightarrow D'(t) = 0$$
Is this correct up to this point? Is there any simpler way to achieve the same?
Now, time for the hard part . Checking distance at t = 0 and t = T is trivial, finding roots of D'(t) isn't .
Projectile always moves with uniform motion. Ship, however, can move in one of following ways:
- If ship's engine is turned off, it moves with uniform motion.
- If ship's engine is on, but it doesn't rotate, it moves with uniformly accelerated motion.
- If ship's engine is on and it rotates, it moves with non-uniformly accelerated motion.
Case 1. is simple and I was able to deduce a formula for finding root of D'(t), but I have problems with points 2. and 3.
In case 2. ship's equations of motion are like this:
$$x_{ship}(t) = x_0 + v_{x0}t + \frac{|F|\sin(\alpha)t^2}{2m}$$
$$y_{ship}(t) = y_0 + v_{y0}t + \frac{|F|\cos(\alpha)t^2}{2m}$$
where F is engine's force, alpha is ship's rotation (engine's force vector is applied with this rotation) and m is ship's mass, v_x0 and x_0 are ship's initial velocity and position respectively. The problem I have with these equations is that they give me a ridiculously complex cubic equation when I want to compute D'(t), and since I target mobile devices I need something simpler. Maybe there is some better way to check for collision in this case?
In case 3. I'm not sure if my equation of motion is correct. This is how I deduced it (only for x axis, y axis is similar):
- First I came up with acceleration function (omega is ship's rotation speed): $$a_x(t) = \sin(\alpha+\omega t)\frac{|F|}{m}$$
- I integrated it once and got velocity function: $$v_x(t) = v_{x0} - \frac{|F|}{m\omega}\cos(\alpha + \omega t)$$
- I integrated it once more and got position function: $$x_{ship}(t) = x_0 + v_{x0}t - \frac{|F|}{m\omega^2}\sin(\alpha + \omega t)$$
I'm worried, because I'd expect that
$$x_{ship}(0) = x_0$$
but it isn't. My physics and math skills are extremely rusty, so I'd be grateful if anyone could point me what I did wrong here.
Thanks for any input!