vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
interpolation.h
1#pragma once
2
3#include <cstdint>
4#include <unordered_map>
5#include <vector>
6
7#include <glm/glm.hpp>
8#include <glm/gtc/quaternion.hpp>
9
10#include "ecs/entity.h"
11#include "net/prediction/sample_track.h"
12
13namespace Vkm::Engine {
14
15class Scene;
16
25class NetInterpolation {
26 public:
34 static constexpr float SNAPSHOTS_BEHIND = 2.0f;
35
42 static constexpr size_t HISTORY = 8;
43
51 static constexpr float MAX_SNAPSHOTS_BEHIND = static_cast<float>(HISTORY) / 2.0f;
52
53 static_assert(
55 "the least delay has to fit the samples kept"
56 );
57
58 public:
59 NetInterpolation() = default;
60 ~NetInterpolation() = default;
61
62 NetInterpolation(const NetInterpolation& other) = delete;
63 NetInterpolation& operator=(const NetInterpolation& other) = delete;
64
65 NetInterpolation(NetInterpolation && other) = delete;
66 NetInterpolation& operator=(NetInterpolation && other) = delete;
67
68 public:
80 void record(EntityId entity, uint32_t tick, const glm::vec3& position, const glm::quat& rotation);
81
93 void heard(uint32_t tick, float tickRate);
94
105 float delayTicks(float tickRate, float snapshotRate) const;
106
116
125 void apply(Scene& scene, float deltaTime, float tickRate, float snapshotRate);
126
138 void hold(EntityId entity, bool held);
139
141 void forget(EntityId entity);
142
143 void clear();
144
145 size_t tracked() const { return m_tracks.size(); }
146
154 double renderTick() const { return m_renderTick; }
155
157 bool started() const { return m_started; }
158
166 float behindTicks() const {
167 return m_started ? static_cast<float>(static_cast<double>(m_newestTick) - m_renderTick) : 0.0f;
168 }
169
170 private:
172 struct Track {
173 EntityId entity;
174 SampleTrack<HISTORY> history;
175 bool held = false;
176 };
177
179 bool isPosed(uint32_t slot) const { return slot < m_posed.size() && m_posed[slot]; }
180
182 void heardOf(uint32_t tick);
183
184 private:
185 std::unordered_map<uint32_t, Track> m_tracks;
186
188 std::vector<bool> m_posed;
189
190 double m_renderTick = 0.0;
191 uint32_t m_newestTick = 0;
192 bool m_started = false;
193
194 double m_seconds = 0.0;
195 double m_heardAt = 0.0;
196 uint32_t m_heardTick = 0;
197 float m_jitter = 0.0f;
198 bool m_heardAny = false;
199};
200
201} // namespace Vkm::Engine
void record(EntityId entity, uint32_t tick, const glm::vec3 &position, const glm::quat &rotation)
Record where the authority says entity is, as of tick.
bool started() const
Whether a snapshot has been heard, so the render clock runs.
Definition interpolation.h:157
static constexpr float SNAPSHOTS_BEHIND
The least that is drawn behind the newest news, in snapshots.
Definition interpolation.h:34
static constexpr float MAX_SNAPSHOTS_BEHIND
The most that is drawn behind the newest news, in snapshots, however badly the link jitters.
Definition interpolation.h:51
static constexpr size_t HISTORY
Samples kept per entity.
Definition interpolation.h:42
void restoreConfirmed(Scene &scene)
Put the confirmed transform back, for the entities that have one.
double renderTick() const
Where the clock that decides what is drawn currently stands, in ticks.
Definition interpolation.h:154
float behindTicks() const
How far behind the newest news it is drawing, in ticks.
Definition interpolation.h:166
void hold(EntityId entity, bool held)
Stop drawing entity late, without forgetting where it was.
void heard(uint32_t tick, float tickRate)
Note that a snapshot of tick has arrived, now.
float delayTicks(float tickRate, float snapshotRate) const
How far behind the newest news to draw, in ticks.
void forget(EntityId entity)
Stop tracking entity, for one that left the world.
void apply(Scene &scene, float deltaTime, float tickRate, float snapshotRate)
Move every tracked entity to where it was, a moment ago.
Central registry managing entities and an open set of component types.
Definition scene.h:29
Names one entity of one Scene, for as long as that slot holds it.
Definition entity.h:23
A short history of where one entity was, kept by tick.
Definition sample_track.h:19