instance_closest_approach
/*
** Usage:
** instance_closest_approach(inst[,time])
**
** Given:
** inst instance id
** time set to true to return time rather than distance (optional)
**
** Returns:
** the distance in pixels or time in steps until the calling and given
** instances are at their nearest separation based on their current
** positions and speeds. The return is zero if the instances are
** moving in parallel, negative if the instances are diverging.
**
** GMLscripts.com
*/
{
var x1,y1,x2,y2,dh,dv,t;
x1 = x;
y1 = y;
x2 = argument0.x;
y2 = argument0.y;
dh = argument0.hspeed - hspeed;
dv = argument0.vspeed - vspeed;
if ((dh == 0) && (dv == 0)) {
if (argument1) return 0;
else return point_distance(x,y,argument0.x,argument0.y);
}else{
t = -((x2 - x1) * dh + (y2 - y1) * dv) / (sqr(dh) + sqr(dv));
if (argument1) return t;
else return sign(t) * point_distance(x + t * hspeed, y + t * vspeed,
argument0.x + t * argument0.hspeed, argument0.y + t * argument0.vspeed);
}
}
** Usage:
** instance_closest_approach(inst[,time])
**
** Given:
** inst instance id
** time set to true to return time rather than distance (optional)
**
** Returns:
** the distance in pixels or time in steps until the calling and given
** instances are at their nearest separation based on their current
** positions and speeds. The return is zero if the instances are
** moving in parallel, negative if the instances are diverging.
**
** GMLscripts.com
*/
{
var x1,y1,x2,y2,dh,dv,t;
x1 = x;
y1 = y;
x2 = argument0.x;
y2 = argument0.y;
dh = argument0.hspeed - hspeed;
dv = argument0.vspeed - vspeed;
if ((dh == 0) && (dv == 0)) {
if (argument1) return 0;
else return point_distance(x,y,argument0.x,argument0.y);
}else{
t = -((x2 - x1) * dh + (y2 - y1) * dv) / (sqr(dh) + sqr(dv));
if (argument1) return t;
else return sign(t) * point_distance(x + t * hspeed, y + t * vspeed,
argument0.x + t * argument0.hspeed, argument0.y + t * argument0.vspeed);
}
}
[Please Login]
Projects: 2
Contributors: paul23, xot
comments powered by Disqus

Related: