Skip to content

Commit 1ddf3eb

Browse files
🤖 Merge PR DefinitelyTyped#73620 [@types/matter.js] Update type definitions for Matter.Runner to reflect matter.js docs by @danstuff
Co-authored-by: Steven Snoeijen <[email protected]>
1 parent 9a74cf9 commit 1ddf3eb

File tree

2 files changed

+137
-22
lines changed

2 files changed

+137
-22
lines changed

‎types/matter-js/index.d.ts‎

Lines changed: 132 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2704,28 +2704,84 @@ declare namespace Matter {
27042704

27052705
export interface IRunnerOptions {
27062706
/**
2707-
* A `Boolean` that specifies if the runner should use a fixed timestep (otherwise it is variable).
2708-
* If timing is fixed, then the apparent simulation speed will change depending on the frame rate (but behaviour will be deterministic).
2709-
* If the timing is variable, then the apparent simulation speed will be constant (approximately, but at the cost of determininism).
2707+
* The fixed timestep size used for `Engine.update` calls in milliseconds, known as `delta`.
27102708
*
2711-
* @default false
2712-
*/
2713-
isFixed?: boolean | undefined;
2714-
2715-
/**
2716-
* A `Number` that specifies the time step between updates in milliseconds.
2717-
* If `engine.timing.isFixed` is set to `true`, then `delta` is fixed.
2718-
* If it is `false`, then `delta` can dynamically change to maintain the correct apparent simulation speed.
2709+
* This value is recommended to be `1000 / 60` ms or smaller (i.e. equivalent to at least 60hz).
2710+
*
2711+
* Smaller `delta` values provide higher quality results at the cost of performance.
2712+
*
2713+
* You should usually avoid changing `delta` during running, otherwise quality may be affected.
2714+
*
2715+
* For smoother frame pacing choose a `delta` that is an even multiple of each display FPS you target, i.e. `1000 / (n * fps)` as this helps distribute an equal number of updates over each display frame.
2716+
*
2717+
* For example with a 60 Hz `delta` i.e. `1000 / 60` the runner will on average perform one update per frame on displays running 60 FPS and one update every two frames on displays running 120 FPS, etc.
2718+
*
2719+
* Where as e.g. using a 240 Hz `delta` i.e. `1000 / 240` the runner will on average perform four updates per frame on displays running 60 FPS and two updates per frame on displays running 120 FPS, etc.
2720+
*
2721+
* Therefore `Runner.run` will call multiple engine updates (or none) as needed to simulate the time elapsed between browser frames.
2722+
*
2723+
* In practice the number of updates in any particular frame may be restricted to respect the runner's performance budgets. These are specified by `runner.maxFrameTime` and `runner.maxUpdates`, see those properties for details.
27192724
*
27202725
* @default 1000 / 60
27212726
*/
27222727
delta?: number | undefined;
27232728

27242729
/**
27252730
* A flag that specifies whether the runner is running or not.
2731+
*
27262732
* @default true
27272733
*/
27282734
enabled?: boolean | undefined;
2735+
2736+
/**
2737+
* The measured time elapsed between the last two browser frames in milliseconds.
2738+
* This is useful e.g. to estimate the current browser FPS using `1000 / runner.frameDelta`.
2739+
*/
2740+
frameDelta?: number | undefined;
2741+
2742+
/**
2743+
* Enables averaging to smooth frame rate measurements and therefore stabilise play rate.
2744+
*
2745+
* @default true
2746+
*/
2747+
frameDeltaSmoothing?: boolean | undefined;
2748+
2749+
/**
2750+
* Rounds measured browser frame delta to the nearest 1 Hz.
2751+
* This option can help smooth frame rate measurements and simplify handling hardware timing differences e.g. 59.94Hz and 60Hz displays.
2752+
* For best results you should also round your `runner.delta` equivalent to the nearest 1 Hz.
2753+
*
2754+
* @default true
2755+
*/
2756+
frameDeltaSnapping?: boolean | undefined;
2757+
2758+
/**
2759+
* A performance budget that limits execution time allowed for this runner per browser frame in milliseconds.
2760+
*
2761+
* To calculate the effective browser FPS at which this throttle is applied use `1000 / runner.maxFrameTime`.
2762+
*
2763+
* This performance budget is intended to help maintain browser interactivity and help improve framerate recovery during temporary high CPU usage.
2764+
*
2765+
* This budget only covers the measured time elapsed executing the functions called in the scope of the runner tick, including `Engine.update` and its related user event callbacks.
2766+
*
2767+
* You may also reduce this budget to allow for any significant additional processing you perform on the same thread outside the scope of this runner tick, e.g. rendering time.
2768+
*
2769+
* See also `runner.maxUpdates`.
2770+
*
2771+
* @default 1000 / 30
2772+
*/
2773+
maxFrameTime?: number | undefined;
2774+
2775+
/**
2776+
* An optional limit for maximum engine update count allowed per frame tick in addition to `runner.maxFrameTime`.
2777+
*
2778+
* Unless you set a value it is automatically chosen based on `runner.delta` and `runner.maxFrameTime`.
2779+
*
2780+
* See also `runner.maxFrameTime`.
2781+
*
2782+
* @default null
2783+
*/
2784+
maxUpdates?: number | null;
27292785
}
27302786

27312787
/**
@@ -2783,6 +2839,29 @@ declare namespace Matter {
27832839
*/
27842840
static start(runner: Runner, engine: Engine): void;
27852841

2842+
/**
2843+
* The fixed timestep size used for `Engine.update` calls in milliseconds, known as `delta`.
2844+
*
2845+
* This value is recommended to be `1000 / 60` ms or smaller (i.e. equivalent to at least 60hz).
2846+
*
2847+
* Smaller `delta` values provide higher quality results at the cost of performance.
2848+
*
2849+
* You should usually avoid changing `delta` during running, otherwise quality may be affected.
2850+
*
2851+
* For smoother frame pacing choose a `delta` that is an even multiple of each display FPS you target, i.e. `1000 / (n * fps)` as this helps distribute an equal number of updates over each display frame.
2852+
*
2853+
* For example with a 60 Hz `delta` i.e. `1000 / 60` the runner will on average perform one update per frame on displays running 60 FPS and one update every two frames on displays running 120 FPS, etc.
2854+
*
2855+
* Where as e.g. using a 240 Hz `delta` i.e. `1000 / 240` the runner will on average perform four updates per frame on displays running 60 FPS and two updates per frame on displays running 120 FPS, etc.
2856+
*
2857+
* Therefore `Runner.run` will call multiple engine updates (or none) as needed to simulate the time elapsed between browser frames.
2858+
*
2859+
* In practice the number of updates in any particular frame may be restricted to respect the runner's performance budgets. These are specified by `runner.maxFrameTime` and `runner.maxUpdates`, see those properties for details.
2860+
*
2861+
* @default 1000 / 60
2862+
*/
2863+
delta: number;
2864+
27862865
/**
27872866
* A flag that specifies whether the runner is running or not.
27882867
*
@@ -2791,22 +2870,54 @@ declare namespace Matter {
27912870
enabled: boolean;
27922871

27932872
/**
2794-
* A `Boolean` that specifies if the runner should use a fixed timestep (otherwise it is variable).
2795-
* If timing is fixed, then the apparent simulation speed will change depending on the frame rate (but behaviour will be deterministic).
2796-
* If the timing is variable, then the apparent simulation speed will be constant (approximately, but at the cost of determininism).
2873+
* The measured time elapsed between the last two browser frames in milliseconds.
2874+
* This is useful e.g. to estimate the current browser FPS using `1000 / runner.frameDelta`.
2875+
*/
2876+
frameDelta: number;
2877+
2878+
/**
2879+
* Enables averaging to smooth frame rate measurements and therefore stabilise play rate.
27972880
*
2798-
* @default false
2881+
* @default true
27992882
*/
2800-
isFixed: boolean;
2883+
frameDeltaSmoothing: boolean;
28012884

28022885
/**
2803-
* A `Number` that specifies the time step between updates in milliseconds.
2804-
* If `engine.timing.isFixed` is set to `true`, then `delta` is fixed.
2805-
* If it is `false`, then `delta` can dynamically change to maintain the correct apparent simulation speed.
2886+
* Rounds measured browser frame delta to the nearest 1 Hz.
2887+
* This option can help smooth frame rate measurements and simplify handling hardware timing differences e.g. 59.94Hz and 60Hz displays.
2888+
* For best results you should also round your `runner.delta` equivalent to the nearest 1 Hz.
28062889
*
2807-
* @default 1000 / 60
2890+
* @default true
28082891
*/
2809-
delta: number;
2892+
frameDeltaSnapping: boolean;
2893+
2894+
/**
2895+
* A performance budget that limits execution time allowed for this runner per browser frame in milliseconds.
2896+
*
2897+
* To calculate the effective browser FPS at which this throttle is applied use `1000 / runner.maxFrameTime`.
2898+
*
2899+
* This performance budget is intended to help maintain browser interactivity and help improve framerate recovery during temporary high CPU usage.
2900+
*
2901+
* This budget only covers the measured time elapsed executing the functions called in the scope of the runner tick, including `Engine.update` and its related user event callbacks.
2902+
*
2903+
* You may also reduce this budget to allow for any significant additional processing you perform on the same thread outside the scope of this runner tick, e.g. rendering time.
2904+
*
2905+
* See also `runner.maxUpdates`.
2906+
*
2907+
* @default 1000 / 30
2908+
*/
2909+
maxFrameTime: number;
2910+
2911+
/**
2912+
* An optional limit for maximum engine update count allowed per frame tick in addition to `runner.maxFrameTime`.
2913+
*
2914+
* Unless you set a value it is automatically chosen based on `runner.delta` and `runner.maxFrameTime`.
2915+
*
2916+
* See also `runner.maxFrameTime`.
2917+
*
2918+
* @default null
2919+
*/
2920+
maxUpdates: number | null;
28102921
}
28112922

28122923
/**

‎types/matter-js/matter-js-tests.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,12 @@ var render = Render.create({
177177
// Runner
178178
const runner1 = Matter.Runner.create({
179179
delta: 1000 / 60,
180-
isFixed: false,
181180
enabled: true,
181+
frameDelta: 1000 / 60,
182+
frameDeltaSmoothing: true,
183+
frameDeltaSnapping: true,
184+
maxFrameTime: 1000 / 30,
185+
maxUpdates: null,
182186
});
183187
const runner2 = Matter.Runner.create({});
184188
const runner3 = Matter.Runner.create();

0 commit comments

Comments
 (0)