• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Question regarding mp grids

Hey there! So my game uses an mp grid for pathfinding. I had it so that each unit in the game is the size of a single square on the grid, but then I decided that I wanted a little more organic feeling movement, so I made it so that each unit takes up a 3x3 space on the grid. My issue now comes with the pathfinding. The path is generated from the central square of the unit and goes by each individual square, which is good because that will offer the more fluid movement I'm after, but because of that, the first step on the path is in a square taken up by the unit itself. So the unit can't move because it's colliding with itself. I could just remove itself from the grid, but then things will be able to just pass through it. Anyone got any ideas on how to solve this?

Also, when moving, I have my game check the squares along the path to see if they're open. I want to check all the surrounding squares too though now that my units take up more than a single square. So I'm wondering if the best method to do this is just checking by offsetting the coordinates and check multiple times? Or is there a better, more efficient way, like checking squares equal to the height and width of the unit?

Sorry if this is confusing, wrote this up really quick. I can elaborate more if clarification is needed. Thanks in advance for your help!
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Hey there! So my game uses an mp grid for pathfinding. I had it so that each unit in the game is the size of a single square on the grid, but then I decided that I wanted a little more organic feeling movement, so I made it so that each unit takes up a 3x3 space on the grid. My issue now comes with the pathfinding. The path is generated from the central square of the unit and goes by each individual square, which is good because that will offer the more fluid movement I'm after, but because of that, the first step on the path is in a square taken up by the unit itself. So the unit can't move because it's colliding with itself. I could just remove itself from the grid, but then things will be able to just pass through it. Anyone got any ideas on how to solve this?
As suggested above you could change the mask sprite, however this may give problems when enemies bunch together, and it also means hit detection will be way off for things like bullets (if the game has them). I'm assuming the instance stops as you have code to prevent them overlapping when they come into contact with each other? If this is so, then how are you doing that? You should be able to easily check if the collision is with itself by checking the id variable of the instance found against the id variable of the instance checking. You could even change and use the collision_ functions, which have a very specific "notme" argument to prevent self collisions.

Also, when moving, I have my game check the squares along the path to see if they're open. I want to check all the surrounding squares too though now that my units take up more than a single square. So I'm wondering if the best method to do this is just checking by offsetting the coordinates and check multiple times? Or is there a better, more efficient way, like checking squares equal to the height and width of the unit?
The easiest way to do this would be to use a collision_rectangle() check. So, if the point of the path is in the center of a grid square, and you want to check an area of 3 grid squares, the check would simply become (x - cell - (cell /2), y - cell - (cell / 2), x + cell + (cell/2), y + cell + (cell / 2)). Alternatively, using a collision_circle() might yield a better result, just using a radius (cell + (cell/2)). In both these options "cell" is the width of one cell in the mp grid. :)
 
As suggested above you could change the mask sprite, however this may give problems when enemies bunch together, and it also means hit detection will be way off for things like bullets (if the game has them). I'm assuming the instance stops as you have code to prevent them overlapping when they come into contact with each other? If this is so, then how are you doing that? You should be able to easily check if the collision is with itself by checking the id variable of the instance found against the id variable of the instance checking. You could even change and use the collision_ functions, which have a very specific "notme" argument to prevent self collisions.


The easiest way to do this would be to use a collision_rectangle() check. So, if the point of the path is in the center of a grid square, and you want to check an area of 3 grid squares, the check would simply become (x - cell - (cell /2), y - cell - (cell / 2), x + cell + (cell/2), y + cell + (cell / 2)). Alternatively, using a collision_circle() might yield a better result, just using a radius (cell + (cell/2)). In both these options "cell" is the width of one cell in the mp grid. :)
Yeah I don't think I'm too interested in changing the sprite mask to be a single square since I don't want them to overlap. Hmm is there a good way to check the id of something taking up a cell on an mp_grid? Like give the coordinates, and if the cell is not free then check what is occupying it, and then I can have my code ignore it if the id is the same as the thing trying to move?

Alternatively, if there isn't a function for that, I suppose I could have a grid manager which has a variable for each square on the grid that stores the id of something when it moves. I could check it that way, but that seems inefficient.

Thanks for the tips on checking the cells! I didn't know a collision_rectangle or collision_circle can check cells specifically, I would've thought that would've just checked the collision masks. Good to know!
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Thanks for the tips on checking the cells! I didn't know a collision_rectangle or collision_circle can check cells specifically, I would've thought that would've just checked the collision masks. Good to know!
Sorry, it seems I haven't understood correctly or haven't explained myself correctly. Those functions check for collisions with a sprite mask, NOT if a cell is occupied in the MP grid...I apologise as I thought you'd be checking for objects along the path, not for occupied grid spaces.

Alternatively, if there isn't a function for that, I suppose I could have a grid manager which has a variable for each square on the grid that stores the id of something when it moves. I could check it that way, but that seems inefficient.
This would require you to have a companion DS grid to hold the occupied/unoccupied status of the MP grid. This is actually a perfectly reasonable solution and one that I myself used in my game Skein.
 
Sorry, it seems I haven't understood correctly or haven't explained myself correctly. Those functions check for collisions with a sprite mask, NOT if a cell is occupied in the MP grid...I apologise as I thought you'd be checking for objects along the path, not for occupied grid spaces.
Ah okay that makes sense. Yeah I think the best approach is to check the grid's available cells when doing collision. I'm making a small online (LAN specifically) rts game with relatively simple pathfinding. I consider myself fluent in GameMaker so I feel ready to tackle this challenge. The way I'm handling it so far is that I'm checking collision by checking the grid in the local client's game, and if that's available, I send the movement request to the server which forwards it to the other client. That client returns if the space is clear on their end. If everything is clear, then the server tells both clients simultaneously that those cells are now occupied and the object moves to the new cell. I must admit that this is my second attempt at networking and I hope that I'm going about it in a decent way. I don't want to do all this work on it to find out I've gotta gut it and start over because my movement code is bad. But things seem to work just fine so far. Any insights on this would be super helpful!
This would require you to have a companion DS grid to hold the occupied/unoccupied status of the MP grid. This is actually a perfectly reasonable solution and one that I myself used in my game Skein.
Ah okay, yeah that could be a good way to do it. If I can store the ID of the object in the space, then I can check to see if the collision code is colliding with itself and just bypass that collision.
 
Top