Let's say your tiles are either solid or not. Here's some pseudocode for creating fewer physics shapes, by consolidating horizontally-adjacent tiles into larger rectangles.
More aggressive consolidation should be possible (e.g. rectangles that include tiles from more than one row), but I haven't figured out how to do it yet.
Code: Select all
Create one body B for the immobile tiles, with mass zero
For every row,
Initialize a boolean PreviousSolid to false
Initialize an int StartSolid to 0
For every tile in this row,
If this tile is solid and PreviousSolid is false
set StartSolid to the X coordinate of this tile
If PreviousSolid is true and (this is the last tile in the row OR this tile is not solid)
create a new rectangle Shape
with Y coordinates spanning from the top of this row to the bottom of this row
with X coordinates spanning from StartShape to the X coordinate of this tile
attach this shape to B
PreviousSolid = this tile is solid
Whenever I mention "the X coordinate of this tile" or similar, you may need to do some +-0.5 tweakery depending on what conventions you are using for the size/placement of your tiles. But it gets the gist across.