2
votes

I have just finished to develop my vertical scrolling game for android using libgdx and box2d. Everything worked fine till I implemented the handling of the game state (pause, resume,running,game over). I followed the example of superjumper and it works. Here is my code where I update the state:

   @Override
public void update(float deltaTime) {

    switch (state) {
        case GAME_RUNNING:
            assetsLoader.update(deltaTime, world);
            world.step(deltaTime, 12,6);
            updateRunning();
            break;
        case GAME_PAUSED:
            world.step(0, 12, 6);
            updatePaused();
            break;
    }

}

And here there is the function draw that I call in my render method:

public void draw(){

    switch (state) {
        case GAME_RUNNING:
            presentRunning();
            break;
        case GAME_PAUSED:
            presentPaused();
            break;
        case GAME_OVER:
            presentGameOver();
            break;
    }

}

Now the problem is that when I start the game I experiment a lot of lag (my fps are around 59-61) and my bodies(I have got only 10 of them and they move with a linear velocity that never changes) start to stutter. I have already set restitution to 0 but nothing changed so I think that the problem is inside the switch state probably because it slows down the performance of my game also because before I implemented this part I didn't experience these problems.

Is it possible to manage the game states in another way?

1
Your problem is probably in the "present___" methods, and not in the state handling itself. - EpicPandaForce
Thank for your answer @EpicPandaForce but my "present_" methods haven't got any problems. If I remove the switch part from the function draw and put only "presentRunning" and also the switch part from the function update the game works fine (obviously I cannot pause it and the player cannot die). - francesco_libgdx
How high are the FPS, when you remove the switch? To me it sounds like your state is wrong sometimes and therefore the bodies don't move for some frames, causing the stuttering. - Robert P
Do you scale the body's using a scaled camera? Or do you draw and render actual size? - Maarten

1 Answers

0
votes

A common mistake people make when using Box2D is that their pixel per meter ratio is far too low. Box2D uses meters rather than pixels so your measurements in and out of Box2D should be scaled down.

If not, you could also try lowering the Box2D updates per cycle (the 12 and 6 in world.step(...)).

If that still doesn't work, make sure you are not rendering twice, it looks like a possible cause because of the update assets call and the presentRunning() call.

If all else fails, try an if statement...