I’ve started piecing together a little TypeScript game after researching and fiddling with Babylon.js and Three.js. I’ve run into an issue where the game runs at different speeds on different browser and devices depending on the frame rate. I can’t quite find anything substantial on Google or this forum that discusses this or a solution.
At the moment I’m using standard HTML events to detect key, mouse and touch events and then the scene.onBeforeRenderObservable + engine.runRenderLoop to move my mesh. Is there a setting to tell the engine to make movement/animation time based? I’ve been fiddling with using mesh.moveWithCollision (although I can’t get it to work properly) in hopes that modifying the mesh position is the problem.
I also looked into using the Babylon.js animate functions but couldn’t figure out how to do this as my movement is based on a key being held down and the animation is based on a single event to trigger key frames.
I don’t have time to setup an example, but i’m comfident that you’re looking for what’s normally referred to as “delta time”, the time in ms since last frame.
The value of your speed variable can be multiplied by “deltaTime” during onBeforeRenderObservable to ensure correct simulation across multiple devices and framerates.
Note: before applying deltaTime, you should divide your speed variable like so: speed = (speed / (1000 / 60))
@all Thanks for the feedback. So far it seems like the consensus is to use delta time. I am busy investigating although I can’t seem to find any proper samples using Babylon.js so I’ll see what I can get working.
@aWeirdo One more question though. Should my jump animation then also use delta time? I was looking at key-framing the jump but now understanding what you’ve explained it seems conflicting to use key-frames…
Or do I tell my mesh to increase it’s y coordinate and wait for gravity physics to pull him down? Which is also confusing as surely I would need to decrease “inertia” so that gravity eventually becomes stronger than the y force as it decays? Plus I assume I would need to add the delta time to the force + gravity? A little confusing…
BTW, this is for a platform style game. I built a game in Flash in about 2004 that had simple jump animations but back then never considered FPS or true physics so a little overwhelmed.
@Wancieho
Well, this sounds primarily as a design choice, you can use an animation with root motion or you can use a “static” animation and control the motion completely by code as you wrote about.
Either should be okay, animations themselves are controlled by time, and not scene fps, so only the motion would need deltaTime applied.
Afraid i don’t have much experience with simulating jumping.