4
\$\begingroup\$

I am trying to implement Box2D into my top down tile game in LibGDX. Each world consists of a set of chunks, and each chunk is 16 tiles wide/long. The maximum/minimum chunks loaded at one time is 9 (a 3x3 set of chunks with the player in the middle). This means that there are 2304 tiles active at all times.

I have tried creating a Static box shape for each of these tiles but performance was understandably terrible. I then tried creating a box shape for each of these tiles that are solid. Performance was still terrible because at one time there was 500-1000 individual boxes.

One partial solution I found to this is reducing the chunk size. At chunk size 8 with boxes around only solid tiles performance was 60 fps on mobile devices, however this was only sustained when the chunks that were loaded did not contain many solid tiles so this isn't really a solution.

I then came across a potential solution which would join all solid adjacent tiles within a chunk, so that instead of having a box for each individual solid tile, there would be a irregular polygon for each set of connected solid tiles. This solution appears to be the best, however what would happen when I want to destroy a solid tile. I would then have to recalculate all of the tile connections within that chunk and recreate another irregular polygon. To get around this, I then thought of dividing the chunk up into 4x4 sections. Each of these sections would handle their own irregular polygon so that when a tile was destroyed only that section would need to be updated and recalcuated. This however increases the number of bodies in the box2d world compared to the previous solution where there is just 1 body for each group of solid tiles.

I am looking for any other ideas that might help.

On a side note: The only reason I am using box2D is so I can achieve a nice lighting effect with shadows and everything with box2DLights. If there are any substitutes of this library that you know of please let me know!

\$\endgroup\$
  • \$\begingroup\$ Have you done some timing to see where the performance hit is happening (collision detection, rendering, etc)? Also, your solid tiles will never collide with each other, is there some way you can skip checking tile to tile collisions? You may also be able to rule out a collision by checking the x and y coordinates of the two bodies and seeing they are too far apart to collide. \$\endgroup\$ – spectacularbob May 3 '16 at 21:14
1
\$\begingroup\$

I don't know if this is going to work, but if I was you, I would only use one chunk at a time and that is the number of tiles that are visible. So something like 32x32 chunk should be fine, it depends on your viewPort and tile seizes. That way the performance should be much better.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.