For a 2D game I’m working on, I’d like to figure out when a projectile reaches its closest to point to its target.
A projectile is a point that moves by a constant dx,dy per frame.
A target is another point whose speed relative to the projectile is slow enough as to be considered stationary.
I want to have the projectile explode when it is (approximately) as close to the target as it will be.
What’s a good way to calculate this?
Absolute precision is not critical, this is the client-side simulation of an event that has already been resolved on the server. I’d prefer an algorithm that was fast and simple to one that was pixel perfect.
Keep track of the previous distance, and check for the first moment the distance starts to increase.
It is only this simple because you path geometry is trivial (i.e. a straight line), but it works just fine.
This has the added advantage of working when the target is faster than you describe as well, so long as both tracks are straight (or at least gently curving).
Limitation, you step size needs to be small compared to the “size” of the explosion, or this won’t work right. In that case, you would need to precompute the next distance…