turbulenz / turbulenz_engine

Turbulenz is a modular 3D and 2D game framework for making HTML5 powered games for browsers, desktops and mobile devices.

Home Page:http://turbulenz.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

using requestAnimationFrame for logic updates?

jeremyawon opened this issue · comments

I was looking over the implementation of setInterval in turbulenzengine.ts.

Using requestAnimationFrame surprises me since I thought only rendering would be done in an animationFrame callback, with game state updates happening through an independent loop.

Am I even right in thinking that, e.g., physics engine updates happen through WebGLTurbulenzEngine.setInterval and therefore in a requestAnimationFrame? Is there another interval system I'm overlooking?

In any case, this system clearly works well in turbulenz. Did the team try different architectures?

Yes, updates and rendering are done in that way if you are looking to update at 60fps. We use request animation frame because its more reliable callback than window.setInterval for millisecond accurate callbacks on some browsers. You can of course use window.setInterval and window.setTimeout if you wish, but if precise timing is important to you then you should probably stick with TurbulenzEngine.setInterval.

One side note. On some browsers requestAnimationFrame won't be called if the tab being displayed is hidden. In Turbulenz we use this to make sure games are not updating when they are not visible. If you need to process i.e. doing networking processing etc, then you might want to use timeouts and/or intervals for keep alives and TurbulenzEngine.setInterval for rendering.