我想创建一个基于速度的运动的二维游戏。正如当前的代码一样,我正在使用增量时间来使帧率之间的速度变化保持一致。但是,当在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)