-
-
Notifications
You must be signed in to change notification settings - Fork 188
Description
Goal
Support non-60hz display rates. This includes:
-
the 50hz standard used in Europe, Australia, New Zealand, and parts of Africa, Asia, and South America.
-
high refresh rate displays (many Androids and gaming monitors)
-
ProMotion variable refresh rate displays (latest iPhone models)
I want p5play developers to be able to share their games with the whole world and have it run well on any capable device.
p5play game developers and players shouldn't have to worry about frame rates.
The Problem
The default target frame rate is 60. Currently if you want the target to be something else, like 50hz for example, you'd need to run frameRate(50)
.
The p5.js draw loop uses requestAnimationFrame
but just avoids drawing anything at a higher rate than 60fps by default. Users with higher refresh rate displays are just stuck at 60hz, but if the user's display rate is lower, like 50hz... umm Houston we got a problem! The physics simulation will run 16% slower than real time because by default, p5play updates the physics simulation by 1/60th of a second at the end of each draw call.
By default q5.js will run the draw loop using requestAnimationFrame
based on the device's display rate. This would also make the physics simulation too slow on 50hz displays and way too fast on high refresh rate displays.
Personally, as a tech consumer, I've been a big fan of increasing visual fidelity over smoothness, opting for 4K over higher refresh rates. For me the difference between 4K and 1080p is HUGE, but with refresh rates higher than 60hz, I can't really tell the difference. Maybe my eyes are slow or something lol. All the devices I personally own can "only" do 60hz. So my personal biases led me to not consider this major problem until now.
Also it'd be nice if p5play could limit the physics simulation to 30hz, if the user's device isn't capable of achieving 60fps.
I was quite conflicted on how to approach this problem, so I did research on Unity.
How Unity does it
Unity separates the game's physics updates from frame rendering.
Unity uses a fixed timestep of 1/50th of a second for physics calculations to ensure consistent physics simulation, regardless of the frame rate.
Unity uses a variable timestep for rendering and general game logic, which is handled in the Update method. This method is called once per frame, so the frequency can vary depending on the display rate.
In between physics updates, Unity interpolates the positions of physics objects for rendering. This means that even if a physics update doesn't happen at the exact same time as a frame update, Unity will estimate the current position game objects based on its previous physics update, projecting from there. This allows the game object to appear to move smoothly at any higher frame rate, even though its actual position is only being updated in the physics sim at a fixed rate of 50hz.
This approach enables Unity to provide consistent physics simulation while still rendering as smoothly as possible based on the performance and capabilities of the device.
I think implementing something like this in p5play would be ideal, but how?
Frames
If the goal is to not require developers or players to worry about frame rates, that means not having a default frame rate. That means maybe frames shouldn't be used as a unit of time measurement anywhere in a game's code. Yikes!
- The collision handling system stores how long players have been colliding in frames.
- Input devices store how long the user has been doing an input action in frames.
- Animations use the
ani.frameDelay
property to define how many frames an image in the animation is displayed before the next image is displayed. - Also q5/p5 sketches typically use
frameCount
as the measure of time.
Switching from frames to seconds would require making tons of breaking changes to p5play.
The case for abstracting frames
@davepagurek suggested an alternative solution:
I guess there's maybe a world where "frames" are like css pixels, and one "frame" as a unit might actually represent multiple real frames under the hood on high refresh rate displays?
not sure if that's more or less confusing though haha
like if everyone defined sketches as if it were 60fps, but sometimes the frame count is a fraction instead of a whole number
More info: https://www.quirksmode.org/blog/archives/2010/04/a_pixel_is_not.html
I do find using frames to be more convenient than milliseconds, a level of precision that's not required for typical user input handling.
I also agree that this issue with higher refresh rates is a bit analogous to the challenge that high pixel density displays posed to developers over a decade ago.
When Apple first introduced high pixel density displays to consumers, I thought the abstract re-branding of pixels was confusing, now it seems perfectly natural. Yet, was that only acceptable because web developers no longer needed to care about real pixels? With retina displays users could zoom in and out without really compromising the visual appearance of text, which Apple had just a few years prior been boasting about always displaying pixel perfect on lower resolution displays.
Have we gotten to that same point with high refresh rate displays above 60fps? Perhaps so.
...continued below
Metadata
Metadata
Assignees
Labels
Projects
Status