Jump to content

BabylonJS, the game loop and "time"


dav74
 Share

Recommended Posts

Hi Temechon thank you for your help (again ;-))

for example in threeJS we have : 

var chrono=new THREE.Clock();//init world...............chrono.start();	function animation(){	t=chrono.getElapsedTime()	sph2.position.x=80*Math.sin(t*0.7);	sph2.position.z=80*Math.cos(t*0.7);	}	function play(){	requestAnimationFrame(play);	animation();	renderer.render(scene, camera);}

Does the  "BABYLON.Clock()" constructor exist ?

else how to do the same thing (to separate "time variable" and FPS) ? With 2 "GetCurentTime" at the beginning and at the end of the game loop ?

Link to comment
Share on other sites

I don't think the constructor BABYLON.Clock() exists today (but maybe Deltakosh will contradict me), but you can create it yourself (it may be a good exercise for your students :) ).

I would create a new function using 

(new Date).getTime()

 to have the current time.

 

Then, this method could be used at the beginning and at the end of your process. I think it's the easiest way to do it.

Link to comment
Share on other sites

how reach the beginning and the end of the process ? In the "RenderLoop" function ?

// Render loopvar renderLoop = function () {    // Start new frame    engine.beginFrame();    scene.render();    // Present    engine.endFrame();    // Register new frame    BABYLON.Tools.QueueNewFrame(renderLoop);};BABYLON.Tools.QueueNewFrame(renderLoop);
Link to comment
Share on other sites

If you want to know the time consumed to render your scene, you have access to this function : 

myScene.getRenderDuration();

This function returns the time used to render the current scene (without any postprocess and without any actions registered with registerBeforeRender and registerAfterRender).

 

If you want to know the time used by the whole render process (with postprocessing, AND actions registered with registerBeforeRender  and registerAfterRender), then you can use the method 

myScene.getLastFrameDuration();

If I still don't understand your question (shame on me :) ), you can send me an MP (in english or french) and I will try to help you the best I can.

 

Cheers , 

Link to comment
Share on other sites

  • 2 years later...

I found that using scene.getLastFrameDuration(); caused movement slowdowns (when multiplying rotation and translations by it) near large objects. I use this instead and everything is smooth:

// Put this at the end of all the JavaScript, since it starts immediately.var clock = {    before: performance.now(),    getDelta: function () {        var now = performance.now()        var delta = now - this.before        this.before = now        return delta    }}
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...