| this.ctx = canvas.getContext("2d"); | this.ctx = canvas.getContext("2d"); | ||||
| this.lastTime = null; | this.lastTime = null; | ||||
| this.raf = null; | this.raf = null; | ||||
| this.minPhysFPS = 30; | |||||
| this.minPhysFPS = 20; | |||||
| this.minFPS = 5; | this.minFPS = 5; | ||||
| this.backgroundColor = "#87CEFA"; | this.backgroundColor = "#87CEFA"; | ||||
| // We accept really low FPSes, | // We accept really low FPSes, | ||||
| // but if the FPS gets too low, we want to run multiple | // but if the FPS gets too low, we want to run multiple | ||||
| // physics steps in the frame. | // physics steps in the frame. | ||||
| let physdt = dt; | |||||
| let nSteps = 1; | let nSteps = 1; | ||||
| while (1 / dt < this.minPhysFPS) { | |||||
| dt /= 2; | |||||
| while (1 / physdt < this.minPhysFPS) { | |||||
| physdt /= 2; | |||||
| nSteps *= 2; | nSteps *= 2; | ||||
| } | } | ||||
| if (nSteps != 1) | |||||
| console.log(nSteps); | |||||
| if (nSteps > 1) { | |||||
| console.log( | |||||
| "Too long between frames for physics ("+dt.toFixed(2)+"s). "+ | |||||
| "Running "+nSteps+" simulations in one frame."); | |||||
| } | |||||
| for (let i = 0; i < nSteps; ++i) | for (let i = 0; i < nSteps; ++i) | ||||
| this.physics(dt); | |||||
| this.physics(physdt); | |||||
| } | } | ||||
| this.draw(); | this.draw(); |