A fixed timestep.
Timestep
uses an integrator
to connect a game loop with game state.
Create the following interface:
// my_integrator.ts
import type { IntegratorInterface } from "timestep";
class Integrator implements IntegratorInterface {
integrate(msInterval: number): void {
// tick through physics step
}
render(msInterval: number, deltaRemainder: number): void {
// Draw to canvas or update dom.
//
// Interpolate between [previous state, current state]
// with the delta remainder [0, 1].
}
error(e: Error) {
// maximum integration time was exceeded
}
}
The integrate
function is called between renders.
After integration, the render
funtion is called and given the timestep remainder.
Pass an Integrator
to an instance of Timestep
.
import { Timestep } from "timestep";
import { Integrator } from "my_integrator.ts";
let msInterval = 10; // millisecond integration interval
let integrator = new Integrator();
const timestep = new Timestep({ msInterval, integrator });
Then call start
or stop
where appropriate.
timestep.start();
timestep.stop();
Checkout a tiny ( code | live ) example.
Timestamp-js
is released under the BSD 3-Clause License.