Quantcast
Channel: Question and Answer » game-loop
Viewing all articles
Browse latest Browse all 30

is this approach correct to make my game framerate indipendent in javascript?

$
0
0

this is what i did

my game loop is run every REFRESH milliseconds

loop = setInterval(tick,REFRESH);

in the loop i calculate how much time is passed since last tick and subdivide that for how much time i expected to be passed

var now = performance.now();
var elapsedTime = now - lastTick;
var delta = time / REFRESH;
lastTick = now;

and every time based action that should happen every tick is “scaled” with the delta factor,
for example my movement code

var distance=ball.dest.x-ball.x;
var movement=distance/speed;
ball.x+=movement;

will have movement multiplied with delta


Viewing all articles
Browse latest Browse all 30

Trending Articles