• 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!

Spatial(?) Collision Grid

I'd like to drastically cut down on collision checks by using what I believe is a spatial grid (I think?)
Basically you'd split your room into different, equal sized cells, and every object is stored into one or more of these cells if they overlap them. Objects would only need to check for collisions with objects in the same cell(s).

I'd actually be capable of doing this myself, but I'm not sure how I should go about storing the information. If I have a ds_list for each cell that would work perfectly, but it might take up more memory. If I use a ds_grid then it would be easier to handle, but adding new object ID's to it would be much more difficult.

Here's an example of what I'm going for:

https://conkerjo.wordpress.com/2009/06/13/spatial-hashing-implementation-for-fast-2d-collisions/
 

Paskaler

Member
Turn on GameMaker's Fast Collision System and that kind of optimization will run internally inside of GM itself.
 
Turn on GameMaker's Fast Collision System and that kind of optimization will run internally inside of GM itself.
Unfortunately the way my code is, I have to basically set this up myself. I put object code inside of their user events so I can run it all in order, so my code relies on my own collisions instead of GM's default ones.
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
If I have a ds_list for each cell that would work perfectly, but it might take up more memory.
You can test the expected worst case scenario (largest map size, largest allowed number of units scattered around it) and see how the memory usage looks. Unless your maps are huge or your cells are tiny, it would say in acceptable range - I had used this approach before GM had built-in spatial collision checking to cut down number of units to check when picking attack targets in a RTS game.
 
Top