Reducing AABB by combining tiles
Recently I make a terrible mistake, create AABB collision rectangle for each tile, in my wip game. The collision system contains 3941 object and takes 63% of execution time. The debug draw looks like a spider web:

The most straightforward method is merging small tiles into larger rectangle to reduce total collision object number. This problems is very similar to Algorithm for βhealingβ multiple rectangles into a smaller number of rectangles . The algorithm has to step:
- for each row
- combine continuous tiles into stripe shape rectangle
- from top to bottom row
- if it and next row have stripes with same horizontal range
- merge these stripes
After running first step, my collision objects reduce to 363 and collision system only take 38% of execution time. Now the debug draw becomes zebra crossing:

The first run give us enough confidence to show tile sprites ( without covering by bunch of debug draw ). So the second step makes my collision objects reduce to 48 and the debug draw finally look decent:

Even the author thinks this method is not an optimal. But, it is actually very suitable for merge in-door tiles ( dungeon, rooms ). It is shame that I do not use this approach in first place. After reducing collision objects, the FPS increases from 70 to 210. The dramatic improvement also points out how big this mistake is. I guess render system needs some optimizations too. (ο½γ»Ογ»Β΄)
Reference:
2 thoughts on “Reducing AABB by combining tiles”