fix(net): interpolate between snapshots to absorb jitter (P1-6)
netfight eased to the newest snapshot with no buffer, so a jittery client's robot stalled then jumped. Render ~120 ms in the past between the two bracketing snapshots instead. Dev params ?netlag/?interp/ ?netprobe reproduce it; unit test for the interp math + a Playwright lag test (tests/e2e/netlag.spec.js).
This commit is contained in:
@@ -5,8 +5,8 @@ const { expect } = require('@playwright/test');
|
||||
const mode = page => page.locator('body').getAttribute('data-mode');
|
||||
|
||||
// open the game in online mode and log in with name + password through the login card
|
||||
async function login(page, name, pass) {
|
||||
await page.goto('/?online');
|
||||
async function login(page, name, pass, query = '') {
|
||||
await page.goto('/?online' + query);
|
||||
await page.locator('#oLogin').fill(name);
|
||||
await page.locator('#oPass').fill(pass);
|
||||
await page.locator('#oGo').click();
|
||||
|
||||
48
tests/e2e/netlag.spec.js
Normal file
48
tests/e2e/netlag.spec.js
Normal file
@@ -0,0 +1,48 @@
|
||||
// Repeatable lag test: one client gets a seeded snapshot delay+jitter (?netlag). The opponent robot must still
|
||||
// move SMOOTHLY on that client (entity interpolation), i.e. no large per-step jumps - the bug Michi reported.
|
||||
'use strict';
|
||||
const { test, expect } = require('@playwright/test');
|
||||
const { login, toLobby } = require('./helpers');
|
||||
|
||||
test('a jittery client still sees the opponent move smoothly (interpolation buffer)', async ({ browser }) => {
|
||||
const ctxA = await browser.newContext(), ctxB = await browser.newContext();
|
||||
const a = await ctxA.newPage(), b = await ctxB.newPage();
|
||||
const errors = [];
|
||||
for (const p of [a, b]) p.on('pageerror', e => errors.push(e.message));
|
||||
|
||||
// michi gets a repeatable snapshot delay + jitter (80 ms +/- 40 ms, seeded) + the position probe
|
||||
await login(a, 'michi', 'Michi2000!', '&netlag=80,40,7&netprobe');
|
||||
await login(b, 'benni', 'Benni2000!');
|
||||
await toLobby(a); await toLobby(b);
|
||||
|
||||
await a.locator('.prow', { hasText: 'benni' }).locator('.chal').click();
|
||||
await expect(b.locator('#cYes')).toBeVisible();
|
||||
await b.locator('#cYes').click();
|
||||
await expect(a.locator('body')).toHaveAttribute('data-mode', 'fight');
|
||||
await expect(b.locator('body')).toHaveAttribute('data-mode', 'fight');
|
||||
|
||||
await a.waitForTimeout(4200); // countdown
|
||||
await a.evaluate(() => { globalThis.__probe.length = 0; });
|
||||
await b.keyboard.down('w'); // benni drives so his robot moves across michi's screen
|
||||
await a.keyboard.down('a'); // michi turns a bit (keeps both sides active)
|
||||
await a.waitForTimeout(2600);
|
||||
await b.keyboard.up('w'); await a.keyboard.up('a');
|
||||
|
||||
const probe = await a.evaluate(() => globalThis.__probe.slice());
|
||||
// consider only samples while the opponent was actually moving
|
||||
const deltas = [];
|
||||
let moved = 0;
|
||||
for (let i = 1; i < probe.length; i++) {
|
||||
const dx = probe[i].x - probe[i - 1].x, dz = probe[i].z - probe[i - 1].z;
|
||||
const d = Math.hypot(dx, dz);
|
||||
deltas.push(d); moved += d;
|
||||
}
|
||||
expect(probe.length, 'probe collected samples').toBeGreaterThan(30);
|
||||
expect(moved, 'the opponent actually moved (test is meaningful)').toBeGreaterThan(0.8);
|
||||
const maxJump = Math.max(...deltas);
|
||||
// at ~5 m/s over a 60 Hz step a smooth move is ~0.08/step; a snapshot-jump would be several times that.
|
||||
expect(maxJump, 'no large per-step jump despite jitter (smooth interpolation)').toBeLessThan(0.35);
|
||||
|
||||
expect(errors, 'no JavaScript errors').toEqual([]);
|
||||
await ctxA.close(); await ctxB.close();
|
||||
});
|
||||
49
tests/netfight.test.js
Normal file
49
tests/netfight.test.js
Normal file
@@ -0,0 +1,49 @@
|
||||
'use strict';
|
||||
// Entity-interpolation math for the live fight (the jitter buffer that stops the robot from jumping).
|
||||
const { test } = require('node:test');
|
||||
const assert = require('node:assert');
|
||||
const path = require('path');
|
||||
require(path.join(__dirname, '..', 'game', 'protocol.js'));
|
||||
require(path.join(__dirname, '..', 'game', 'netfight.js'));
|
||||
const RC = globalThis.RC;
|
||||
const { pickFrames, lerpRobot } = RC.NetFight;
|
||||
|
||||
test('pickFrames: brackets the render time and gives the fraction between the two snapshots', () => {
|
||||
const buf = [{ at: 0, tick: 1 }, { at: 100, tick: 2 }];
|
||||
const fr = pickFrames(buf, 50);
|
||||
assert.strictEqual(fr.a.tick, 1);
|
||||
assert.strictEqual(fr.b.tick, 2);
|
||||
assert.ok(Math.abs(fr.frac - 0.5) < 1e-9);
|
||||
});
|
||||
|
||||
test('pickFrames: past the newest snapshot holds it (no extrapolation jump)', () => {
|
||||
const buf = [{ at: 0, tick: 1 }, { at: 100, tick: 2 }];
|
||||
const fr = pickFrames(buf, 500);
|
||||
assert.strictEqual(fr.a.tick, 2);
|
||||
assert.strictEqual(fr.b.tick, 2);
|
||||
assert.strictEqual(fr.frac, 1);
|
||||
});
|
||||
|
||||
test('pickFrames: empty buffer -> null', () => {
|
||||
assert.strictEqual(pickFrames([], 0), null);
|
||||
});
|
||||
|
||||
test('lerpRobot: positions interpolate linearly', () => {
|
||||
const r = lerpRobot({ x: 0, z: 0, y: 0, hp: 100 }, { x: 10, z: -4, y: 2, hp: 50 }, 0.5);
|
||||
assert.ok(Math.abs(r.x - 5) < 1e-9);
|
||||
assert.ok(Math.abs(r.z + 2) < 1e-9);
|
||||
assert.ok(Math.abs(r.y - 1) < 1e-9);
|
||||
});
|
||||
|
||||
test('lerpRobot: angles take the shortest path across the +/-PI seam', () => {
|
||||
const r = lerpRobot({ a: 3.0 }, { a: -3.0 }, 0.5);
|
||||
// short way is +0.28 rad across PI, not -6 rad the long way
|
||||
assert.ok(r.a > 3.0 || r.a < -3.0, 'wrapped past PI, not back toward 0');
|
||||
assert.ok(Math.abs(Math.abs(r.a) - Math.PI) < 0.2, 'lands near the seam');
|
||||
});
|
||||
|
||||
test('lerpRobot: discrete fields come from the reached (older) snapshot, not the newer one', () => {
|
||||
const r = lerpRobot({ x: 0, hp: 100, dead: false }, { x: 10, hp: 0, dead: true }, 0.5);
|
||||
assert.strictEqual(r.hp, 100);
|
||||
assert.strictEqual(r.dead, false);
|
||||
});
|
||||
Reference in New Issue
Block a user