I am working on a tile-based platform game. Below are the main steps at runtime to facilitate understanding of the problem.

There is an array in the form of world [x] [y], which determines the size of our world. This array is clogged with numbers that correspond to different tiles.

Tiles are squares of the same size in W and H.

After that, I bring all the tiles onto the monitor, respectively, next to it I screw the card scrolling function and the clipping function of what does not fall into the visible part of the application, i.e. "I do not draw what is not visible."

At this stage, everything is fine, it works as it should, any size of the world does not cause FPS drawdown.

It's time to add a character. For certain reasons, I decided to use a ready-made physics engine (Box2D) to check the collisions.

Fine. I create an array of bodies (under this concept in this context, I combined b2Body, b2BodyDef, b2PolygonShape) in the form [x] [y], after which we again run the array through the cycle, but this time we set the coordinates and dimensions of the bodies.

In the rendering cycle, we obtain the coordinates of the bodies and, on the basis of them, draw tiles. Everything is working.

But now we are changing the size of the world and as a result we get ... FPS drawdown! And the larger the size of the world, the stronger the FPS falls. As I understand it, this is due to the large number of static objects and I have no idea how exactly to optimize this moment. doSleep has a very small effect.

What can be done? Maybe I'm doing something wrong?

    1 answer 1

    And what static objects in question?

    As I understand you, all the "characters" are active and ALL are processed in each iteration of the game cycle - which is why the FPS drawdown most likely occurs. In such platformers, usually dynamic objects are processed only those that fall into the active part of the map. Or they are created only when they fall into the active part of the card, then are deleted.

    Try to either create / delete moving objects dynamically. Or process only visible objects in the game cycle.

    In the second case, you also need to select the desired collection type, for quick access only to visible objects and quick calculation / change of the "visibility" parameter.

    • This is what I tried to do. But due to lack of experience, it was not very successful. I refused Box2D and did a simple collision check on AABB with tiles located directly next to the player. - Aquinary