intercept_course

Downloadintercept_course(origin,target,speed)   Returns the course direction required to intercept a moving target at a given speed. Returns (-1) if there is no solution.
/*
**  Usage:
**      intercept_course(origin,target,speed)
**
**  Given:
**      origin      instance with position (x,y)
**      target      instance with position (x,y) and (speed)
**      speed       speed of the projectile
**
**  Returns:
**      course direction required to intercept
**      a moving target at a given speed,
**      or (-1) if no solution found
**
**  GMLscripts.com
*/

{
    var origin,target,pspeed,dir,alpha,phi,beta;
    origin = argument0;
    target = argument1;
    pspeed = argument2;
    dir = point_direction(origin.x,origin.y,target.x,target.y);
    alpha = target.speed / pspeed;
    phi = degtorad(target.direction - dir);
    beta = alpha * sin(phi);
    if (abs(beta) >= 1) {
        return (-1);
    }
    dir += radtodeg(arcsin(beta));
    return dir;
}

Click if you've used this script[Please Login]
Projects: 17

 Contributor: EricDB


comments powered by Disqus