Reputation: 203
My game is a platformer and the screen follows the player who is constantly moving right across the backround. Every second the game slows the movement right and then speeds up again. How would I stop this from happening. I have already passed delta time into my world.step but they did not help the lag or stutter. Thanks
MAIN RENDER LOOP
public void update(float dt){
world.step(1 / 60f, 2, 2);
handleInput(dt);
camX();
player.moveMario();
hud.update();
gameCam.update();
renderer.setView(gameCam);
}
@Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
game.batch.setProjectionMatrix(gameCam.combined);
game.batch.begin();
game.batch.draw(ball, player.b2Body.getPosition().x - MarioBros.RADIUS_CHARACTER / MarioBros.PPM, player.b2Body.getPosition().y - MarioBros.RADIUS_CHARACTER / MarioBros.PPM, 70 / MarioBros.PPM, 70 / MarioBros.PPM);
mapCreator.createSpikeBalls(map, animation, game.batch);
endOfGame();
game.batch.end();
b2dr.render(world, gameCam.combined);
}
public void camX(){
gameCam.position.x = gamePort.getWorldWidth() / 2;
if(player.b2Body.getPosition().x >= gamePort.getWorldWidth() / 2){
gameCam.position.x = player.b2Body.getPosition().x;
}
}
MOVE THE CHARACTER TO THE RIGHT
public void moveMario(){
if (b2Body.getLinearVelocity().x < 4){
b2Body.applyLinearImpulse(new Vector2(2.7f, 0), b2Body.getWorldCenter(), true);
}
}
Upvotes: 0
Views: 160
Reputation: 6067
You didn't write anything about your world and body settings but I guess that the following happens:
b2Body.getLinearVelocity().x < 4
is true, you again apply an impulse to marioApart from that, you definitely should pass deltaTime to the world's step method
Upvotes: 1