2016-11-06 12 views
0

私はシンプルなプラットフォーマーを作ろうとしています。プレイヤーがジャンプするとベロシティを一定に設定し、重力* 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); 
} 

任意の助けもいただければ幸いです:プレイヤーがいない場合はプラットフォーマーがうまく動作しません。常に同じ高さにジャンプ!

答えて

0

新しい変数firstYと新しい値JUMP_HEIGHTを宣言することはどうですか?

GameState:

if(Gdx.input.isKeyPressed(Keys.SPACE) && !player.falling) { 
    player.firstY = player.getBoundingRectangle().y; 
    player.velocity.y = Constants.PLAYER_JUMP_SPEED; 
    player.falling = true; 
} 

世界:

public Vector2 resolveCollisions() { 
    Rectangle p = player.getBoundingRectangle(); 
    //JUMP_HEIGHT is a height player always jumps 
    if(p.y + player.velocity.y >= player.firstY + Constants.JUMP_HEIGHT) { 
    player.velocity.y = 0; 
    return new Vector2(p.x + player.velocity.x, player.firstY + JUMP_HEIGHT); 
    } 
    return new Vector2(p.x + player.velocity.x, p.y + player.velocity.y); 
} 

私は、これはあなたを助けることを願っています。

0

ジャンプの高さは、明らかにdeltaTimeのために変化します。 deltaTimeは、各フレーム間の時間であり、この時間はフレームによって異なります。そのため、常に異なる数値で乗算すると、ジャンプの高さは常に異なることになります。

あなたはvelocity.yを常に同じにするだけでこれを修正できます。

ユーザーがジャンプするたびにvelocity.yを一定の定数にしてから、重力を各フレーム(重力にdeltaTime btwを掛けることができる)にします。

public static final int GRAVITY = -15; 

public void onJump(){ 
    velocity.y = 300; 
} 

public void update(float deltaTime){ 
    velocity.add(0, GRAVITY * deltaTime); //make the gravity detract from the upwards velocity each frame 
} 
+0

私が知っている限り、それはまさに現時点で起こっていることです。プレーヤーがジャンプすると、yの速度が設定され、プレーヤーが重力を更新するたびに、yの速度からdeltaTimeが減算されます。しかし、助けてくれてありがとう! – Calvin

0

何らかの理由で、今朝問題が解決しました。これは後で起こる可能性があるので緊張しますが、今のところそれは問題ではありません。良いアイデアをありがとう!ずっとこの上で働いた後

EDIT 私はSkypeの呼び出しは、この問題が発生したバックグラウンドで起こって、ない他の時に持っていたときに、私が言うことができます。

関連する問題