PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Sync game speed using frame rate ratio ?

I want to sync my game speed using Warmi's formula. Am I doing it right?

Code:

//FLOAT nFrameRate = 0;
//m_timer.GetActualFrameRate(&nFrameRate;);

int GameSpeedX = 0;
int DEF_TARGET_FPS = 40;
FLOAT nActualFrameTime = 0;

m_timer.GetActualFrameTime(&nActualFrameTime;);

int FrameRateRatio = int( DEF_TARGET_FPS/nActualFrameTime);

GameSpeedX += (GameSpeedX*FrameRateRatio);




I cant find a good function to round off the value.
I'm not familiar with Warmi's stuff, so I'm probably off base here, but why would you take 40 FPS and divide it by a number of milliseconds(assuming that nActualFrameTime is milliseconds)?

That would give you frames/Sec/Sec. Unless your target is actually 40 ms, and you read the actual ms, then you get a ratio that is unitless.

Then use that as a linear adjustment to any iterative things you're doing...

i.e.
DeltaX += Velocity * (correctionfactor);
Round by adding 0.5 before casting to int. (Only works on positive numbers and won't round 0.5 to even as statisticians prefer.)
fast_rx: here's the thread http://www.pocketmatrix.com/forums/viewtopic.php?t=12685&start;=0

Pls make the formula clear to me. Thanks.
ok... I've briefly looked through that other thread. I wasn't off topic.

Here's what I use in my game:

//this is in a class, but could be in your main game loop or whatever
Code:
static unsigned long CurrentTick;
static unsigned long LastTick=GetTickCount();
static unsigned long DeltaTick;


CurrentTick = GetTickCount();

if(ResetTimerCount)
{
ResetTimerCount=0;
LastTick=CurrentTick;

}

DeltaTick = CurrentTick-LastTick;
LastTick = CurrentTick;

TimeCorrection = (DeltaTick)/33.0f; //target 30 FPS



Not very elagant, and it uses floating point, but it works.

My previous post was incorrect in that I wasn't calculating the "Delta" time for the correction factor.

Does this make sense? Just as in the other thread... if you had something moving at a velocity of 1 on a device running at 30 FPS, then it would adjust to 1*(20.0/33.3) for a device running at 50 FPS...

Be sure not to get your frame time (in milliseconds) confused with Frames per second... As in the example above, 20 mS = 50 FPS and 33.3 mS = 30 FPS...
Some other things I just thought about. They may interest you or anyone else reading...

IF you use integers for your iterated values, you'll be thowing out small (less than 1, obviously) parts of your movement... And since it's iterated dozens of times (hopefully) per second, the errors can really add up. I'd suggest either using floats or at least fixed point floats in order to minimize the error.

Another important thing to consider is that we really don't know what devices will be out in a couple of years... so, ensure you code so that really, really fast devices don't cause a problem. In the previous message, I suggest taking the ratio of the delta time to the desired delta. On a super fast machine, that will approach zero. And if you add in the round off errors of int's or even floats for that matter, you may end up with things not moving at all...

A good way to check to see if your code is working is to compile it in debug (runs slow) and then check it with a full optimized build. They should be almost identical in the timing of things happening, right?

And lastly, I mentioned that this works fine for linearly tweaked values (i.e. constant speed for instance). But in my game Lejjo (http://lejjo.4t.com), I am iterating a mass-spring system. This caused a problem because there was a point where if my iterations were too large, the spring-mass system equations would go crazy. The fix was to check to see if I was below a minimum update rate, and if not, use multiple smaller iteration time slices.

If any of you made it through all that, I congratulate you.
Don't scare him :-)
90% of games , especially on PPC, don't even need to worry about Euler simulations and potential instabilities.

Fake it - that's my advice :-)
Thanks Fast and Warmi :-) I dont worry right now about the incoming machines since I may end up re writing my games anyway.

Btw, warmi are you using integer in your movements?

eg int gamespeed = 0;

gamespeed = gamespeed + 1 ?
I use floats and fixed point exclusively.

Integers are useless for this kind of stuff ( well, you could scale them but that is no different than using fixed point. )
ok so it's like this FLOAT gameSpeed = 5.0; But when you check collision detection do u still round off to int? so many questions...
Yeah.
While it is not 100% correct - who cares, nobody will be able to tell the difference.

BTW. You can download the source code for ZRaid at:
http://www.warmi.net/pocketpc/zraid/
I use float exclusively. So far so good. I'd like to switch to fixed point, it compiles but I have equation issues in my collision detection that I can't be bothered to debug right now, so that remains a task for the future.