I am trying to make a simple platformer, and when the player jumps I set the velocity to some constant, and then decrease that constant over time by gravity * deltaTime. However, for some reason this is resulting in varying jump height. I think it has to do with the deltaTime because when I replace deltaTime with a constant in the main game loop this issue goes away.
Here is the revelvent code.
GameState
public void resolveInput(float deltaTime) {
if(Gdx.input.isKeyJustPressed(Keys.ESCAPE))
Gdx.app.exit(); //temporary until we build menus
player.velocity.x = 0;
if(Gdx.input.isKeyPressed(Keys.A)) {
player.velocity.x -= Constants.PLAYER_MOVE_SPEED * deltaTime;
player.right = false;
}
if(Gdx.input.isKeyPressed(Keys.D)) {
player.velocity.x += Constants.PLAYER_MOVE_SPEED * deltaTime;
player.right = true;
}
if(Gdx.input.isKeyPressed(Keys.SPACE) && !player.falling) {
player.velocity.y = Constants.PLAYER_JUMP_SPEED;
player.falling = true;
}
}
public void update(float deltaTime) {
resolveInput(deltaTime);
world.update(deltaTime);
camHelper.update(deltaTime);
}
World:
public void update(float deltaTime) {
player.update(deltaTime);
player.setPosition(resolveCollisions());
}
public Vector2 resolveCollisions() {
Rectangle p = player.getBoundingRectangle();
//The rest of the code doesn't get called in my test cases so I won't put it in here...
return new Vector2(p.x + player.velocity.x, p.y + player.velocity.y);
}//resolveCollisions
Player:
public void update(float deltaTime) {
if(velocity.len() == 0)
sTime = 0;
else
sTime += deltaTime;
if(falling)
velocity.y -= Constants.GRAVITY * deltaTime;
velocity.y = MathUtils.clamp(velocity.y, -Constants.PLAYER_MAX_SPEED, Constants.PLAYER_MAX_SPEED);
}
Any help would be much appreciated: a platformer doesn't work well if the player doesn't always jump the same height!