3 ms·
Quick / dumb question : in the breakout game example (https://github.com/bradtraversy/vanillawebprojects/blob/master/breakout-game/script.js https://github.com/
by IMTDb 7y ago
Quick / dumb question : in the breakout game example (https://github.com/bradtraversy/vanillawebprojects/blob/master/breakout-game/script.js https://github.com/bradtraversy/vanillawebprojects/blob/mast...) wouldn't the game feel fast/slow depending on the power of your computer since the physics loop does not make use of the elapsed time between updates ?
- phormy 7y agoPhysics and game logic should always run at fixed intervals and not depend on the time available. Otherwise you may run into oscillations that can be observed e.g. in games with ragdoll physics where bodies suddenly start to twitch due to such glitches. Thus, physics and logic should run at a fixed rate (e.g. 120 Hz) while graphics can run at whatever speed the machine can manage.
- bArray 7y agoThe function `requestAnimationFrame(update)` on line 191 [1], typically updates at 60 frames per second [2]. When it fires, it re-calls the `update()` function starting on line 184 [3]. As you say though, it's not 100% reliable in terms of speed, but I imagine 90%+ who play the game get a good experience. [1] https://github.com/bradtraversy/vanillawebprojects/blob/master/breakout-game/script.js#L191 https://github.com/bradtraversy/vanillawebprojects/blob/mast... [2] https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame https://developer.mozilla.org/en-US/docs/Web/API/window/requ... [3] https://github.com/bradtraversy/vanillawebprojects/blob/master/breakout-game/script.js#L184 https://github.com/bradtraversy/vanillawebprojects/blob/mast...
- rypskar 7y agoTrue when using a normal monitor, but could be a problem if using a 120Hz monitor or if the previous call didn't finish fast enough. The best is to use the timestamp requestAnimationFrame sends in to the callback to find time-delta since last call >> The number of callbacks is usually 60 times per second, but will generally match the display refresh rate in most web browsers as per W3C recommendation.