Is there any playground example about how to use this delta time? Scene | Babylon.js Documentation (babylonjs.com), is this the right thing to use if I need to know how many seconds passed?
You can use scene.deltaTime to know how many milliseconds has passed since the last frame, but it’s clamped between scene.MinDeltaTime and scene.MaxDeltaTime, which default to 1 and 1000. If you want the raw, unclamped value you can call engine.getDeltaTime() instead.
Here’s just a super simple example of using deltaTime to animate a box’s rotation. It rotates at 0.001 radians per millisecond, so the overall animation speed should end up the same at slower or faster frame rates.
PS, you can also use scene.getAnimationRatio() instead, which is just deltaTime * (60 / 1000). So it will be 1 when the frame rate is 60 fps, and it will be 2 when the frame rate is 30 fps (since you need to rotate the box twice as far per frame), for example. And here’s the same PG using getAnimationRatio() for comparison.
Thank you Blake, I’m using scene.registerBeforeRender, and I noticed that depending on the fps, my method executed with different time, is there any way to fix this? For example, I want the box to rotate every 5 seconds, here is a playground example I did based on your example.
simple animation example using deltaTime | Babylon.js Playground (babylonjs.com)
Ooh I thought you were looking for smooth animation, the topic that I answered about. If you just want something to happen once every 5 seconds you could use setInterval like below.
There’s also other options, e.g. you could use engine.getDeltaTime() to get the unclamped, actual time since the last frame and use a counter… Or you could use Date.now() to get the current time in milliseconds and calculate your own delta from the last event that way. Also there’s external libraries on GitHub for timers as well. These days I mostly just use setInterval thou with long timeouts for things that aren’t as important, and then just use getAnimationRatio for everything that needs to update each frame…
Sorry for the confusion, the example you give about smooth animation is also very helpful.
For the setInterval method, I see in your playground, it shows
I also searched for the example about setInterval in babylon doc, the playground here also does not work
Getting Started - Chapter 2 - Sound | Babylon.js Documentation (babylonjs.com)
Babylon.js Playground (babylonjs.com)
That example from the documentation page is working for me, it’s beeping every 3 seconds. You might have it muted or maybe the site doesn’t have permission to play sounds on you browser maybe?
Hmm, I’m really not sure why you’re getting the webgl not supported error on the pg that just rotates the box thou. The other playgrounds are still working for you, right?
Yes, but I get the idea here, thank you! @Blake