如果您不熟悉 Gaffer on Games 文章“修复您的时间步长”,您可以在这里找到它:https : //gafferongames.com/post/fix_your_timestep/
我正在构建一个游戏引擎,为了让 std::chrono 更加舒适,我一直在尝试使用 std::chrono 实现一个固定的时间步长......几天了,但我不能似乎把我的头环绕在它周围。这是我正在努力的伪代码:
double t = 0.0;
double dt = 0.01;
double currentTime = hires_time_in_seconds();
double accumulator = 0.0;
State previous;
State current;
while ( !quit )
{
double newTime = time();
double frameTime = newTime - currentTime;
if ( frameTime > 0.25 )
frameTime = 0.25;
currentTime = newTime;
accumulator += frameTime;
while ( accumulator >= dt )
{
previousState = currentState;
integrate( currentState, t, dt );
t += …Run Code Online (Sandbox Code Playgroud) 我有一个来自我正在使用的库中的函数,它需要一个 double 作为参数。它需要传递一个纳秒类型的偏移量加上 sytem_clock::now()。到目前为止我有这个代码:
system_clock::time_point now = std::chrono::system_clock::now();
auto timepointoffset = (now + desiredOffset);
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
编辑:所以我需要补充一点,问题是我需要在不存在丢失数据风险的情况下进行操作。我有这个代码:
system_clock::time_point now = std::chrono::system_clock::now();
auto timepointoffset = std::chrono::time_point_cast<std::chrono::nanoseconds>(now + desiredOffset);
double value = timepointoffset.time_since_epoch().count();
Run Code Online (Sandbox Code Playgroud)
问题是编译器说可能会丢失数据。
我有以下日期时间结构:
struct DateTime
{
std::uint16_t year;
std::uint8_t month;
std::uint8_t day;
std::uint8_t hour;
std::uint8_t minute;
std::uint8_t second;
std::uint16_t milisecond;
};
Run Code Online (Sandbox Code Playgroud)
我的疑问是关于LessThan和GreaterThan方法。为了避免一堆ifs和elses ,我已经实现如下,但我可能没有涵盖所有可能的情况:
bool GreaterThan(const DateTime& datetime)
{
bool greater{true};
// When found a different value for the most significant value, the evaluation is interrupted
if ((year <= datetime.year) && (month <= datetime.month || year < datetime.year) &&
(day <= datetime.day || month < datetime.month) && (hour <= datetime.hour || day < datetime.day) &&
(minute …Run Code Online (Sandbox Code Playgroud) c++ ×3
c++-chrono ×2
c++11 ×2
datetime ×1
game-engine ×1
game-loop ×1
struct ×1
time ×1
timestamp ×1