Skip to main content
1 of 4
Philipp
  • 121.5k
  • 28
  • 261
  • 342

You are checking each object against each collision tile, even when those tiles are too far away to have a chance to affect it. That's inefficient. Divide your map into zones and keep track of which zones each mobile object touches. You can do so by checking the position before and after whenever you change the position of an object. Remove it from the object-set of any zones it doesn't touch anymore and add it to the object-set of all zones it now touches.

When you check for collisions, you only check for collision of objects and tiles which are in the same zone (you might have to check adjacent zones too, when objects are large enought to span multiple zones).

I also assume (but might assume wrong), that you have a lot more collision tiles than mobile objects. When that assumption is right, most zones will always contain collision tiles, but will often not contain any mobile objects. In that case it would be better to iterate all objects and check them against the tiles in their zone instead of the other way like you are doing right now (iterate tiles and check them against mobile objects).

Philipp
  • 121.5k
  • 28
  • 261
  • 342