gameloop和插值的DeltaTime C++
问题描述:
我有一个疑问,这个gameloop实现:gameloop和插值的DeltaTime C++
#include <chrono>
#include <iostream>
using namespace std::chrono_literals;
// we use a fixed timestep of 1/(60 fps) = 16 milliseconds
constexpr std::chrono::nanoseconds timestep(16ms);
struct game_state {
// this contains the state of your game, such as positions and velocities
};
bool handle_events() {
// poll for events
return false; // true if the user wants to quit the game
}
void update(game_state *) {
// update game logic here
std::cout << "Update\n";
}
void render(game_state const &) {
// render stuff here
//std::cout << "Render\n";
}
game_state interpolate(game_state const & current, game_state const & previous, float alpha) {
game_state interpolated_state;
// interpolate between previous and current by alpha here
return interpolated_state;
}
int main() {
using clock = std::chrono::high_resolution_clock;
std::chrono::nanoseconds lag(0ns);
auto time_start = clock::now();
bool quit_game = false;
game_state current_state;
game_state previous_state;
while(!quit_game) {
auto delta_time = clock::now() - time_start;
time_start = clock::now();
lag += std::chrono::duration_cast<std::chrono::nanoseconds>(delta_time);
quit_game = handle_events();
// update game logic as lag permits
while(lag >= timestep) {
lag -= timestep;
previous_state = current_state;
update(¤t_state); // update at a fixed rate each time
}
// calculate how close or far we are from the next timestep
auto alpha = (float) lag.count()/timestep.count();
auto interpolated_state = interpolate(current_state, previous_state, alpha);
render(interpolated_state);
}
}
我需要知道我应该如何实现的DeltaTime和插值, 来确保平稳“世界对象”运动。这是一个很好的实现使用deltaTime和插值的“advance”吗?还是应该有所不同?例如:
例子:
obj* myObj = new myObj();
float movement = 2.0f;
myObj->position.x += (movement*deltaTime)*interpolation;
我需要关于使用插值的的DeltaTime的一些帮助。
谢谢!
答
插值将始终用于一种预测函数 - 精灵将在未来的时间。要回答您如何使用插值变量的问题,您需要两个更新函数 - 一个将delta作为参数,另一个采用插值时间。见下面的示例中的功能(实现按编码语言你正在使用):
void update_funcA(int delta)
{
sprite.x += (objectSpeedperSecond * delta);
}
void predict_funcB(int interpolation)
{
sprite.x += (objectSpeedperSecond * interpolation);
}
正如你可以看到上面这两个函数做同样的事情,所以有一个与作为参数传递的增量和插值来了两次每个呼叫都会做,但在某些情况下,有两个funcs会更好,例如也许在处理重力 - 物理游戏时。你需要了解的是内插值总是预期循环时间的一小部分,因此你将需要你的精灵来移动这个分数以确保平滑移动,而不管帧速率如何。