私はシンプルなプラットフォーマーを作ろうとしています。プレイヤーがジャンプするとベロシティを一定に設定し、重力* deltaTimeで時間の経過と共にその定数を減少させます。しかし、なんらかの理由により、ジャンプの高さが変化することになります。デルタタイムをメインゲームループの定数で置き換えると、この問題は解消されるので、デルタタイムと関係があると思います。libGDX変化するジャンプの高さ
ここにrevelventコードがあります。
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);
}
世界:
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
プレーヤー:
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);
}
任意の助けもいただければ幸いです:プレイヤーがいない場合はプラットフォーマーがうまく動作しません。常に同じ高さにジャンプ!
私が知っている限り、それはまさに現時点で起こっていることです。プレーヤーがジャンプすると、yの速度が設定され、プレーヤーが重力を更新するたびに、yの速度からdeltaTimeが減算されます。しかし、助けてくれてありがとう! – Calvin