Add M10.7a tick clock and remote-marker interpolation
Foundation / checks (deploy) (push) Successful in 6s
Foundation / checks (ecs) (push) Successful in 1m59s
Foundation / checks (client) (push) Successful in 12m44s
Foundation / checks (network) (push) Successful in 12m45s
Foundation / checks (tsan) (push) Successful in 8m5s
Foundation / checks (checks) (push) Successful in 32m4s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 21m38s

ADR-0095 (presentation only): a drift-corrected monotonic tick clock that
never runs more than one tick past the newest observation; remote probe
markers shown 2-6 ticks behind (adaptive to snapshot gaps), interpolated,
extrapolated for at most 2 ticks and then marked stale; appearance and
removal at their snapshot ticks; organisms never extrapolated; sub-tick
own-marker prediction.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-06 17:16:20 -04:00
co-authored by Claude Opus 5.5
parent 61d899de3c
commit 90cdb61e30
12 changed files with 539 additions and 5 deletions
+147
View File
@@ -0,0 +1,147 @@
#include "evo/client/interpolation.hpp"
#include <algorithm>
#include <cmath>
namespace evo::client {
namespace {
double ms(TickClock::Clock::duration d) {
return std::chrono::duration<double, std::milli>(d).count();
}
// Held-vector motion in micrometres for a fractional number of ticks (presentation only).
double motion_um(std::int16_t component, double ticks) {
return static_cast<double>(component) *
static_cast<double>(network::motion::speed_um_per_tick) / 1000.0 * ticks;
}
} // namespace
void TickClock::reset() noexcept {
base_tick_.reset();
newest_ = 0;
returned_ = 0;
}
double TickClock::raw(double base, Clock::time_point now) const {
return base + (ms(now - base_time_) / limits_.tick_ms);
}
void TickClock::observe(std::uint64_t tick, Clock::time_point now) {
newest_ = std::max(newest_, tick);
const auto sample = static_cast<double>(tick);
if (!base_tick_) {
base_tick_ = sample;
base_time_ = now;
return;
}
const auto estimate = raw(*base_tick_, now);
const auto error = sample - estimate;
if (error > limits_.snap_ticks) {
base_tick_ = sample; // Far behind (a pause or a reconnect): catch up at once.
++snaps_;
} else {
base_tick_ = estimate + (error * limits_.gain); // Slew; at() keeps it monotonic.
}
base_time_ = now;
}
std::optional<double> TickClock::at(Clock::time_point now) {
if (!base_tick_)
return std::nullopt;
const auto bounded =
std::min(raw(*base_tick_, now), static_cast<double>(newest_) + limits_.max_ahead);
returned_ = std::max(returned_, bounded);
return returned_;
}
void MarkerInterpolator::reset() {
snapshots_.clear();
gaps_.clear();
delay_ = target_ = limits_.min_delay;
}
void MarkerInterpolator::push(std::uint64_t tick, std::span<const RemoteMarker> markers) {
if (!snapshots_.empty() && tick <= snapshots_.back().tick) {
++stats_.ignored;
return;
}
++stats_.snapshots;
if (!snapshots_.empty()) {
gaps_.push_back(tick - snapshots_.back().tick);
while (gaps_.size() > limits_.gap_window)
gaps_.pop_front();
// Cover the 90th-percentile gap between received snapshots, plus the clock's lead.
std::vector<std::uint64_t> sorted(gaps_.begin(), gaps_.end());
std::ranges::sort(sorted);
const auto p90 = sorted.at((sorted.size() - 1) * 9 / 10);
target_ = std::clamp(static_cast<double>(p90) + 1, limits_.min_delay, limits_.max_delay);
}
Snapshot s{tick, {markers.begin(), markers.end()}};
std::ranges::sort(s.markers, {}, &RemoteMarker::id);
snapshots_.push_back(std::move(s));
while (snapshots_.size() > limits_.max_snapshots)
snapshots_.pop_front();
}
void MarkerInterpolator::advance(double elapsed_ms) {
if (elapsed_ms <= 0)
return;
const auto seconds = elapsed_ms / 1000;
if (delay_ < target_)
delay_ = std::min(target_, delay_ + (limits_.raise_per_second * seconds));
else
delay_ = std::max(target_, delay_ - (limits_.lower_per_second * seconds));
}
std::vector<PresentedMarker> MarkerInterpolator::at(double render_tick) {
std::vector<PresentedMarker> out;
if (snapshots_.empty())
return out;
const auto& newest = snapshots_.back();
const auto show = [&](const Snapshot& s) {
for (const auto& m : s.markers)
out.push_back({m.id, static_cast<double>(m.state.x), static_cast<double>(m.state.y)});
};
if (render_tick >= static_cast<double>(newest.tick)) {
// Beyond the newest snapshot: continue the held vector briefly, then freeze (stale).
const auto beyond = render_tick - static_cast<double>(newest.tick);
const auto ticks = std::min(beyond, limits_.max_extrapolation);
const bool stale = beyond > limits_.max_extrapolation;
for (const auto& m : newest.markers)
out.push_back({m.id,
static_cast<double>(m.state.x) + motion_um(m.state.vector.x, ticks),
static_cast<double>(m.state.y) + motion_um(m.state.vector.y, ticks),
ticks > 0, stale});
stats_.extrapolated_frames += ticks > 0 ? 1 : 0;
stats_.stale_frames += stale ? 1 : 0;
return out;
}
if (render_tick <= static_cast<double>(snapshots_.front().tick)) {
show(snapshots_.front()); // Not enough history yet: the oldest as it was.
return out;
}
// a.tick <= render_tick < b.tick.
const auto b = std::ranges::find_if(
snapshots_, [&](const Snapshot& s) { return static_cast<double>(s.tick) > render_tick; });
const auto& a = *std::prev(b);
if (b->tick - a.tick > limits_.max_gap) {
++stats_.gap_snaps; // Too far apart to interpolate: hold the older until the newer.
show(a);
return out;
}
const auto t =
(render_tick - static_cast<double>(a.tick)) / static_cast<double>(b->tick - a.tick);
for (const auto& m : a.markers) {
const auto next = std::ranges::lower_bound(b->markers, m.id, {}, &RemoteMarker::id);
if (next == b->markers.end() || next->id != m.id) {
// Removed by b: shown where it was until b's tick, never moved toward a removal.
out.push_back({m.id, static_cast<double>(m.state.x), static_cast<double>(m.state.y)});
continue;
}
const auto lerp = [t](std::int64_t from, std::int64_t to) {
return static_cast<double>(from) +
((static_cast<double>(to) - static_cast<double>(from)) * t);
};
out.push_back({m.id, lerp(m.state.x, next->state.x), lerp(m.state.y, next->state.y)});
}
return out; // Markers new in b appear from b's tick.
}
} // namespace evo::client
@@ -81,6 +81,19 @@ std::optional<network::motion::State> MotionPredictor::predict(std::uint64_t tic
return state;
}
std::optional<MotionPredictor::Point> MotionPredictor::predict_between(std::uint64_t tick,
double fraction) {
const auto from = predict(tick);
if (!from)
return std::nullopt;
const auto to = replay(tick + 1);
if (!to)
return std::nullopt;
const auto f = std::clamp(fraction, 0.0, 1.0);
return Point{static_cast<double>(from->x) + (static_cast<double>(to->x - from->x) * f),
static_cast<double>(from->y) + (static_cast<double>(to->y - from->y) * f)};
}
std::optional<network::motion::State> MotionPredictor::replay(std::uint64_t tick) {
if (!authority_)
return std::nullopt;