4 ms·
Since the movement speed is tied to requestAnimationFrame, the speed of the game changes with the update frequency of the user's monitor. Since all my monitors
by daxterspeed 8y ago
Since the movement speed is tied to requestAnimationFrame, the speed of the game changes with the update frequency of the user's monitor. Since all my monitors are at 144Hz this game is ridiculously fast for me.
- kakarot 8y agoTo clarify, there's nothing wrong with tying your movement to requestAnimationFrame() and it is in fact encouraged, but you must modulate it by the frame delta.
- giornogiovanna 8y agoThat is only true if you write your own physics. Otherwise, your physics framerate should be fixed, usually at 30FPS. Many physics engines don't support variable timestep, and in the ones that do, it can lead to instability and general weirdness.
- kakarot 8y agoI don't understand how not writing your own physics engine automatically negates what I said. If a developer has implemented a third-party physics engine, they should refer to that engine's documentation.
- giornogiovanna 8y agoTypically physics engines can't be "modulated by the delta" (which I assume means varying the dt when integrating), you just have to tick the physics world multiple times and keep the roundoff for next time. [0] gives a good example of implementing this kind of thing. If you implement your own, you can make dt a parameter instead of a constant, but this is more complicated, and can lead to tunnelling when the framerate is too low. [0]: https://gafferongames.com/post/fix_your_timestep/ https://gafferongames.com/post/fix_your_timestep/
- kakarot 8y agoWell first it depends on what you mean by typically. Number of total engines in the wild? Or current market share? But besides, if you are using a fixed physics engine you should not be using rAF. It's not meant for that. Your comment is just a bit tangential.
- giornogiovanna 8y agoBullet, Box2D and PhysX all highly recommend using a fixed timestep, and no, you should definitely be using requestAnimationFrame with a fixed timestep, just like in the fix-your-timestep article. The number of frames to advance the simulation by is still determined by the screen's refresh rate, after all.
- kakarot 8y agoThat really depends. Pegging your fixed timestep engine to rAF could have the consequence of dropping frames when you have a large gap between two frames and enough physics frames get called at once that the next rAF window gets missed. If your performance is bounded by the physics simulation, you could potentially trigger a spiral of death and never catch up. Of course there are ways to mitigate this, but that just further complicates things. You have the right idea, adding each frame delta to an accumulator and triggering a physics step when reaches the appropriate value, however rAF is not the answer because it can create a potentially infinite frame delta. It's usually best to not peg your fixed-timestep physics to rAF or at the very least, pause the game entirely when the window isn't focused.
- giornogiovanna 8y agoDamn, you're right, it's more complicated than I thought. But what else would you use? setInterval suffers from the same problem, doesn't it?
- kakarot 8y agoThat's a good question. setInterval definitely also has its strengths and weaknesses, but it does beat rAF() here in that you can guarantee at least one frame will be ran every X milliseconds. With regards to Javascript physics engines, I'm only versed with Matter.js. Matter.js's Engine.update() takes a frame delta as an argument and uses Verlet integration [0] to make things work under the hood. You can optionally calculate a correction factor to be applied each frame in order to smooth out calculations with janky framerates, but if you can maintain constant FPS then it's not an issue. Unfortunately, rAF() once again becomes a pain because of the lack of proper control over FPS capping, so monitors with higher refresh rates have a larger window for variable frame deltas. I'm hopeful that these shortcomings will eventually be addressed in the spec. [0] https://lonesock.net/article/verlet.html https://lonesock.net/article/verlet.html
- davedx 8y agoWeird statement. All games and game engines I've worked with have used frame deltas to modulate everything from animation and movement to AI and UI's. I've worked on mobile games to AAA games. Can you link to a physics engine that doesn't support variable timestep?
- deleted 8y ago[deleted]
- giornogiovanna 8y agoYou definitely use frame deltas when updating things (I mean, how else would you know how much time has passed), but when doing physics, that frame delta is constant for the lifetime of the simulation. Box2D User Manual > We also don't like the time step to change much. A variable time step produces variable results, which makes it difficult to debug. So don't tie the time step to your frame rate (unless you really, really have to). https://stackoverflow.com/a/21273467 https://stackoverflow.com/a/21273467 (Bullet) > maxSubSteps: Should generally stay at one so Bullet interpolates current values on its own. A value of zero implies a variable tick rate, meaning Bullet advances the simulation exactly timeStep seconds instead of interpolating. This feature is buggy and not recommended. Edit: The archived version of the (404-ing) Bullet Wiki also says "Bullet maintains an internal clock, in order to keep the actual length of ticks constant. This is pivotally important for framerate independence. The third parameter is the size of that internal step."
- bartread 8y ago> Since the movement speed is tied to requestAnimationFrame, the speed of the game changes with the update frequency of the user's monitor. Since all my monitors are at 144Hz this game is ridiculously fast for me. Damn. I just checked this and you're right: https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame https://developer.mozilla.org/en-US/docs/Web/API/window/requ.... Learned something there so thanks for pointing this out. I'd always believed that callbacks were locked at 60 fps, although I'm not sure why. Clearly this means all my own games will run at the wrong speed on machines with a higher refresh rate, and will probably appear jerky on displays with a lower rate (assuming such still exist).
- tvararu 8y agoI was surprised to learn that many commercial HDR 4K TVs have a 50Hz real refresh rate. Sub 60Hz screens are bountiful and common.
- nacs 8y agoIt's also common on a lot of mobile devices as well -- iOS can actually vary the refresh rate on a device from 30Hz (energy-efficient) to 60hz ('standard') upto 120Mhz ("ProMotion") and I'm sure Android probably does this also.