Toast Engine
Toggle main menu visibility
Loading...
Searching...
No Matches
accumulator.hpp
1
7
8
#pragma once
9
#include "
physics_settings.hpp
"
10
11
#include <algorithm>
12
#include <chrono>
13
#include <cmath>
14
#include <cstdint>
15
#include <
toast/log.hpp
>
16
#include <
toast/time.hpp
>
17
#include <tracy/Tracy.hpp>
18
19
namespace
physics {
20
21
struct
StepResult
{
22
unsigned
steps = 0;
23
double
alpha = 0.0;
24
bool
dropped_time =
false
;
25
27
bool
time_budget_reached
=
false
;
28
};
29
30
class
Accumulator
{
31
public
:
32
[[nodiscard]]
33
static
auto
fixedDelta() ->
double
{
34
return
tunables().fixedDelta();
35
}
36
37
[[nodiscard]]
38
static
auto
maxSteps() ->
unsigned
{
39
return
std::max(tunables().max_substeps, 1u);
40
}
41
42
template
<
typename
Fn>
43
auto
tick(
double
dt, Fn&& fn) ->
StepResult
{
44
ZoneScopedN(
"physics::Accumulator"
);
45
46
m_accumulator += dt;
47
48
StepResult
result;
49
const
double
fixed_delta = fixedDelta();
50
const
unsigned
max_steps = maxSteps();
51
const
auto
burst_start = std::chrono::steady_clock::now();
52
53
while
(m_accumulator >= fixed_delta && result.steps < max_steps) {
54
if
(result.steps > 0) {
55
const
auto
elapsed = std::chrono::duration<double>(std::chrono::steady_clock::now() - burst_start).count();
56
if
(elapsed >= tunables().max_burst_seconds) {
57
result.
time_budget_reached
=
true
;
58
break
;
59
}
60
}
61
62
fn();
63
64
m_accumulator -= fixed_delta;
65
++result.steps;
66
}
67
68
if
(m_accumulator >= fixed_delta) {
69
TOAST_WARN
(
"Physics"
,
"Accumulator overflow: {} seconds of physics time dropped"
, m_accumulator);
70
m_accumulator = std::fmod(m_accumulator, fixed_delta);
71
result.dropped_time =
true
;
72
}
73
74
result.alpha = m_accumulator / fixed_delta;
75
return
result;
76
}
77
78
[[nodiscard]]
79
auto
alpha()
const
noexcept
->
double
{
80
return
m_accumulator / fixedDelta();
81
}
82
83
[[nodiscard]]
84
auto
value()
const
noexcept
->
double
{
85
return
m_accumulator;
86
}
87
88
void
reset()
noexcept
{ m_accumulator = 0.0; }
89
90
private
:
91
double
m_accumulator = 0.0;
92
};
93
94
}
physics::Accumulator
Definition
accumulator.hpp:30
log.hpp
Public logging interface for the engine.
TOAST_WARN
#define TOAST_WARN(sink,...)
Indicates possible code missuse.
Definition
log.hpp:94
physics_settings.hpp
physics::StepResult
Definition
accumulator.hpp:21
physics::StepResult::time_budget_reached
bool time_budget_reached
The loop stopped early on the real-time budget, so a slow tick shrinks its own catch-up allowance.
Definition
accumulator.hpp:27
time.hpp
Controls the time of the application.
engine
src
toast
physics
accumulator.hpp
Generated by
1.17.0