Tymski

Using DeltaTime in Update(), usage of Tickers?

Recommended Posts

Hello!
I got quite confused after reading code for pixiJS examples and basics.

I have an update function in which I want to control my objects in such a way framerate doesn't affect things.
How do I declare deltaTime to do this?
I've seen in example one can use Ticker delta but I don't know how to use it in my loop.
 

function update() {
    someObject.move( deltaTime * someValue );
    renderer.render(stage);
    requestAnimationFrame(update);
}



Sorry for duplicating post, this editor is pretty weird. As I wanted to TAB to make space in my code it changed focus to Submit button.

Share this post


Link to post
Share on other sites

Hi Tymski. You can use the code below to calculate deltaTime. The newer versions of Pixi make it easier though. This link from the official examples shows minimal code to do what you need: Pixi Examples.

var lastTime = 0;
requestAnimationFrame(update);
function update(time) {
    var deltaTime = 0;
    if (lastTime) {
        deltaTime = time - lastTime;
    }
    lastTime = time;

    someObject.move(deltaTime * someValue);

    renderer.render(stage);
    requestAnimationFrame(update);
}

 

Share this post


Link to post
Share on other sites

I used Date.now() to get current time and saved it in a variable.
Difference between that variable and current time is the time between frames.
Divided that by 16.66666 (each frame should take 16.66666 ms to get 60 frames a second).
Thanks for the responses.

Share this post


Link to post
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...

  • Recently Browsing   0 members

    No registered users viewing this page.