Discuss and collaborate on GML scripts

You are not logged in.

#1 2021-04-04 23:11:03

Registered: 2021-04-04
Posts: 1


Expand/// intercept_course_precise(origin,target,bulletspeed);
//  Returns the course direction required to hit a moving target
//  at a given projectile speed, or (-1) if no solution is found.
//  It works even if target is moving away or towards projectile.
//      origin      instance with position (x,y), real
//      target       instance with position (x,y) and (speed), real
//      bulletspeed       the speed of the projectile, real
function intercept_course_precise(origin,target,bulletspeed)
	var di;var dist;var alpha;var a;var b;var c;var delta;var time;var final;
    di = point_direction(origin.x,origin.y,target.x,target.y);
    dist = point_distance(origin.x,origin.y,target.x,target.y);
    alpha = target.direction - di;

	//using bhaskara to solve cosine law
    a = sqr(target.speed) - sqr(bulletspeed);
    b = -2*dist*target.speed*dcos(alpha);
    c = sqr(dist);
    //cauculating delta
    delta = sqr(b) - (4*a*c);

    //checks if delta is negative if it is not uses bhaskara formula
    if sign(delta) == -1{return -1;}
    time = (-b-sqrt(delta))/(2*a);
    //them cauculate sides and apply sine law
    safety = clamp(dsin(alpha)*(target.speed*time)/(bulletspeed*time),-1,1);
    if safety == NaN{return -1;}
    final = di + darcsin(safety);
    return final;

Last edited by Perg (2021-04-05 07:48:09)


#2 2021-05-25 14:40:39

Registered: 2007-08-18
Posts: 1,239

Re: intercept_course_precise

I need some explanation about what this does differently than the existing function.

Apologies for the late reply.

Abusing forum power since 1986.


Board footer

Powered by FluxBB