fenomas / noa

Experimental voxel game engine.
MIT License
616 stars 91 forks source link

How many ticks per second? #67

Closed EliteAsian123 closed 5 years ago

EliteAsian123 commented 5 years ago

Hello Andy! Sorry if this question is documented somewhere, but how many ticks are there per second? By ticks I mean this function:

noa.on("tick", function(dt) { blah blah });

Thanks!

Edit: I'm guessing dt is delta time, however, on my 144 hertz (fps) monitor, dt is 33 and on my 60 hertz it's also 33. Is the rate that tick is called constant? Or does dt not stand for delta time? Thanks!

fenomas commented 5 years ago

Hi, thanks for a very good question - this is the kind of thing I need to document 😁

So there are two different recurring events in the engine, tick and render. Ticks are for game logic - processing inputs, doing physics, checking for collisions, etc. And renders are when the game redraws the screen. The assumption is that game logic may be too heavy to run at 60fps, so that's why the two update rates are decoupled.

Anyway the tick rate is configurable and defaults to 33 (milliseconds), meaning that physics and whatnot happens 30 times/second. The dt parameter to that event is the delta time between events, but it's always the same number since tick events use fixed timesteps.

Meanwhile render events are a separate interval, which is usually 60fps. Note that if you need to run display logic noa also emits beforeRender and afterRender events. They both have a dt parameter, but in this case the number will vary since it's the actual time since the last render (in milliseconds).

Finally, regarding 144hz monitors: noa's render events basically originate from requestAnimationFrame. And the docs claim that most browsers will fire RAF according to the display rate, but for me personally it seems to always fire at 60fps, even when I'm on a 120hz monitor. But I haven't investigated this deeply, there could be something I'm missing.

EliteAsian123 commented 5 years ago

Thanks for the answer! Have a good night!