Currently, I am delving into the world of game-engine development using TypeScript to expand my skills. The fixed update mechanism in Unity operates on a theoretical premise that involves processing physics within a while loop. Here's how it is envisioned:
var physicsTimeSimulated = 0;
var lastUpdateTime = 0;
while (Unity is Running)
{
while (physicsTimeSimulated < Time.time)
{
Engine.ExecutePhysicsStep();
Engine.FixedUpdate(); // <-- sent to all objects
physicsTimeSimulated += physicsTimeStep;
}
deltaTime = Time.time - lastUpdateTime;
Engine.RenderFrame();
Engine.Update(); // <-- sent to all objects
lastUpdateTime = CurrentTime;
// and repeat...
}
I am considering the best way to implement a similar behavior in TypeScript. My current approach to handling updates looks like this:
private fixedUpdateTiming: number = 1;
private lastUpdate:number = Date.now();
public Update(): void {
while((Date.now() - this.lastUpdate) > this.fixedUpdateTiming){
this.onAppFixedUpdate.dispatch();
this.lastUpdate = Date.now();
}
this.onAppUpdate.dispatch();
window.requestAnimationFrame(() => {
this.Update();
});
}
This method allows for multiple updates per fixedupdate cycle, ensuring only one fixed update per update execution.
Thank you in advance for your insights,
Steenbrink