Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(frontend): Ensure physics run consistently across different device framerates #14861

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- Fix: デッキのタイムラインカラムで「センシティブなファイルを含むノートを表示」設定が使用できなかった問題を修正
- Fix: Encode RSS urls with escape sequences before fetching allowing query parameters to be used
- Fix: リンク切れを修正
- Fix: Ensure physics run the same across different framerates

### Server
- Enhance: 起動前の疎通チェックで、DBとメイン以外のRedisの疎通確認も行うように
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"insert-text-at-cursor": "0.3.0",
"is-file-animated": "1.0.2",
"json5": "2.2.3",
"matter-js": "0.19.0",
"matter-js": "0.20.0",
"mfm-js": "0.24.0",
"misskey-bubble-game": "workspace:*",
"misskey-js": "workspace:*",
Expand Down
33 changes: 17 additions & 16 deletions packages/frontend/src/pages/drop-and-fusion.game.vue
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ let bgmNodes: ReturnType<typeof sound.createSourceNode> | null = null;
let renderer: Matter.Render | null = null;
let monoTextures: Record<string, Blob> = {};
let monoTextureUrls: Record<string, string> = {};
let tickRaf: number | null = null;
let tickInterval: number | null = null;
let game = new DropAndFusionGame({
seed: seed,
gameMode: props.gameMode,
Expand Down Expand Up @@ -663,13 +663,20 @@ function getTextureImageUrl(mono: Mono) {
}
}

function startTicking(tickFunction: () => void) {
tickInterval = window.setInterval(tickFunction, game.TICK_DELTA);
}

function stopTicking() {
if (tickInterval !== null) {
window.clearInterval(tickInterval);
tickInterval = null;
}
}

function tick() {
const hasNextTick = game.tick();
if (hasNextTick) {
tickRaf = window.requestAnimationFrame(tick);
} else {
tickRaf = null;
}
if (!hasNextTick) stopTicking();
}

function tickReplay() {
Expand Down Expand Up @@ -700,11 +707,7 @@ function tickReplay() {
if (!hasNextTick) break;
}

if (hasNextTick) {
tickRaf = window.requestAnimationFrame(tickReplay);
} else {
tickRaf = null;
}
if (!hasNextTick) stopTicking();
}

async function start() {
Expand All @@ -716,7 +719,7 @@ async function start() {
});
Matter.Render.run(renderer);
game.start();
window.requestAnimationFrame(tick);
startTicking(tick);

gameLoaded.value = true;

Expand Down Expand Up @@ -803,9 +806,7 @@ function reset() {
function dispose() {
game.dispose();
if (renderer) Matter.Render.stop(renderer);
if (tickRaf) {
window.cancelAnimationFrame(tickRaf);
}
stopTicking();
}

function backToTitle() {
Expand All @@ -829,7 +830,7 @@ function replay() {
});
Matter.Render.run(renderer);
game.start();
window.requestAnimationFrame(tickReplay);
startTicking(tickReplay);
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/misskey-bubble-game/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class DropAndFusionGame extends EventEmitter<{
public readonly DROP_COOLTIME = 30; // frame
public readonly PLAYAREA_MARGIN = 25;
private STOCK_MAX = 4;
private TICK_DELTA = 1000 / 60; // 60fps
public readonly TICK_DELTA = 1000 / 60; // 60fps

public frame = 0;
public engine: Matter.Engine;
Expand Down
16 changes: 13 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading