Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timer: Added FixedTimer #27423

Merged
merged 4 commits into from
Dec 22, 2023
Merged

Timer: Added FixedTimer #27423

merged 4 commits into from
Dec 22, 2023

Conversation

mrdoob
Copy link
Owner

@mrdoob mrdoob commented Dec 22, 2023

Related issue: #17912

Description

Moved the _fixedDelta and _useFixedDelta functionality into its own FixedTimer class and removed the enableFixedDelta() and disableFixedDelta() methods.

I think this approach is cleaner... but I'm not sure how FixedTimer is supposed to be used.

@Mugen87 I guess it's supposed to be used for debugging? #17912 (comment)

@mrdoob mrdoob added this to the r160 milestone Dec 22, 2023
@mrdoob mrdoob requested a review from Mugen87 December 22, 2023 08:59
@CodyJasonBennett
Copy link
Contributor

Very nice for physics or delta-based simulation, also see https://gafferongames.com/post/fix_your_timestep.

@mrdoob
Copy link
Owner Author

mrdoob commented Dec 22, 2023

Hmm...

In the context of physics in the browser, isn't this better?

const fps = 60;

setInterval( step, 1000 / fps );

function step() {

    world.stepSimulation( 1000 / fps, 10 );
    ...

}

https://github.com/mrdoob/three.js/blob/dev/examples/jsm/physics/AmmoPhysics.js#L265
https://github.com/mrdoob/three.js/blob/dev/examples/jsm/physics/RapierPhysics.js#L209

Otherwise, FixedTimer would need to have more logic inside if we want to ensure 60fps in any device.

@Mugen87
Copy link
Collaborator

Mugen87 commented Dec 22, 2023

The idea of fixed time deltas comes from Unity: https://docs.unity3d.com/ScriptReference/Time.html. They describe their property as follows:

The interval in seconds at which physics and other fixed frame rate updates (like MonoBehaviour's FixedUpdate) are performed.

I have found it useful by myself if I can configure a fixed time delta for testing purposes.

@Mugen87
Copy link
Collaborator

Mugen87 commented Dec 22, 2023

Does the setInterval() approach compute exact time deltas? I'm asking since I don't know how precise setInterval() actually is.

@mrdoob
Copy link
Owner Author

mrdoob commented Dec 22, 2023

Using setInterval() we can update a function at 60fps even if the device runs at 120fps.

Then we compute the deltas ourselves:
https://github.com/mrdoob/three.js/blob/dev/examples/jsm/physics/AmmoPhysics.js#L209

I can imagine new FixedTimer() being useful for debugging, but I'm just not sure how it's supposed to be used for physics.

@CodyJasonBennett
Copy link
Contributor

I had misunderstood how this class worked. Physics wants to subdivide the real delta time as per the reference I linked, which is not what this class does.

# Conflicts:
#	examples/jsm/misc/Timer.js
@Mugen87
Copy link
Collaborator

Mugen87 commented Dec 22, 2023

@mrdoob Indeed, we can't use it for physics but FixedTimer still has its value for testing purposes. Do you think this use case justifies to keep FixedTimer?

The refactoring itself definitely makes sense and the PR in its current state looks good to me (just need to clean up the docs).

@mrdoob
Copy link
Owner Author

mrdoob commented Dec 22, 2023

I think we can keep FixedTimer yeah.

I'll remove the references to fixedDelta in the docs.

@mrdoob mrdoob merged commit 08b24a1 into dev Dec 22, 2023
11 checks passed
@mrdoob mrdoob deleted the timer branch December 22, 2023 10:20
AdaRoseCannon pushed a commit to AdaRoseCannon/three.js that referenced this pull request Jan 15, 2024
* Timer: Added FixedTimer.

* Updated docs.

* Updated docs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants