Specific Objects

Discussion in 'Programming' started by Soren11112, Nov 15, 2016.

  1. Soren11112

    Soren11112 Member

    Joined:
    Sep 18, 2016
    Posts:
    34
    I am doing a project with raycasting, and I need to find a unique id of the object hit by a collision line is that possible? And, if so how?
     
  2. Bingdom

    Bingdom Googledom

    Joined:
    Jul 1, 2016
    Posts:
    1,678
    Yes.
    You need to store it as a variable, then you can reference from it.

    instance = collision_line()
     
  3. Maximus

    Maximus Guest

    collision_line will return the first instance that it finds on the line but not the first in the direction of the line (maybe, by chance). If you want to get the closest instance that is colliding with that line you will need to check each instance individually and their distance.

    Code:
    /// collision_line_first(x1, y1, x2, y2, obj, prec, notme)
    // returns closest instance colliding with a line or noone. (closest by their origin position to (x1,y1))
    var x1, y1, x2, y2, obj, prec, notme, inst, inst_dist, ans, ans_dist;
    x1 = argument0;
    y1 = argument1;
    x2 = argument2;
    y2 = argument3;
    obj = argument4;
    prec = argument5;
    notme = argument6;
    ans = noone;
    for (var i = 0; i < instance_number(obj); i++)
    {
        inst = collision_line(x1,y1,x2,y2, instance_find(obj, i), prec, notme);
        if (inst != noone)
        {
            if (ans != noone)
            {
                inst_dist = point_distance(x1, y1, ans.x, ans.y);
                if (inst_dist < ans_dist)
                {
                    ans = inst;
                    ans_dist = inst_dist;
                }
            }
            else
            {
                ans = inst;
                ans_dist = point_distance(x1, y1, ans.x, ans.y);
            }
        }
    }
    return ans;
    
     
    Last edited by a moderator: Nov 15, 2016
  4. Soren11112

    Soren11112 Member

    Joined:
    Sep 18, 2016
    Posts:
    34
    Thanks so can I just do like, destroy_instance(instance)?
     
  5. Bingdom

    Bingdom Googledom

    Joined:
    Jul 1, 2016
    Posts:
    1,678
    If you're using GMS2 yes.

    If you're using GMS1 no,
    with(instance) instance_destroy();
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice