gameloop 和 deltatime 插值 c++
gameloop and deltatime interpolation 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 和插值,
进行平滑的 "world object" 运动。这是使用 deltaTime 和插值的 "advance" 的良好实现吗?还是应该不同?例如:
示例:
obj* myObj = new myObj();
float movement = 2.0f;
myObj->position.x += (movement*deltaTime)*interpolation;
我需要一些有关使用增量时间插值的帮助。
谢谢!
插值将始终用于一种预测功能 - 精灵在未来时间的位置。要回答有关如何使用插值变量的问题,您将需要两个更新函数 - 一个将 delta 作为参数,另一个将插值时间作为参数。请参阅下面有关功能的示例(根据您使用的编码语言实现):
void update_funcA(int delta)
{
sprite.x += (objectSpeedperSecond * delta);
}
void predict_funcB(int interpolation)
{
sprite.x += (objectSpeedperSecond * interpolation);
}
正如您在上面看到的,这两个函数做同样的事情,所以在每次调用中将增量和插值值作为参数传递给一个调用两次就可以了,但在某些情况下,有两个函数会更好,例如在处理重力时 - 也许是物理游戏。您需要了解的是,插值值始终是预期循环时间的一小部分,因此您需要精灵移动该部分以确保平滑移动,而不管帧率如何。
我对这个游戏循环的实现有疑问:
#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 和插值, 进行平滑的 "world object" 运动。这是使用 deltaTime 和插值的 "advance" 的良好实现吗?还是应该不同?例如:
示例:
obj* myObj = new myObj();
float movement = 2.0f;
myObj->position.x += (movement*deltaTime)*interpolation;
我需要一些有关使用增量时间插值的帮助。
谢谢!
插值将始终用于一种预测功能 - 精灵在未来时间的位置。要回答有关如何使用插值变量的问题,您将需要两个更新函数 - 一个将 delta 作为参数,另一个将插值时间作为参数。请参阅下面有关功能的示例(根据您使用的编码语言实现):
void update_funcA(int delta)
{
sprite.x += (objectSpeedperSecond * delta);
}
void predict_funcB(int interpolation)
{
sprite.x += (objectSpeedperSecond * interpolation);
}
正如您在上面看到的,这两个函数做同样的事情,所以在每次调用中将增量和插值值作为参数传递给一个调用两次就可以了,但在某些情况下,有两个函数会更好,例如在处理重力时 - 也许是物理游戏。您需要了解的是,插值值始终是预期循环时间的一小部分,因此您需要精灵移动该部分以确保平滑移动,而不管帧率如何。