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
On 4/29/2017 at 2:17 PM, Tymski said:

Can you provide code example?

app.ticker.add(this.update, this);

function update(detla) {

//delta will be passed through in this callback

}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.