游戏中的更新和时间增量

时间:2013-09-21 18:06:14

标签: java updates timedelta

我正在制作Java游戏,我希望我的游戏在任何FPS上运行相同,所以我在每次更新之间使用时间增量。这是播放器的更新方法:

public void update(long timeDelta) {

    //speed is the movement speed of a player on X axis
    //timeDelta is expressed in nano seconds so I'm dividing it with 1000000000 to express it in seconds

    if (Input.keyDown(37))
        this.velocityX -= speed * (timeDelta / 1000000000.0);

    if (Input.keyDown(39))
        this.velocityX += speed * (timeDelta / 1000000000.0);

    if(Input.keyPressed(38)) {
        this.velocityY -= 6;
    }

    velocityY += g * (timeDelta/1000000000.0); //applying gravity
    move(velocityX, velocityY); /*this is method which moves a player 
                                    according to velocityX and velocityY, 
                                    and checking the collision
                                */

    this.velocityX = 0.0;
}

这是我计算timedelta和调用update的方法(这是在Main类中):

lastUpdateTime = System.nanoTime();
while(Main.running) {

    currentTime = System.nanoTime();
    Update.update(currentTime - lastUpdateTime);

    Input.update();

    Render.render();

    measureFPS();

    lastUpdateTime = currentTime;
}

奇怪的是,当我有无限的FPS(和更新号码)时,我的玩家跳了大约10个街区。当FPS增加时,它会跳得更高。如果我限制FPS,它会跳4个街区。 (块:32x32) 我刚刚意识到问题是:

if(Input.keyPressed(38)) {
    this.velocityY -= 6;
}

我向velocityY添加-6,这会使玩家Y按比例增加更新数而不是时间。

但我不知道如何解决这个问题。

0 个答案:

没有答案
相关问题