vkmEngine 1.0.0
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
92 void heard(uint32_t tick, float tickRate);
93
104 float delayTicks(float tickRate, float snapshotRate) const;
105
115
124 void apply(Scene& scene, float deltaTime, float tickRate, float snapshotRate);
125
137 void hold(EntityId entity, bool held);
138
140 void forget(EntityId entity);
141
142 void clear();
143
144 size_t tracked() const { return m_tracks.size(); }
145
153 double renderTick() const { return m_renderTick; }
154
162 float behindTicks() const {
163 return m_started ? static_cast<float>(static_cast<double>(m_newestTick) - m_renderTick) : 0.0f;
164 }
165
166 private:
168 struct Track {
169 EntityId entity;
170 SampleTrack<HISTORY> history;
171 bool held = false;
172 };
173
175 bool isPosed(uint32_t slot) const { return slot < m_posed.size() && m_posed[slot]; }
176
177 private:
178 std::unordered_map<uint32_t, Track> m_tracks;
179
181 std::vector<bool> m_posed;
182
183 double m_renderTick = 0.0;
184 uint32_t m_newestTick = 0;
185 bool m_started = false;
186
187 double m_seconds = 0.0;
188 double m_heardAt = 0.0;
189 uint32_t m_heardTick = 0;
190 float m_jitter = 0.0f;
191 bool m_heardAny = false;
192};
193
194} // 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.
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:153
float behindTicks() const
How far behind the newest news it is drawing, in ticks.
Definition interpolation.h:162
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