1
votes

Okay so I have a side scrolling game where I use box2d for the physics. My camera focuses on the player and only the player moves. The player moves by using box2d, I have set the linear velocity and everything has a friction of 0. The obstacles seem to stutter back and forth every once in a while and sometimes it seems to last forever or from the very beginning it is stuttering.

The way the player's sprite is rendered is that the player has a update method that gets the location of the corresponding box2d body and stores that location for it to be used in its render method. I do use PPM and the everything else is a static body.

I have a sensor following behind the player that will detect and blocks going off screen and reuse these blocks as more obstacles and I do similar things for the background and the floor so the player can run forever. There are only 2 background object 2 floor object and 12 block objects. I made sure that there is no costly operations in my render methods and I do not create any new objects outside of the initial load of the state. Using

Gdx.graphics.getFramesPerSecond()

I have determined that the fps goes between 60 and 61 so I don’t believe this is a lag issue and I have no idea how to fix this.

Main Render method:

 public void render(){
    //clear screen
    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);

    //set camera to follow player
    cam.position.set(player.getPosition().x * B2DVars.PPM + NinjaJumper.V_WIDTH / 4,
            NinjaJumper.V_HEIGHT/2, 0);
    cam.update();

    sb.setProjectionMatrix(cam.combined);
    //render the background
    for (int i = 0; i < backgrounds.length; i++){
        backgrounds[i].render(sb);
    }

    //render the player
    player.render(sb);

    if(haveBlocks) {
        for (int i = 0; i < blocks.length; i++) {
            blocks[i].render(sb);
        }
    }

    //render the ground
    for (int i = 0; i < ground.length; i++){
        ground[i].render(sb);
    }

    sb.setProjectionMatrix(hudCam.combined);
    hud.render(sb);

    if(debug) {
        //set camera to follow player
        b2dCam.position.set(player.getPosition().x + (NinjaJumper.V_WIDTH / 4) / B2DVars.PPM,
                (NinjaJumper.V_HEIGHT / 2) / B2DVars.PPM, 0);
        b2dCam.update();
        b2dr.render(world, b2dCam.combined);
    }
}

Player render and update methods:

public void update(float dt){
    animation.update(dt);
    sprite.setRegion(animation.getFrame());
    sprite.setCenter(body.getPosition().x * B2DVars.PPM,
            body.getPosition().y * B2DVars.PPM  - 5);
}

public void render(SpriteBatch sb){
    sb.begin();
    sprite.draw(sb);
    sb.end();
}

So can someone tell me what I'm doing wrong, I've spent days looking at this and I can't figure it out.

edit pack.pack:

pack.png
format: RGBA8888
filter: Linear,Linear
repeat: none
mainpage
  rotate: false
  xy: 1, 281
  size: 960, 720
  orig: 960, 720
  offset: 0, 0
  index: -1
ground
  rotate: false
  xy: 1, 140
  size: 600, 139
  orig: 600, 139
  offset: 0, 0
  index: -1
treeM
  rotate: false
  xy: 603, 38
  size: 139, 241
  orig: 139, 241
  offset: 0, 0
  index: -1
tree
  rotate: false
  xy: 744, 39
  size: 137, 240
  orig: 137, 240
  offset: 0, 0
  index: -1
treeS1
  rotate: false
  xy: 883, 42
  size: 133, 237
  orig: 133, 237
  offset: 0, 0
  index: -1
treeS
  rotate: false
  xy: 1, 19
  size: 67, 119
  orig: 67, 119
  offset: 0, 0
  index: -1
button1
  rotate: false
  xy: 70, 114
  size: 70, 24
  orig: 70, 24
  offset: 0, 0
  index: -1
button2
  rotate: false
  xy: 70, 88
  size: 70, 24
  orig: 70, 24
  offset: 0, 0
  index: -1
platform
  rotate: false
  xy: 963, 969
  size: 32, 32
  orig: 32, 32
  offset: 0, 0
  index: -1
block
  rotate: false
  xy: 1, 1
  size: 16, 16
  orig: 16, 16
  offset: 0, 0
  index: -1

pack2.png
format: RGBA8888
filter: Linear,Linear
repeat: none
treeM1
  rotate: false
  xy: 1, 625
  size: 255, 378
  orig: 255, 378
  offset: 0, 0
  index: -1
treeH
  rotate: false
  xy: 258, 540
  size: 200, 463
  orig: 200, 463
  offset: 0, 0
  index: -1
ninja1
  rotate: false
  xy: 460, 503
  size: 500, 500
  orig: 500, 500
  offset: 0, 0
  index: -1
ninja2
  rotate: false
  xy: 1, 1
  size: 500, 500
  orig: 500, 500
  offset: 0, 0
  index: -1
ninja3
  rotate: false
  xy: 503, 1
  size: 500, 500
  orig: 500, 500
  offset: 0, 0
  index: -1

pack3.png
format: RGBA8888
filter: Linear,Linear
repeat: none
ninja4
  rotate: false
  xy: 1, 503
  size: 500, 500
  orig: 500, 500
  offset: 0, 0
  index: -1
ninja5
  rotate: false
  xy: 503, 503
  size: 500, 500
  orig: 500, 500
  offset: 0, 0
  index: -1
ninja6
  rotate: false
  xy: 1, 1
  size: 500, 500
  orig: 500, 500
  offset: 0, 0
  index: -1
ninja7
  rotate: false
  xy: 503, 1
  size: 500, 500
  orig: 500, 500
  offset: 0, 0
  index: -1

pack4.png
format: RGBA8888
filter: Linear,Linear
repeat: none
mainpage1
  rotate: false
  xy: 1, 1
  size: 900, 600
  orig: 900, 600
  offset: 0, 0
  index: -1

pack5.png
format: RGBA8888
filter: Linear,Linear
repeat: none
ninja8
  rotate: false
  xy: 1, 1
  size: 500, 500
  orig: 500, 500
  offset: 0, 0
  index: -1
1
are you using TexturePacker with trimming?m.antkowicz
I'm using the TexturePacker but I don't know what trimming is.user1804234
in TP you have something like "Trim mode" have you set it to somethnig else that none ?m.antkowicz
I don't see anything that says trim mode. All my settings are default except min filter and mag filter but I've had this problem before I changed these.user1804234
ok then please share to me your .atlas / .pack filem.antkowicz

1 Answers

-1
votes

There are two possible areas possible to be a source of stuttering - rendering and physics.

When you are using trimming in TexturePacker you need to handle offset during drawing but it is not your case - please notice you have all offsets zeroes and size is exactly the same as orig.

You should focus on your physics. There is well known bug in box2D appears when two bodies - small and big - are in contact and causing stuttering. Please try these two things:

  • use Box2D debug renderer to see if your bodies are stuttering or just a sprite
  • try to log out body.getPosition() in every step to see if x or y is changing in proper way

If it is problem with physics the solution is to set fixture material restitution to 0 and try to setLinearDamping to avoid a little moves