# Closest distance at which a missile passes by your hero

Maybe some people might be interested in this formula when trying to dodge missiles.
Knowing a missile position and velocity via appropriate methods .pos & .velocity.

p stands for position
v stands for velocity
d stands for the closest distance the missile will pass by your hero.

``````p = missile.pos ;
p0 = this.pos ;
v = missile.velocity ;
vNorm = Math.sqrt( v.x * v.x + v.y * v.y ) ;

d = Math.abs( ( v.x * ( p.y - p0.y ) )   -   ( v.y * ( p.x - p0.x ) ) )   /  vNorm ;
``````

In explicit wording, d is equal to the positive ( cross product of ( (the missile velocity) and (the vector “missile -> me”) ) ) divided by the norm of the missile velocity. (This definition would also work in 3D btw, as long as the trajectories are still linear)

A for-loop across the missiles that are close to your hero will provide you a list of dangerous missiles (if d < colisionRadius ). You might want to dodge those missiles.

For more documentation, I invite you to check Wikipedia
Edit : added 3 handwritten lines of explainations.

Explaination

7 Likes

I’m guessing this is the code you used for you apocalypse auto dodge code
Am I right?

Nah, it’s (mainly) for Sarven treasure 5, in order to dodge razor disks from HeadHunters.

My apocalypse code is more a swisscheese problem, where I define holes where I should not be at a given time. I get positions of the explosions with missile.targetPos (X,Y) and the timing is 3.6sec after a missile appears (T). So I find the safest path through this cheese, by checking the space-time distance from the explosion. Here is the idea :

``````// When a missile appears, I record the 3 parameters of the shell in an array.
X = missile.targetPos.x ;
Y = missile.targetPos.y ; // in meters.
T = this.now() + 3.6 ; // in seconds
``````

In order to know if a position (x,y) is safe at a given time t, you can check if this position (x,y,t) is far enough from the explosion.

``````dX = X - x ;
dY = Y - y ;
dT = T - t ;
v = this.velocity

distanceFromExplosion = Math.sqrt( dX*dX + dY*dY + v*v*dT*dT ) ;
``````

If distanceFromExplosion > explosionRadius (which is 10, but using 11 or 12 is safer), then the position (x,y,t) is safe.

So when I detect the position of my hero isn’t safe anymore, I compute several path around me (in a star pattern) to check that every steps (x,y) on my way to a safe position are safe (t is a future time. It is computed via t = distance / velocity). If I find one fully safe path, I do take this path. Rince and repeat.

Sometimes, there isn’t a path and the hero dies horribly. But that’s part of the fun