GM:S 1.4 Check grid cell distance

Discussion in 'Programming' started by thandwiches01, Sep 13, 2019.

  1. thandwiches01

    thandwiches01 Member

    Joined:
    Jan 30, 2017
    Posts:
    37
    I was wanting to know if was any way I could check to see how many grid cells one object is away from another.

    I just have a very basic grid, that I have each cell set to 32x32.
    I have the player at once position in the grid, and was wanting to store the number of cells that are inbetween the player and the mouse, inside a variable.
    I've been trying for the past hour, but I can't seem to figure it out.
    Any help would be greatly appreciated.
     
  2. Relic

    Relic Member

    Joined:
    Jun 27, 2017
    Posts:
    699
    You can focus on just the x and y direction independently then sum the results. For example, if player is in [2,7] and mouse is [6,1] The x change is 6-2=4. The y change is 1-7 = -6. By taking absolute values of both x and y changes, total grid moves to get from player to mouse is 4+6=10.
     
  3. 2Dcube

    2Dcube Member

    Joined:
    Jun 23, 2016
    Posts:
    352
    script grid_distance:
    takes x1,y1 of first cell, and x2, y2 of second cell.
    Code:
    var x1 = argument0;
    var y1 = argument1;
    var x2 = argument2;
    var y2 = argument3;
    return abs(x1- x2) + abs(y1 - y2);
     
  4. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,211
    point_distance(grid_x1,grid_y1,grid_x2,grid_y2) will also do the exact same thing. There's not really a reason to write your own function when this exists, unless you're looking to extend functionality in some way.
     
  5. Relic

    Relic Member

    Joined:
    Jun 27, 2017
    Posts:
    699
    I believe the OP wants a number of grid moves, not a radial distance in grid spaces. But either way he has the answer now.
     
    RefresherTowel likes this.
  6. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,800
    That can return fractional values. You don't want fractions with grid distances.

    If you are at cell 0,0 trying to get to 2,3 that's a distance of 5 cells. Your suggestion would return 3.6 cells.
     
    RefresherTowel likes this.
  7. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,211
    @Relic @TheouAegis Ah, ok, I was assuming it calculated using the manhattan distance, but obviously not.
     

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