在 Typescript 中创建 FixedUpdate

Creating a FixedUpdate in Typesript

我正在使用打字稿开发游戏引擎以学习一些东西。在 Unity 中,固定更新的工作方式(理论上)如下:

 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...
 }

在 Typescript 中实现类似的东西的好方法是什么?我做更新功能的方式如下:

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();
    });
  }

这样,每个fixedupdate可以有很多update,但是我每次只能得到一个fixedupdate。

提前致谢,Steenbrink

好吧,没关系,我很笨。我会把这个留在这里供参考。

我修复的方法如下:

private fixedUpdateTiming: number = 20;
private physicsTimeSimulated:number = Date.now();
private _deltaTime: number = 0;
private lastUpdate: number = Date.now();

public Update(): void {

    while(this.physicsTimeSimulated < Date.now()){

      this.onAppFixedUpdate.dispatch();
      this.physicsTimeSimulated += this.fixedUpdateTiming;
    }

    this.onAppUpdate.dispatch();

    this._deltaTime = Date.now() - this.lastUpdate;
    this.lastUpdate = Date.now();

    window.requestAnimationFrame(() => {
      this.Update();
    });
  }