PDA

View Full Version : Fixed time step - game loop



basilisk
August 16th, 2010, 11:42 AM
Hi,
Guys i am having trouble implementing game loops in flash.I read this article i found on the net http://gafferongames.com/game-physics/fix-your-timestep/ (http://gafferongames.com/game-physics/fix-your-timestep/)
Here is my code in as3



//Original Code:

float t = 0.0f;
const float dt = 0.01f;

float currentTime = time();
float accumulator = 0.0f;

while (!quit)
{
float newTime = time();
float deltaTime = newTime - currentTime;
currentTime = newTime;

accumulator += deltaTime;

while (accumulator>=dt)
{
integrate(state, t, dt);
t += dt;
accumulator -= dt;
}

render(state);
}
//AS code
private var FPS = 50 / 1000;
private var current_time = getTimer()

mc.addEventListenerEvent.ENTER_FRAME,game)

private function game_loop(e:Event):void
{
if (FPS <= 0) throw new Error("INFINITE LOOP");
var _new_time:int = getTimer();
var _time_difference:Number = (_new_time - current_time);
_time_difference /= 1000;
current_time = _new_time;
excess_buffer_time += _time_difference;
while (excess_buffer_time >= FPS)
{
time_elapsed += FPS;
excess_buffer_time -= FPS;
updateState(time_elapsed,FPS);
}

renderVisualState();
actual_time += FPS
if (actual_time > 1)
{
removeEventListener(Event.ENTER_FRAME, game_loop);
}
}
What i dont undestand is how can i use t and dt to integrate and move objects? i.e say moving a car, acceleration,velocity etc. Can anybody help with regards to this?

bluemagica
August 16th, 2010, 12:28 PM
http://www.8bitrocket.com/2008/04/08/tutorial-creating-an-optimized-as3-game-timer-loop/

rumblesushi
August 16th, 2010, 01:04 PM
You multiply any force you apply by the timestep, with a variable timestep.

With a fixed timestep, you can just have all static variables, but that get updated at a constant value, ie every 16.67 milliseconds, even if you're rendering is happening at 30fps.

I don't have much experience with different types of timesteps, and using an accumulator, and interpolating between states etc.

However for Flash I honestly think a variable timestep is better. Better for performance, everything can happen in one loop, things don't need to get iterated over twice (if the rendering and physics are seperate) and if someone has a crap computer, the physics side of things aren't going to be updated on time anyway, so you're game is still going to slowdown or exhibit weird behaviour.

Variable timesteps are tricker than they first appear, I'm in the middle of optimising my variable timestep now.

Even Quake 3 for example, the legendary John Carmac himself didn't get it right with Quake 3, which had significantly different jumping force according to the framerate.

On a powerful machine, it sounds like a fixed timestep is better, because it has more stable physics basically, and you know that the physics are going to behave in the same way.

But for Flash which is an incredibly low powered platform, with radically different performance according to user's machines, I think a variable timestep is preferable, even if it can lead to physics behaving slightly differently.

Incidentally, type your ****ing variables man :)

basilisk
August 18th, 2010, 04:13 AM
Sorry bout the variables was posting from my mobile phone, so typing is a real pain :}. Anyways, i really need to seperate the updating part from rendering. like two seperate threads. I need to have same results everytime i would run the simulation, for eg: if object is supposed to move 50px in 1sec, then no matter wht fps it should move 50fpx.Even if it means using more CPU at point of time.
If anybody can help me out here, i will be gratefull. till then i better start reading more on how enterframe works. Also i thought bout using Timer class in as3, but by running few tests i found out tht even timer is somehow tied to enterframe, i.e rending pipeline in flash.

rumblesushi
August 18th, 2010, 11:18 AM
Using the fixed timestep method, you would have to use the timer/setInterval class. Simply set it to run at your desired timestep, ie 16.67 milliseconds for 60 frames a second.

And in keeping with the article and other fixed ts methods, you'll have to interpolate between states/play catchup etc if the simulation lags behind.

Why do you need exact precision, are you making a physics simulation?