I’m working on a 2D game. I have the game map saved on a js object {} called gameMap. My problem is that reading an item on the matrix takes too long. For collision detection I usually have to check 10 or 20 items of the map matrix which takes around 1ms and having 10 characters on screen collision detection becomes the bottleneck of the app, taking 10ms of the 16ms each frame should last. Also when the map gets too big times scale up.
Let’s say Map has 1000 x 1000 items. Right now if I want to check what is at position (-100,200) I check gameMap['-100'][200]. My idea is to divide the map in quadrants that would group 100 x 100 items. So to check (-100,200) I would test gameMap[quadrantName][-100][200]. This would mean that while gameMap would be about the same size it would work with a lot less items, and probably read speed would scale up in a far smaller proportion. Does anyone know if this would make reading faster? What else can I do to improve reading speed?
First of all, a 10000×10000 array of bytes will consume 100MB! Do you really need such a large array. Perhaps you’d be better of storing just the coordinates of all your elements…
As to your question – you could convert your 2d array to a 1d array and access all cells via
where 10000 would be the ‘width’ of your map. So there would be no need to divide the map into quadrants.