11 constexpr double Pi = 3.1415926535897932384626433832795;
13 double Clamp(
double value,
double minimum,
double maximum)
15 return std::max(minimum, std::min(value, maximum));
20 return a.
x * b.
x + a.
y * b.
y + a.
z * b.
z + a.
w * b.
w;
26 a.
w * b.
x + a.
x * b.
w + a.
y * b.
z - a.
z * b.
y,
27 a.
w * b.
y - a.
x * b.
z + a.
y * b.
w + a.
z * b.
x,
28 a.
w * b.
z + a.
x * b.
y - a.
y * b.
x + a.
z * b.
w,
29 a.
w * b.
w - a.
x * b.
x - a.
y * b.
y - a.
z * b.
z
35 const double halfYaw = yaw * 0.5;
36 const double halfPitch = pitch * 0.5;
37 const double halfRoll = roll * 0.5;
38 const double cy = std::cos(halfYaw);
39 const double sy = std::sin(halfYaw);
40 const double cp = std::cos(halfPitch);
41 const double sp = std::sin(halfPitch);
42 const double cr = std::cos(halfRoll);
43 const double sr = std::sin(halfRoll);
46 result.
x = cy * sp * cr + sy * cp * sr;
47 result.
y = sy * cp * cr - cy * sp * sr;
48 result.
z = cy * cp * sr - sy * sp * cr;
49 result.
w = cy * cp * cr + sy * sp * sr;
57 const double t2 = amount * amount;
58 const double t3 = t2 * amount;
59 const double h00 = 2.0 * t3 - 3.0 * t2 + 1.0;
60 const double h10 = t3 - 2.0 * t2 + amount;
61 const double h01 = -2.0 * t3 + 3.0 * t2;
62 const double h11 = t3 - t2;
70 if (duration <= std::numeric_limits<double>::epsilon())
72 const double t2 = amount * amount;
73 const double dh00 = (6.0 * t2 - 6.0 * amount) / duration;
74 const double dh10 = 3.0 * t2 - 4.0 * amount + 1.0;
75 const double dh01 = (-6.0 * t2 + 6.0 * amount) / duration;
76 const double dh11 = 3.0 * t2 - 2.0 * amount;
98 return std::isfinite(value.
x) && std::isfinite(value.
y) && std::isfinite(value.
z);
103 return std::isfinite(value.
x) && std::isfinite(value.
y) &&
104 std::isfinite(value.
z) && std::isfinite(value.
w);
109 return std::sqrt(value.
x * value.
x + value.
y * value.
y + value.
z * value.
z);
114 return std::sqrt(value.
x * value.
x + value.
y * value.
y + value.
z * value.
z + value.
w * value.
w);
122 if (!std::isfinite(length) || length < 0.5 || length > 2.0)
135 return reference != 0;
144 amount =
Clamp(amount, 0.0, 1.0);
146 double dot = Dot(a, b);
159 result.
x = a.
x + amount * (b.
x - a.
x);
160 result.
y = a.
y + amount * (b.
y - a.
y);
161 result.
z = a.
z + amount * (b.
z - a.
z);
162 result.
w = a.
w + amount * (b.
w - a.
w);
167 const double theta = std::acos(
Clamp(dot, -1.0, 1.0));
168 const double sinTheta = std::sin(theta);
169 const double firstWeight = std::sin((1.0 - amount) * theta) / sinTheta;
170 const double secondWeight = std::sin(amount * theta) / sinTheta;
171 result.
x = a.
x * firstWeight + b.
x * secondWeight;
172 result.
y = a.
y * firstWeight + b.
y * secondWeight;
173 result.
z = a.
z * firstWeight + b.
z * secondWeight;
174 result.
w = a.
w * firstWeight + b.
w * secondWeight;
181 if (seconds <= 0.0 || !
IsFinite(angularVelocity))
184 FromYawPitchRoll(angularVelocity.
y * seconds, angularVelocity.
x * seconds, angularVelocity.
z * seconds));
192 const std::uint16_t delta =
static_cast<std::uint16_t
>(incoming - previous);
193 return delta >= 1 && delta <= 32767;
198 if (incoming >= 255 || previous >= 255)
200 const unsigned delta = (
static_cast<unsigned>(incoming) + 255U -
static_cast<unsigned>(previous)) % 255U;
201 return delta >= 1 && delta <= 127;
206 return static_cast<std::uint16_t
>(incoming - previous);
211 return actualBytes == expectedBytes;
216 static const auto epoch = std::chrono::steady_clock::now();
217 return std::chrono::duration<double>(std::chrono::steady_clock::now() - epoch).count();
221 double elapsedSeconds,
bool hasAuthoritativePresentation)
223 if (hasAuthoritativePresentation || !std::isfinite(elapsedSeconds) || elapsedSeconds <= 0.0)
224 return presentationPosition;
225 return presentationPosition + velocity * elapsedSeconds;
299 if (!std::isfinite(rawIntervalSeconds) || rawIntervalSeconds < 0.005)
301 const double interval =
Clamp(rawIntervalSeconds, 0.005, 10.0);
310 constexpr double Alpha = 0.15;
313 const double deviation = std::abs(interval - previous);
319 if (!std::isfinite(sourceIntervalSeconds) || sourceIntervalSeconds <= 0.0 ||
328 constexpr double Alpha = 0.15;
341 if (!std::isfinite(receiptSeconds) || receiptSeconds < 0.0)
363 std::uint16_t deliveryDelta = 0;
375 if (deliveryDelta == 0)
381 if (deliveryDelta > maximumForward)
406 const std::uint16_t sourceDeltaMs = hadSourceObservation ?
408 const std::uint16_t revisionDelta = hadSourceObservation ?
410 const std::uint32_t maximumSourceDeltaMs =
static_cast<std::uint32_t
>(
412 const bool plausibleSourceDelta = sourceDeltaMs >= 1 && sourceDeltaMs <= maximumSourceDeltaMs;
414 const bool revisionAdvanced = hadSourceObservation &&
IsNewerUShort(
416 const bool revisionMovedBackward = usesSourceRevision && hadSourceObservation && revisionDelta > 32767;
426 bool sourceActivated =
false;
427 if (!
sourceTimed_ && hadSourceObservation && plausibleSourceDelta &&
428 (usesSourceRevision ? revisionAdvanced :
true) && !legacyZeroTransition)
431 sourceActivated =
true;
437 if (usesSourceRevision && hadSourceObservation && (sourceWasTimed || sourceActivated))
439 if (revisionDelta == 0)
441 else if (revisionAdvanced && deliveryDelta == 1 && revisionDelta > 1)
447 const bool impossibleActiveDelta =
sourceTimed_ && !sourceActivated &&
448 (!plausibleSourceDelta || revisionMovedBackward ||
449 (usesSourceRevision && revisionAdvanced && sourceDeltaMs == 0));
450 bool reanchorSource = sourceActivated || longSilence || impossibleActiveDelta;
452 double timeline = receiptSeconds;
455 if (!reanchorSource &&
count_ > 0)
458 static_cast<double>(sourceDeltaMs) / 1000.0;
461 const double phaseCorrection =
Clamp(
471 timeline = receiptSeconds;
472 if (plausibleSourceDelta)
481 const double burstSegment =
Clamp(estimatedInterval * 0.5,
487 bool teleport =
false;
491 const double predictionTime = std::max(0.0, timeline - newest.
timelineSeconds);
507 timeline = receiptSeconds;
558 return Clamp(cadence * 2.0 +
580 amount =
Clamp(amount, 0.0, 1.0);
585 tangentTravel <= chordLength * 8.0 + 1.0;
588 result.
position = HermitePosition(first, second, amount, duration);
624 const double easeStart = cap * 0.75;
626 if (effectiveSeconds > easeStart)
629 effectiveSeconds = easeStart + timeConstant *
630 (1.0 - std::exp(-(effectiveSeconds - easeStart) / timeConstant));
646 for (std::size_t index = 1; index <
count_; ++index)
653 const double amount = duration > 0.0 ? (presentationSeconds - first.
timelineSeconds) / duration : 1.0;
664 if (elapsedSeconds <= 0.0)
667 const double omega = std::log(2.0) / halfLife;
668 const double decay = std::exp(-omega * elapsedSeconds);
669 auto advance = [omega, decay, elapsedSeconds](
double& offset,
double& velocity)
671 const double combined = velocity + omega * offset;
672 offset = (offset + combined * elapsedSeconds) * decay;
673 velocity = (velocity - omega * combined * elapsedSeconds) * decay;
682 if (
count_ == 0 || !std::isfinite(nowSeconds))
716 assert(logicalIndex <
count_);
738 vehicles_[slot].Reset(identity, reference, parentSlot, identityChanged);
744 weapons_[slot].Reset(identity, -127, -1,
true);
750 vehicles_[slot].Reset(0, -127, -1,
false);
756 weapons_[slot].Reset(0, -127, -1,
false);
791 int closestSlot = -1;
792 double closestDistanceSquared = std::numeric_limits<double>::max();
798 const Vec3 observer = snapshot.
reference < 0 ? worldObserverPosition : dockObserverPosition;
800 const double distanceSquared = difference.
x * difference.
x +
801 difference.
y * difference.
y + difference.
z * difference.
z;
802 if (distanceSquared < closestDistanceSquared)
804 closestDistanceSquared = distanceSquared;
805 closestSlot =
static_cast<int>(slot);
constexpr auto Clamp(T1 val, T2 min, T3 max)
double InterpolationDelay() const
void Configure(SequenceMode sequenceMode, const ChannelConfig &config)
std::uint32_t packetWindowCount_
double sourceIntervalSeconds_
PresentationState Extrapolate(const MotionSnapshot &snapshot, double seconds) const
std::array< MotionSnapshot, SnapshotCapacity > snapshots_
PresentationState lastPresentation_
Acceptance Push(MotionSnapshot snapshot, double receiptSeconds)
PresentationState Evaluate(double presentationSeconds) const
SequenceMode sequenceMode_
ChannelMetrics GetMetrics(double nowSeconds) const
const MotionSnapshot & SnapshotAt(std::size_t logicalIndex) const
void Reset(int identity, int reference, int parentSlot, bool requireLowBootstrapSequence)
double sourcePhaseSeconds_
void UpdateSourceCadence(double sourceIntervalSeconds)
PresentationState Sample(double nowSeconds, double elapsedSeconds)
double lastRawReceiptSeconds_
bool hasSourceObservation_
std::size_t PhysicalIndex(std::size_t logicalIndex) const
std::uint16_t lastObservedSourceRevision_
PresentationState Interpolate(const MotionSnapshot &first, const MotionSnapshot &second, double amount) const
bool sourceIntervalInitialized_
void UpdateCadence(double rawIntervalSeconds)
std::uint16_t lastObservedSampleTimeMs_
std::uint16_t ForwardDelta(std::uint16_t incoming) const
bool lastPresentationWasExtrapolated_
bool requireLowBootstrapSequence_
double ExtrapolationCap() const
bool intervalInitialized_
double packetWindowStartSeconds_
void RecordIncoming(double receiptSeconds)
void AdvanceCorrection(double elapsedSeconds)
std::size_t Depth() const
bool HasVehicleSamples(std::size_t slot) const
bool HasWeaponSamples(std::size_t slot) const
void ResetWeapon(std::size_t slot, int identity)
ChannelMetrics VehicleMetrics(std::size_t slot, double nowSeconds) const
void ResetVehicle(std::size_t slot, int identity, int reference, int parentSlot, bool identityChanged)
void RemoveVehicle(std::size_t slot)
Acceptance PushVehicle(std::size_t slot, MotionSnapshot snapshot, double receiptSeconds)
std::array< SnapshotChannel, EntityCapacity > vehicles_
void RemoveWeapon(std::size_t slot)
int ClosestWeaponSlot(const Vec3 &worldObserverPosition, const Vec3 &dockObserverPosition) const
PresentationState SampleVehicle(std::size_t slot, double nowSeconds, double elapsedSeconds)
std::array< SnapshotChannel, EntityCapacity > weapons_
Acceptance PushWeapon(std::size_t slot, MotionSnapshot snapshot, double receiptSeconds)
PresentationState SampleWeapon(std::size_t slot, double nowSeconds, double elapsedSeconds)
ChannelMetrics WeaponMetrics(std::size_t slot, double nowSeconds) const
float Dot(const Vector3 &a, const Vector3 &b)
Quaternion Slerp(const Quaternion &aValue, const Quaternion &bValue, double amount)
double MonotonicSeconds()
Quaternion IntegrateAngularVelocity(const Quaternion &orientation, const Vec3 &angularVelocity, double seconds)
bool HasExactPayloadLength(std::size_t actualBytes, std::size_t expectedBytes)
double QuaternionLength(const Quaternion &value)
double Length(const Vec3 &value)
constexpr std::size_t SnapshotCapacity
constexpr std::size_t EntityCapacity
bool IsNewerModulo255(std::uint8_t incoming, std::uint8_t previous)
std::uint16_t SourceTimeDeltaMilliseconds(std::uint16_t incoming, std::uint16_t previous)
bool IsNewerUShort(std::uint16_t incoming, std::uint16_t previous)
Vec3 WeaponFrameEndpoint(const Vec3 &presentationPosition, const Vec3 &velocity, double elapsedSeconds, bool hasAuthoritativePresentation)
SnapshotSystem gSnapshotSystem
bool ShouldBufferVehicleReference(int reference)
bool NormalizeQuaternion(Quaternion &value)
bool IsFinite(const Vec3 &value)
double sourcePhaseCorrectionAlpha
double teleportBaseDistanceKm
double maximumInterpolationDelaySeconds
double maximumSourcePhaseCorrectionPerPacketSeconds
double maximumSparseInterpolationDelaySeconds
double frequentIntervalThresholdSeconds
double jitterMarginMultiplier
double minimumTimelineSegmentSeconds
double sourceTimelineSilenceResetSeconds
double sparseExtrapolationCapSeconds
double maximumSourceDeltaSeconds
double minimumInterpolationDelaySeconds
double teleportVelocityIntervalMultiplier
double frequentExtrapolationCapSeconds
double maximumBurstTimelineSegmentSeconds
double sparseInterpolationSafetyMarginSeconds
std::uint16_t maximumBootstrapSequence
double correctionHalfLifeSeconds
double newestSnapshotAgeSeconds
std::uint16_t lastDeliverySequenceDelta
std::uint16_t lastSampleTimeMs
std::uint32_t coalescedSourceRevisions
double incomingPacketsPerSecond
std::uint16_t lastAcceptedSequence
std::uint32_t duplicateCount
double interpolationDelaySeconds
std::uint16_t lastSourceRevision
std::uint32_t sequenceGaps
double expectedIntervalSeconds
std::uint32_t invalidCount
std::uint32_t duplicateSourceRevisionCount
std::uint16_t lastSourceDeltaMs
double extrapolationSeconds
std::uint32_t outOfOrderCount
double measuredIntervalSeconds
std::size_t snapshotDepth
double correctionMagnitudeKm
std::uint16_t sourceRevision
double expectedIntervalSeconds
std::uint16_t sampleTimeMs
bool hasPositionDerivative
double extrapolationSeconds
Vec3 & operator+=(const Vec3 &rhs)
Vec3 operator-(const Vec3 &rhs) const
Vec3 operator+(const Vec3 &rhs) const
Vec3 operator*(double scalar) const
Vec3 operator/(double scalar) const