瀏覽器 8 Ball Pool 遊戲的球為何在滾動途中加速(以及另外 3 個錯誤)
Why our pool balls sped up mid-roll (and 3 other bugs from a browser pool game)
3D 8 Ball Pool 瀏覽器遊戲修正了令物理效果失真的四個錯誤,涉及物理步進、球桌口袋、CPU 選球時卡頓,以及首次擊球時的着色器編譯。測試中,修正口袋後出界球數由 356 球中的 13 球降至 357 球中的 0 球。CPU 選球工作量減少 4.5 倍,命中所選球的比例由 53% 升至 65%。
We recently shipped a 3D 8 Ball Pool game for the browser: three.js, TypeScript, no engine. The first player feedback was blunt: "ball speed increases and decreases anytime."
TL;DR: "the physics feels wrong" turned out to be four separate bugs, and none of them were in the collision maths:
- physics stepped by frame time, plus a fast-forward hack;
- balls slipping off the table through the pocket mouth;
- the CPU freezing the game while choosing its shot;
- shaders compiling on the first shot.
1. Physics stepped by frame time, with a fast-forward on top
The loop did the classic thing: step(dt) with the frame's delta time. On top of that, an earlier "optimisation" ran physics 2-3 times per frame once every ball was slow, to cut the boring tail of a shot (a break went from 12 s to 7 s). Players saw exactly what we had built: balls speeding up as they slowed down.
The fix is the boring, correct one (simplified):
const STEP = 1 / 120;
let acc = 0;
function frame(dt: number) {
acc += Math.min(dt, 0.1); // don't spiral after a tab switch
while (acc >= STEP) { prev = snapshot(); step(STEP); acc -= STEP; }
draw(blend(prev, current, acc / STEP)); // smooth at any refresh rate
}
Inside step() we always sub-step at 1/240 s, so the AI's look-ahead simulations produce the same paths as the real game. To make shots end sooner honestly, we changed friction instead of time: deceleration is now 30 + 0.25 * v (units/s²) and shots settle in 2-5 s.
Careful, though: break shots depend on that slow tail. A much stronger friction we tried potted nothing on the break.
2. Balls flew off the table through the pocket mouth
A test harness that fires hundreds of shots found 13 of 356 balls leaving the table. Balls arriving slightly off the pocket's centre slipped through the gap between cushion and pocket and sailed into the void, leaving a frame nobody could finish.
Real tables have "knuckles", the rounded jaw points either side of a pocket. Two small circular jaw points per pocket, plus a safety net (anything past the rail drops into the nearest pocket), took it to 0 of 357.
3. The CPU froze the game at the start of its turn
Profiling showed a 180-270 ms freeze on a desktop at the start of every CPU turn, and up to 1.35 s with CPU throttling. The AI picked its shot by simulating about 170 candidate shots in one go.
We turned the search into a generator and run it for ~5 ms per frame (simplified):
function* searchShot(table: Table) {
for (const cand of candidates(table)) {
if (pathBlocked(table, cand)) continue; // cheap ray check first
score(cand, simulate(table, cand));
yield; // let the frame breathe
}
}
// each frame, while it's the CPU's turn:
const t0 = performance.now();
while (performance.now() - t0 < 5 && !search.next().done) {}
The cheap "is the path blocked" check before each full simulation cut the work 4.5x. As a bonus, the CPU now pots 65% of its chosen shots instead of 53% on our test tables, because it wastes less time on hopeless lines. (It also got a human-paced cue swing, so it no longer shoots instantly.)
4. The first shot of the first game stuttered
About a second of jank on the first frame of the first game: shader compilation. three.js compiles a material's program the first time it's drawn. We now call renderer.compileAsync(scene, camera) behind the title screen, after applying the player's table and cue: a material that gains a texture map needs a new program, so warm up the final look, not the default one. The renderer also only redraws when something changed, which matters on a pool table that sits still most of the time.
The lesson
"The physics feels wrong" was a time-step bug, a geometry gap, a main-thread stall and a shader warm-up. If your game feels off, profile and measure before you touch the physics constants.
You can play the result free in the browser, on desktop or phone: 8 Ball Pool on ZoomyGames. We make free web games at ZoomyGames and write up what we learn along the way.
來源:DEV Community · dev.to


