1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

making players move in locaton target but not same location

Discussion in 'Programming' started by 1213brett, Dec 4, 2019.

  1. 1213brett

    1213brett Member

    Joined:
    May 30, 2018
    Posts:
    24
    I have a bunch of solders in lines. And when they are selected they can be moved to a location of the mouse click. But the problem is if I have more than one group of solders selected and have them move. They move to the same location making one group disappear behind the other. I know why this is but don't know how to go about changing things so that they move to new location at different spots. maybe you can help me thank you.


    basically this is what I have for the move code for all the units. They just all have a spot to move to based on the main spot I have put bellow called Midle. If you need more code I can provide but I left it out due to me not thinking it important to question.


    //// in units step event

    if mp_grid_path(grid,global.AMpath, x, y, Midle.x, Midle.y, 1)
    {
    path_start(global.AMpath, global.MSpeed, 0, 0);
    }

    ///// in Midle objects global left pressed event

    x=mouse_x
    y=mouse_y}
     
  2. Carcophan

    Carcophan Member

    Joined:
    Aug 4, 2019
    Posts:
    100
    Do you need them to be structured in a grid or pattern, or just “not on top of each other”?
     
  3. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    693
    They're all inhabiting the same space, because you presumably don't have anything telling them not to do so. If you want them to spread out around the end destination then you'll have to figure out a way to manage it.

    Assuming they're not the size of a grid cell this would have to be done using standard collision detection. The only problem I can think of is that none of the ones that check for actual pixel collisions will suffice - because the object has to have moved there first, so you could only do it when they've reached the end of the path and then rearrange themselves. Then you would have to do each one in turn, and only have the next one check for a free space when the previous one has finished moving.

    It has to be absolute and ordered, or they will most likely faff about constantly in a state of disarray.

    Alternatively you could use rectangle_in_rectangle, as it could represent a space which they don't yet occupy (rather than the physical collision detections which require them to be already occupying it). Find a free space: set it as their position, then use that to test for the next one, and so on. Work your way around the original destination / now occupied "places", until they have spread out of occupying the same spot.

    Sorry if I haven't explained that very well :)

    EDIT: I've done this easily enough using an mp grid, but each instance was the size of a grid cell - so ordering them was fairly straightforward. I'm assuming your instances are smaller than a grid cell, so require a different approach...??
     
  4. 1213brett

    1213brett Member

    Joined:
    May 30, 2018
    Posts:
    24
    yea im not really using a grid in those terms maybe I should.
     
  5. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    693
    Are they much smaller than the grid cells?

    Just wondering if it's not going to look that great, or be all that useful, if they occupy one grid cell each.

    EDIT: It occurred to me that rectangle_in_rectangle would be a bit tricky to implement, as the area being tested would have to "grow" with each new instance settling (to include all of the previous spaces). Say they end up with an L shape for three units...then it's not a rectangle anymore...

    Perhaps the suggestion of using physical collisions, and having them take turns to do so only when the previous unit has stopped moving, is the easiest way to achieve this?
     
    Last edited: Dec 4, 2019
  6. 1213brett

    1213brett Member

    Joined:
    May 30, 2018
    Posts:
    24
    thank you for your help the men are just under 64 by 64 and the grid is 64 but I don't really know where to go from hear at least I think the grid is

    globalvar grid;
    grid = mp_grid_create(0, 0, room_width div 64, room_height div 64, 64, 64);
    mp_grid_add_instances(grid, obj_wall, false);
     
  7. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    693
    okay - so it won't be wasting space if they occupy a grid cell each, then. What I did was have the instances in a ds priority, with their id and the distance to the point as the value. So they were in the priority going from closest unit first to furthest unit last. Then it was a for loop spreading out the start point, and just setting empty cells as each units destination. I think: this is off the top of my head :)

    I'll look for the project, and post what it had.
     
  8. 1213brett

    1213brett Member

    Joined:
    May 30, 2018
    Posts:
    24
    thank you
     
  9. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    693
    It's been a while since I made this, so explaining it would need a refresher on my part. It works, but is not at all specific about where it places things and could be refined. I include it here in the hopes that it will give you some ideas.

    All this takes place in an object called "grid_obj"

    Setting the grid point:
    Code:
    point_x = mouse_x;
    point_y = mouse_y;
    Filling the ds priority:
    Code:
    with (obj_bug)
    {
    ds_priority_add(grid_obj.closest, id, point_distance(x, y,grid_obj.point_x, grid_obj.point_y));
    }
    Looking through the mp grid:
    Code:
    var size = ds_priority_size(closest);
    var grid_x_number = room_width div 64; // <<< the grid is the same size as my room, and the cells are 64 x 64
    var grid_y_number = room_height div 64;
    var grid_x = point_x div 64;
    var grid_y = point_y div 64;
    var is_obj, dist, do_path, new_x, new_y;
    
    repeat(size)
    {
    is_obj = ds_priority_delete_min(closest);
    dist = 1000;
    do_path = false;
    new_x = point_x;
    new_y = point_y;
    if mp_grid_get_cell(grid, grid_x, grid_y) != 0
    {
    for (a = 0; a < grid_x_number; a++)
    {
    for (b = 0; b < grid_y_number; b++)
    {
    if mp_grid_get_cell(grid, a, b) == 0
    {
    new_dist = point_distance(grid_x, grid_y, a, b);
    if new_dist < dist
    {
    new_x = a;
    new_y = b;
    dist = new_dist; 
    new_x *= 64;
    new_x += 32;
    new_y *= 64;
    new_y += 32;
    do_path = true;
    }
    }
    }
    }
    }
    else 
    {
    do_path = true;
    }
    
    if do_path
    {
    path = path_add();
    mp_grid_path(grid, path, is_obj.x, is_obj.y, new_x, new_y, true);
    with (is_obj)
    {
    if path_exists(path)
    {
    path_delete(path);
    }
    path = path_duplicate(grid_obj.path);
    path_start(path, 2, path_action_stop, 0);
    }
    path_delete(path);
    mp_grid_add_cell(grid, floor(new_x / 64), floor(new_y / 64));
    }
    }
    
    EDIT: Messed it up, but now it's fixed
     
    Last edited: Dec 4, 2019

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