Jump to content

Update gfx positions. After move or in outer loop?


hashi
 Share

Recommended Posts

Assum we use physic engine like box2d or p2.js. And both graphic and physic updating are aprox 60 fps.

What is better?

1. Update gfx position right after physic body moved?

GameObject.prototype.integrate = function() {
    this.body.integrate(); //body has changed his position according to physic simulation

    this.gfx.x = this.body.x;
    this.gfx.y = this.body.y;
}

2. or in separeted loop?

requestAnimFrame(() => {

    world.eachObject((obj) => {
        obj.gfx.x = obj.body.x;
        obj.gfx.y = obj.body.y;
    });

});

 

Link to comment
Share on other sites

Antriel, you have misunderstood my question. I have already fixed timestep full of accuratness.

The question is about better update gfx position right after body changed position or in outer loop(eachObject/Body) in for example requestAnimationFrame.

Link to comment
Share on other sites

It seems to me that maybe you misunderstood what interpolating positions means. Fixed timestep for physics is great, but that means you are out of sync with render rate. You want to update gfx positions inside the RAF loop, but you can't just set them to what physics tells you it's at at that time. Or well, you can, but then you are limiting yourself to whatever frequency your physics runs (not to mention frame pacing issues).

The optimal solution is to do what that article explains and update position within your reneder loop by interpolating: `State state = currentState * alpha + previousState * ( 1.0 - alpha );` which would translate to something like `obj.gfx.x = obj.body.prevX + (obj.body.x - obj.body.prevX) * alpha;`

I once talked about this approach here too:

 

But if you don't want to interpolate, then it doesn't really matter where you update the positions.

Link to comment
Share on other sites

All i do in RAF for now is only rendering, not setting positions.

I wonder which is better, updating gfx position right after single body position changed. Or after whole physic step (all bodies changed position) with additional outer loop. Which is better design.

BTW. hello friend from neighborhood :D Poland here.

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...