指定角度最近实例判断——instance_nearest_dir

脚本说明

在GMS2中有一个自带的函数instance_nearest可以用来获取最近的实例,你可以指定某一坐标和甄别的对象,然后设置一个要判断的方向角度,再定义最小和最大的偏差值,这个脚本会去判断“(角度-最小偏差值)到(角度+最大偏差值)”这一个角度范围内的距离你最近的实例并将实例ID返回。

参数说明

参数序号 参数名 参数说明
argument0 x 要判断的起点x坐标
argument1 y 要判断的起点y坐标
argument2 obj 需要判断是否有实例的对象
argument3 dir 方向角度
argument4 min 最小偏差角度
argument5 max 最大偏差角度

代码正文

///instance_nearest_dir(x,y,obj,dir,min,max);
//
// gets the nearest instance in a direction within a range of dir - min to dir + max degrees
//
//
//      x      the x position to check from
//      y      the y position to check from
//      obj    the object type to check for
//      dir    the direction to check within 90 degrees of
//      min    the ammount less than the direction to check
//      max    the ammount more than the direction to check
//
/// GMLscripts.com/license

var _x = argument[0];
var _y = argument[1];
var _obj = argument[2];
var _dir = argument[3];
var _min = argument[4];
var _max = argument[5];

var _nearst = noone;
var _nearstdist = 0;

with(_obj){
    var _nearst_point_dir = point_direction(_x,_y,x,y);
    var _nearst_point_dist = point_distance(_x,_y,x,y);
    show_debug_message(string(angle_difference(0,_nearst_point_dir)) + "ang");    
    if(angle_difference(0,_nearst_point_dir) >= _dir - _min && angle_difference(0,_nearst_point_dir) <= _dir + _max
    && (_nearst == noone || _nearst_point_dist < _nearstdist)){
        _nearst = id;
        _nearstdist = _nearst_point_dist;
    }
}

return _nearst;

本脚本转自GMLSCRIPT.COM,原作者r0wan
原帖地址: csv_to_grid

2017-12-25 22:34
Comments
Write a Comment