Fix net code
This commit is contained in:
parent
1f10b9c750
commit
a1fb6755c7
5 changed files with 408 additions and 236 deletions
|
|
@ -1,36 +0,0 @@
|
|||
export class LinearExtrapolator {
|
||||
private velocity = 0;
|
||||
private compensationVelocity = 0;
|
||||
private timeSinceSet = 0;
|
||||
|
||||
constructor(private currentValue: number) {}
|
||||
|
||||
public addFrame(value: number, rateOfChange: number) {
|
||||
this.timeSinceSet = 0;
|
||||
const differenceFromCurrent = value - this.currentValue;
|
||||
|
||||
if (Math.abs(differenceFromCurrent) > 200) {
|
||||
this.currentValue = value;
|
||||
this.compensationVelocity = 0;
|
||||
} else {
|
||||
this.compensationVelocity = differenceFromCurrent / (1 / 30);
|
||||
}
|
||||
|
||||
this.velocity = rateOfChange;
|
||||
}
|
||||
|
||||
public getValue(deltaTime: number): number {
|
||||
this.currentValue += deltaTime * this.velocity;
|
||||
|
||||
const compensationTimeLeft = 1 / 30 - this.timeSinceSet;
|
||||
|
||||
if (compensationTimeLeft > 0) {
|
||||
this.currentValue +=
|
||||
Math.min(compensationTimeLeft, deltaTime) * this.compensationVelocity;
|
||||
}
|
||||
|
||||
this.timeSinceSet += deltaTime;
|
||||
|
||||
return this.currentValue;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
import { Circle } from 'shared';
|
||||
import { LinearInterpolator } from './linear-interpolator';
|
||||
import { Vec2Interpolator } from './vec2-interpolator';
|
||||
|
||||
export class CircleInterpolator {
|
||||
private center: Vec2Interpolator;
|
||||
private radius: LinearInterpolator;
|
||||
|
||||
constructor(currentValue: Circle) {
|
||||
this.center = new Vec2Interpolator(currentValue.center);
|
||||
this.radius = new LinearInterpolator(currentValue.radius);
|
||||
}
|
||||
|
||||
public addFrame(value: Circle, rateOfChange: Circle) {
|
||||
this.center.addFrame(value.center, rateOfChange.center);
|
||||
this.radius.addFrame(value.radius, rateOfChange.radius);
|
||||
}
|
||||
|
||||
public getValue(deltaTime: number): Circle {
|
||||
return new Circle(this.center.getValue(deltaTime), this.radius.getValue(deltaTime));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,99 @@
|
|||
import { last, mix } from 'shared';
|
||||
import { serverTimeline } from '../server-timeline';
|
||||
|
||||
interface Frame {
|
||||
time: number;
|
||||
value: number;
|
||||
velocity: number;
|
||||
}
|
||||
|
||||
// How far past the newest snapshot the value may coast on its last known
|
||||
// velocity (network hiccup) before freezing in place.
|
||||
const maxCoastSeconds = 0.06;
|
||||
|
||||
// When fresh snapshots arrive after a coast, they usually disagree with where
|
||||
// the coast ended up; the difference decays away with this time constant
|
||||
// instead of being shown as a jump.
|
||||
const blendSeconds = 0.12;
|
||||
|
||||
// At 25 snapshots per second this spans over a second of state, far more than
|
||||
// rendering ever needs; it only bounds memory while the tab is hidden and
|
||||
// snapshots keep arriving without being consumed.
|
||||
const maxFrames = 32;
|
||||
|
||||
/**
|
||||
* Replays a server-streamed scalar by interpolating between timestamped
|
||||
* snapshots at the shared timeline's render time, which trails the newest
|
||||
* snapshot. The streamed rate of change is only used to coast briefly when
|
||||
* the buffer runs dry.
|
||||
*/
|
||||
export class LinearInterpolator {
|
||||
private frames: Array<Frame> = [];
|
||||
private blendOffset = 0;
|
||||
private lastValue: number;
|
||||
private isCoasting = false;
|
||||
|
||||
constructor(private readonly initialValue: number) {
|
||||
this.lastValue = initialValue;
|
||||
}
|
||||
|
||||
public addFrame(value: number, rateOfChange: number) {
|
||||
const time = serverTimeline.snapshotTime;
|
||||
const newest = last(this.frames);
|
||||
const wasCoasting = this.isCoasting;
|
||||
|
||||
if (newest && time <= newest.time) {
|
||||
this.frames[this.frames.length - 1] = {
|
||||
time: newest.time,
|
||||
value,
|
||||
velocity: rateOfChange,
|
||||
};
|
||||
} else {
|
||||
this.frames.push({ time, value, velocity: rateOfChange });
|
||||
if (this.frames.length > maxFrames) {
|
||||
this.frames.shift();
|
||||
}
|
||||
}
|
||||
|
||||
if (wasCoasting) {
|
||||
// Continue from where the coast left off and let the offset decay,
|
||||
// instead of jumping onto the freshly revealed trajectory.
|
||||
this.blendOffset = this.lastValue - this.sample(serverTimeline.renderTime);
|
||||
}
|
||||
}
|
||||
|
||||
public getValue(deltaTimeInSeconds: number): number {
|
||||
this.blendOffset *= Math.exp(-deltaTimeInSeconds / blendSeconds);
|
||||
return (this.lastValue = this.sample(serverTimeline.renderTime) + this.blendOffset);
|
||||
}
|
||||
|
||||
private sample(time: number): number {
|
||||
if (this.frames.length === 0) {
|
||||
return this.initialValue;
|
||||
}
|
||||
|
||||
while (this.frames.length >= 2 && this.frames[1].time <= time) {
|
||||
this.frames.shift();
|
||||
}
|
||||
|
||||
const [current, next] = this.frames;
|
||||
this.isCoasting = false;
|
||||
|
||||
if (time <= current.time) {
|
||||
return current.value;
|
||||
}
|
||||
|
||||
if (next) {
|
||||
return mix(
|
||||
current.value,
|
||||
next.value,
|
||||
(time - current.time) / (next.time - current.time),
|
||||
);
|
||||
}
|
||||
|
||||
this.isCoasting = true;
|
||||
return (
|
||||
current.value + current.velocity * Math.min(time - current.time, maxCoastSeconds)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
import { LinearInterpolator } from './linear-interpolator';
|
||||
|
||||
export class Vec2Interpolator {
|
||||
private x: LinearInterpolator;
|
||||
private y: LinearInterpolator;
|
||||
|
||||
constructor(currentValue: vec2) {
|
||||
this.x = new LinearInterpolator(currentValue.x);
|
||||
this.y = new LinearInterpolator(currentValue.y);
|
||||
}
|
||||
|
||||
public addFrame(value: vec2, rateOfChange: vec2) {
|
||||
this.x.addFrame(value.x, rateOfChange.x);
|
||||
this.y.addFrame(value.y, rateOfChange.y);
|
||||
}
|
||||
|
||||
public getValue(deltaTime: number): vec2 {
|
||||
return vec2.fromValues(this.x.getValue(deltaTime), this.y.getValue(deltaTime));
|
||||
}
|
||||
}
|
||||
84
frontend/src/scripts/helper/server-timeline.ts
Normal file
84
frontend/src/scripts/helper/server-timeline.ts
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
import { clamp, settings } from 'shared';
|
||||
|
||||
// Convergence rate of the playback cursor towards its target; a deviation
|
||||
// decays with a time constant of 1 / rateGain seconds.
|
||||
const rateGain = 2;
|
||||
|
||||
// Playback speed stays within [0.75, 1.25]× so corrections are invisible.
|
||||
const maxRateAdjustment = 0.25;
|
||||
|
||||
// Beyond this divergence (tab was in the background, server changed) chasing
|
||||
// the target is pointless: jump straight to it.
|
||||
const resyncSeconds = 0.3;
|
||||
|
||||
/**
|
||||
* The playback clock for state streamed from the server.
|
||||
*
|
||||
* Snapshot timestamps estimate the server's clock: the newest received
|
||||
* timestamp plus the time elapsed since it arrived. Rendering happens
|
||||
* settings.interpolationDelaySeconds behind that estimate, so there is
|
||||
* normally a newer snapshot to interpolate towards and network jitter is
|
||||
* absorbed by the buffer instead of being shown.
|
||||
*
|
||||
* The cursor never jumps under normal operation: it runs at a gently adjusted
|
||||
* rate to stay on target and only snaps after a long divergence.
|
||||
*/
|
||||
class ServerTimeline {
|
||||
private cursor?: number;
|
||||
private newestSnapshotTime?: number;
|
||||
private sinceNewestSnapshot = 0;
|
||||
private _snapshotTime = 0;
|
||||
|
||||
/** Timestamp of the update batch currently being applied. */
|
||||
public get snapshotTime(): number {
|
||||
return this._snapshotTime;
|
||||
}
|
||||
|
||||
/** The point on the server's clock that should be rendered this frame. */
|
||||
public get renderTime(): number {
|
||||
return this.cursor ?? 0;
|
||||
}
|
||||
|
||||
public onSnapshot(timestamp: number) {
|
||||
this._snapshotTime = timestamp;
|
||||
if (this.newestSnapshotTime === undefined || timestamp > this.newestSnapshotTime) {
|
||||
this.newestSnapshotTime = timestamp;
|
||||
this.sinceNewestSnapshot = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Must be called with unscaled wall-clock time, even during the end-game
|
||||
// slow motion: the slowdown is already baked into the snapshots.
|
||||
public step(deltaTimeInSeconds: number) {
|
||||
if (this.newestSnapshotTime === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.sinceNewestSnapshot += deltaTimeInSeconds;
|
||||
const target =
|
||||
this.newestSnapshotTime +
|
||||
this.sinceNewestSnapshot -
|
||||
settings.interpolationDelaySeconds;
|
||||
|
||||
if (this.cursor === undefined || Math.abs(target - this.cursor) > resyncSeconds) {
|
||||
this.cursor = target;
|
||||
return;
|
||||
}
|
||||
|
||||
const rate = clamp(
|
||||
1 + (target - this.cursor) * rateGain,
|
||||
1 - maxRateAdjustment,
|
||||
1 + maxRateAdjustment,
|
||||
);
|
||||
this.cursor += deltaTimeInSeconds * rate;
|
||||
}
|
||||
|
||||
public reset() {
|
||||
this.cursor = undefined;
|
||||
this.newestSnapshotTime = undefined;
|
||||
this.sinceNewestSnapshot = 0;
|
||||
this._snapshotTime = 0;
|
||||
}
|
||||
}
|
||||
|
||||
export const serverTimeline = new ServerTimeline();
|
||||
Loading…
Reference in a new issue