小编zer*_*ain的帖子

如何使用deltaTime产生一致的运动

我想创建一个基于速​​度的运动的二维游戏。正如当前的代码一样,我正在使用增量时间来使帧率之间的速度变化保持一致。但是,当在144hz监视器上运行时,跳跃时的跳跃高度每次跳跃都略有不同(最大相差约3个像素,最大高度约为104像素),但是当我切换到60hz时,跳跃高度会立即增加平均约5像素,最大高度约109像素。

我曾尝试过用增量时间对哪些值进行规范化的许多不同变体,但我总是回到最接近我想要的输出的那个值。

static bool grounded = false;  

if (!grounded) {  
    velocity.y += (5000.0f * deltaTime);   //gravity  
}  

if (keys[SPACE_INPUT]) {       //jump command  
    if (grounded) {  
        position.y = 750;  
        velocity.y = -1000.0f;  
        grounded = false;  
    }   
}  

//Used to measure the max height of the jump  
static float maxheight = position.y;  

if (position.y < maxheight) {  
    maxheight = position.y;  
    std::cout << maxheight << std::endl;  
}

//if at the top of the screen  
if (position.y + (velocity.y * deltaTime) < 0) { …
Run Code Online (Sandbox Code Playgroud)

c++ game-physics

2
推荐指数
1
解决办法
57
查看次数

标签 统计

c++ ×1

game-physics ×1