Thursday, August 4, 2016

2d - How does one prevent homing missiles from orbiting their targets?


I am developing a 2D space game with no friction, and I am finding it very easy to make a homing missile orbit its target. I am curious about anti-orbiting strategies.


A simple example is a homing missile that simply accelerates directly toward its target. If that target were to move perpendicular to the missile's trajectory then stop, the missile's acceleration toward the target would not be enough to overcome it's own velocity, and the missile could be driven into orbit around the target, as depicted:


Orbiting Problem



  • In frame 1, the missile is heading straight for its target, no problems.

  • In frame 2, the target has moved to a new position as demonstrated. The missile continues to accelerate directly toward the target (in red), while still moving toward where the target used to be (in black) due to its existing velocity.


  • In frame 3, the missile's velocity continues to carry the missile around the side of the target (black) while the acceleration vector tries desperately to pull the missile toward the target.

  • In frames 4 and beyond, the missile falls into a potentially stable orbit around the target and never reaches its goal. The black arrows indicate a velocity vector while the red lines indicate acceleration vectors at the same moment in time.


Considering that there is no friction in space, there is nothing to slow the velocity of the missile down and collapse the orbit. A possible solution would be to aim "behind" the target, and this would cause the orbit to close, but how is this done from a programming point of view?


How do I make a homing missile reach its target?



Answer



First of all, you should make all calculations about what acceleration to apply in the missile's frame of reference (that's where the missile is stationary and everything else moves around it, also often called "object coordinates" or "local coordinates" in game engines, though in our case we want the velocity to be exactly zero as well).


The idea then is not to aim for the target, but to aim for the place where the target will be at the estimated time of impact. So the general algorithm looks like this:





  1. Estimate how much time it will take for the missile to reach the target. If the target is flying directly at it (remember, the missile is stationary), it can be as simple as calculating distance / speed, in other cases it can be more complicated. If the target can try and evade you won't be able to make a perfect estimate anyway, so it's ok to not be very precise.




  2. Assuming constant speed (1st degree estimate) or constant acceleration (2nd degree estimate) of the target, calculate where it will be at the estimated time above.




  3. Calculate acceleration which will lead to the missile to be at roughly the same spot at the same time.




  4. Re-project the acceleration back from the missile's frame of reference to the global one, use that.





The important part here is to get the time estimate in the rough ballpark, and to not forget the missile's acceleration capabilities while doing so. For example, a better estimate for "the target is straight ahead of us and flying in our direction" would be to solve the equation ..


distance = speed x time + 1/2 x acceleration x time2


... for time (use negative speed for objects flying straight away from the missile), with the solution you're looking for using the standard quadratic formula being ...


time = (√(speed2 + 2 x acceleration x distance) - speed) / acceleration


Adding additional parameters - drag, for example - quickly turns this into differential equations with no algebraic solutions. This is why rocket science is so hard.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...