The project has gone through a lot of progress in the past 3 weeks. For one thing, the title now is called ‘Sentinels of Mijil’ and has gone through several beta testing sessions and a lot of bug fixing. It’s almost two months since the initial code from scratch… and not so long enough, it’ll be A LIVE!!!…
… erm … i mean, ONLINE.
One of the aspects I took careful thought through the game is how the collision detection is done. I wanted a fast-paced action shooter with swarming enemies emerging while you get to pop em with explosion galore. So, definitely a good collision detection is key.
I’ve read several articles about it, and concluded grid spatial indexing is best for the game due to the fact that all object collisions are calculated through hitboxes and there are only 2 sizes of hitbox, plus grids are easier to do
. I had to admit I was very intrigued to use quadtrees, but I felt I needed something quick to code and that works well enough.
Grid spatial indexing is, well of course, a method of spatial indexing. In plain English, this means grouping visual information and managing them so it’s easy to retrieve the data and do whatever you like to it. In the topic of collision detection, spatial indexing is a way to sort all the objects based on visual information (coordinates, dimension, etc) from grids and calculate only the most possible group of objects that are expected to collide. Or plainly saying, just calculate objects that are near (in the same grid) and forget the others
.
Comparing to the brute-force method I mentioned earlier, spatial indexing totally rocks. Just think about it, with brute-force (or some like to call it pairwise) method you need to calculate as much as the square amount of collidable objects. For example, if you have 100 objects to collide, for every tick of game loop you need to calculate as much as 100 x 100 = 10,000 collisions! That, my friend, is a lot of processing waste. Not to mention we’re using the Flash platform here (although the AVM2 is very fast now, pairwise is still an overkill). I’ve tried it, and it is as what it’s expected: a very big drop in FPS.

Implementation is quite easy, here are my steps:
- Setup the grids at start. Of how many grids is enough for your project really depends on how big is the game window, and how big objects of collision are generally. For instance, in SoM the game window is set to 500 x 500. Hitboxes of enemy units and the size of players hitbox is approximately 20 pixels so it’s rather obvious to have 25 rows and 25 columns of grids.
- Create an array to place grid ids of grids with objects in it, this way you’ll only loop through grids in that array and not all grids. Let’s call the array CalculateGrids.
- During game tick, each object needs to register or unregister their current grid id to the CalculateGrids array. Knowing which grid the object is in is quite easy, by dividing the objects coordinates with the grid dimension will give you fast results.
- Then it’s off to calculate the actual collision, or what it’s usually called as the narrow-phase of collision detection (while spatial indexing is usually associated as the broad-phase of collision detection). There are a lot of popular methods in narrow-phase, like Axis-Aligned Bounding Box (AABB) and Separating Axis Theorem (SAT), but since the collision behaviors are very simple and nothing fancy, I think hitTestObject is quite enough.
The results are pretty much satisfying. With a swarm of 100 objects at once, no fps drop occurred. It start dropping when 400 – 500 objects are running the same time, while with pairwise since 100 it has dropped. This wasn’t an ideal situation although it can be acceptable by tweaking with the gameplay. There are several thoughts in optimizing, like:
- Replace the narrow-phase method to SAT. Sometimes I wonder if hitTestObject is actually making the lag and is that bad.
- Work on the register and unregister of CalculateGrids array. This can also be a problem as registering and unregistering can be very frequent.
- Use quadtrees.
For now I don’t have any code to share as I’m still focusing on releasing the game, but I might whip up one soon. I hope this is useful.