How to create PhysicsBody for each tile on TileMap?

How to create PhysicsBody for each tile on TileMap?
0.0 0

#1

Hi everyone,
I’m currently trying to combine the inbuilt Physics Engine and TileMap classes to create the levels for my physics-based sidescroller. These functionalities work fine seperately and there is no slowdown. However there is major slowdown (down to 15fps for a long time) when I try to combine these two steps, after creating the tilemap I try to place a physics body for every non-blank tile on the tilemap layer.

for    (int x=0; x < 80; x++)       //width of map
    {
        for (int y = 0; y < 24; y++)   //height of map
        {
            auto spriteTile = wallLayer->getTileAt(Vec2(x,y));
            if (spriteTile != NULL)    
            {
                PhysicsBody* tilePhysics = PhysicsBody::createBox(Size(30.0f, 30.0f), PhysicsMaterial(1.0f, 1.0f, 0.0f));                 
                tilePhysics->setDynamic(false);   //static is good enough for walls
                spriteTile->setPhysicsBody(tilePhysics);
            }
        }
    }

As you can see I am basically bruteforcing it, the map is size 8024 tiles and each tile is 3030 pixels big. Is this inefficient with memory, and is is there a better way to assign physics bodies to tiles?

Note: wallLayer is the layer of the tilemap the tiles are on, and most of the space there is blank, so I don’t think the number of tiles is the problem.

Any insight would be helpful, thanks


#2

Sorry about reviving an old topic but I was wondering if you or anyone has found a solution to this?