GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2016-09-13 07:05:10

DJ Coco
Member
Registered: 2016-09-13
Posts: 5

collision_raycast(x1, y1, x2, y2, obj, prec, notme, [max_dist])

Expand/// collision_raycast(x1, y1, x2, y2, obj, prec, notme, [max_dist])
//
//  Returns id of collision hit ("noone" if no collision)
//  Saves impact coordinates in "raycast_hit_x" and "raycast_hit_y" (saves "noone" if no collision)
//
/// GMLscripts.com/license
{
    raycast_hit_x = noone;
    raycast_hit_y = noone;
    
    var x1, y1, x2, y2, obj, prec, notme, max_dist, tdir, tdist, ret_val;
    x1 = argument[0];
    y1 = argument[1];
    x2 = argument[2];
    y2 = argument[3];
    obj = argument[4];
    prec = argument[5];
    notme = argument[6];
    tdir = point_direction(x1, y1, x2, y2);
    tdist = point_distance(x1, y1, x2, y2);
    ret_val = noone;
    
    max_dist = tdist;
    if (argument_count > 7) {
        max_dist = argument[7];
        if (max_dist > tdist) {max_dist = tdist;}
    }
    
    var xx, yy;
    xx = x1;
    yy = y1;
    for (var i = 0; i < max_dist; i++) {
        var col = collision_point(xx, yy, obj, prec, notme);
        if (col) {
            ret_val = col;
            other.raycast_hit_x = xx;
            other.raycast_hit_y = yy;
            break;
        }
        xx += lengthdir_x(1, tdir);
        yy += lengthdir_y(1, tdir);
    }
    
    return ret_val;
}

Offline

#2 2016-09-15 04:07:32

xot
Administrator
Registered: 2007-08-18
Posts: 1,240

Re: collision_raycast(x1, y1, x2, y2, obj, prec, notme, [max_dist])

Welcome to the forums, DJ Coco, and thanks for the submission.

We already have a couple of scripts that more-or-less do what this does. Please see:

http://www.gmlscripts.com/script/collision_line_first

http://www.gmlscripts.com/script/range_finder


Abusing forum power since 1986.

Offline

#3 2016-09-17 12:17:57

DJ Coco
Member
Registered: 2016-09-13
Posts: 5

Re: collision_raycast(x1, y1, x2, y2, obj, prec, notme, [max_dist])

Oh, all right. Somehow I totally missed those, sorry.

Offline

Board footer

Powered by FluxBB