hashi Posted April 3, 2018 Share Posted April 3, 2018 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; }); }); Quote Link to comment Share on other sites More sharing options...
Antriel Posted April 4, 2018 Share Posted April 4, 2018 This is best: https://gafferongames.com/post/fix_your_timestep/ You can't tell how often will requestAnimationFrame fire, so you should interpolate your positions. Quote Link to comment Share on other sites More sharing options...
hashi Posted April 4, 2018 Author Share Posted April 4, 2018 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. Quote Link to comment Share on other sites More sharing options...
Antriel Posted April 4, 2018 Share Posted April 4, 2018 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. Quote Link to comment Share on other sites More sharing options...
hashi Posted April 4, 2018 Author Share Posted April 4, 2018 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 Poland here. Quote Link to comment Share on other sites More sharing options...
Antriel Posted April 4, 2018 Share Posted April 4, 2018 In that case it doesn't really matter, whichever is easiest. When doing it during physics loop you might save yourself additional iteration, but that's negligible so really whichever you like more. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.