Making my games independent of processing speed

Coders forum. Discuss techniques, swap code snippets, ask for help etc. relating to all forms of software development.
User avatar
piku
Sheepie Whore
Sheepie Whore
Posts: 20960
Joined: Wed Apr 24, 2002 10:44 am 0
Website: Http://Www.ncot.uk
Location: Wakefield
Contact:

Making my games independent of processing speed

Post by piku »

Are there any decent websites, or can someone explain to me how I can make my games run independent of the speed of the player's machine? I'm after the general theory and explanation, not a specific piece of code I can drop into my games.

Currently all code I write is fine on my machine, but when I give it to someone else they usually end up saying "it's great, but can you slow it down a bit my Quantum-threaded AMD machine makes it a bit too quick to play".

I've found various bits of websites that give a vague explanation and a wodge of code, but they don't seem to explain the theory behind it. It's one of those things that must be fairly simple given every game has to do it, but maybe it's so simple I'm missing the subtle point...

kthnksbye ;)
return (0);
User avatar
Mr Dom
Baaaaa!
Baaaaa!
Posts: 1375
Joined: Fri Dec 03, 2004 3:03 pm 0
Location: Cardiff

Post by Mr Dom »

From our code:

Code: Select all

// update timers
long startTime =  System::currentTimeMillis();
lastTime = CSGlobal::currentTime;
currentTime = startTime;
elapsedTime = startTime - CSGlobal::lastTime;
if(elapsedTime > 250)
	elapsedTime = 250;
else
	taLogicTime = (0.9f*taLogicTime + 0.1f*elapsedTime);
logicTime += (int)(taLogicTime+0.5f);

// update game
if (logicTime>250)	// max logic to 1/4 second in case of big stall
	logicTime = 250;
int first = 1;
totLogicTime = (logicTime / TARGET_TIME)*TARGET_TIME;
while (logicTime >= TARGET_TIME)
{
	game->tick(TARGET_TIME, first);
	first = 0;
	logicTime -= TARGET_TIME;
}

// draw game
// sleep for remaining time
startTime = TARGET_TIME - (System::currentTimeMillis() - startTime);
if (startTime < 0)
	startTime = 0;
Sleep(startTime);
and TARGET_TIME = 20 (in ms) for 50 fps logic
Basically it repeats the logic only when enough time has passed, and copes with fast & slow machines. The elapsed time is smoothed out to account for the 'sampling errors' from the interaction between refresh rates & logictime which gives much smoother updates.
Hope this helps,

:D
- Dom
Play Games
<Haircuts are for wimps!>
User avatar
piku
Sheepie Whore
Sheepie Whore
Posts: 20960
Joined: Wed Apr 24, 2002 10:44 am 0
Website: Http://Www.ncot.uk
Location: Wakefield
Contact:

Post by piku »

What's

Code: Select all

game->tick()
do, and what are the two parameters being passed in to it for?

Is this correct:

Each frame of your game has to complete before the next one is due, and this whole process needs to be done quickly enough that we see the end result as fluid animation?

So, if your game logic and screen drawing takes too long, the game slows down and framerate and responsiveness drops. If your machine is quicker than the game needs, the game runs too quickly and becomes unplayable.

So what you have to do is accept that on a slow machine the game is going to run slowly, and concentrate on making it not run out of control on fast machines. And to do that a delay needs calculating each frame, based on a desired framerate - "I want this to run at at least 30fps".

In that case, how is it that Doom might run at a smooth 30fps on my 486, but on your Pentium it runs at an equally smooth 57fps? And yet your player isn't running around the screen any quicker than mine? Is that where you somehow disconnect screen drawing from game logic?
return (0);
User avatar
RiK
Sheepie Whore
Sheepie Whore
Posts: 16092
Joined: Tue Apr 23, 2002 11:32 pm 0
XBL Gamertag: Dr Olpin
PSN: Dr Thumpy
Location: The Shire
Contact:

Post by RiK »

Piku, try a google for delta timing.
User avatar
radio
Baaaaa!
Baaaaa!
Posts: 1076
Joined: Mon Oct 20, 2003 11:34 am 0
Location: Brighton
Contact:

Post by radio »

I use something like:

Code: Select all

sysTime = systemTimeInMS();
timePassed = sysTime  - lastTime;
lastTime = sysTime;

objectPosition += ( (objectVelocity * baseRate) * timePassed);
You'll probbaly need to re-calculate all your speeds because at 30 fps, timePassed will be 0.0333334, or not very much at all.
If the machine has enough grunt to run at 60fps, then that time is half that, 0.016666667, so to move the same distance will take 2 frames instead of one, but because we're moving less each frame, the overall effect is of smoother movement.

that's how DOOM runs smooth at different framerates.
You need to edit lots of bits of code, compared to Mr Dom's example, but should allow your game to scale up and down more smoothly.


(Also in that example, I've got baseRate which I use to control the overall speed of the game, normally it's == 1, but if you set it to 0.5, everything moves at half speed, 'bullet time'-esque, set it to 2 and everything moves at benny-hill pace:))
Planting a flag in the moon doesn't make it yours, 'makes a hole in the moon.
//!
User avatar
Istar
Baaaaaa!
Baaaaaa!
Posts: 4869
Joined: Mon Apr 22, 2002 4:42 pm 0
Location: London, UK.

Post by Istar »

Let's not forget VSYNC. It's not perfect, but it has its uses.

I.
User avatar
PaulCunningham
Baaaaa!
Baaaaa!
Posts: 1218
Joined: Sun Jan 18, 2004 11:47 pm 0
Location: Walsall, England
Contact:

Post by PaulCunningham »

I do the Mr Dom method. Fixed logic / physics step and variable rendering.

Pretty easy to set up and gives the benefit of being able to easily do recordable demos (via saving inputs).

Here is an excellent post on Flipcode explaining the how and why.

http://www.flipcode.org/cgi-bin/fcartic ... show=63823

You'll need to interpolate between last and current frame when drawing to maintain smooth motion.
Cheers,
Paul Cunningham
Pumpkin Games
User avatar
the2bears
Baaaaaa!
Baaaaaa!
Posts: 3686
Joined: Wed Oct 27, 2004 9:30 pm 0
Location: The City by the Bay
Contact:

Post by the2bears »

RiK wrote:Piku, try a google for delta timing.
Avoid delta timing if the gameplay is 2D, go for a fixed frame rate. Easier to, have a fallback for slower machines (ie 60/30fps or 50/25fps).

Bill
the2bears - the indie shmup blog
User avatar
Mr Dom
Baaaaa!
Baaaaa!
Posts: 1375
Joined: Fri Dec 03, 2004 3:03 pm 0
Location: Cardiff

Post by Mr Dom »

piku wrote:What's

Code: Select all

game->tick()
do, and what are the two parameters being passed in to it for?
This calls our game to do a logic update - move stuff around etc.
The TARGET_TIME is the 'elapsed time' for a logic frame. Its like this as we have a define so we can change the logic time for different games.

The 'first' parameter lets the logic know that this is the first logic frame to process. We need this as the input isnt changed inside the game tick - so for example mouse clicks are only processed when 'first' is true.
piku wrote:Is this correct:
Each frame of your game has to complete before the next one is due, and this whole process needs to be done quickly enough that we see the end result as fluid animation?
Yes.
piku wrote: So, if your game logic and screen drawing takes too long, the game slows down and framerate and responsiveness drops. If your machine is quicker than the game needs, the game runs too quickly and becomes unplayable.
Usually its the drawing that stalls. For whatever the stall was, you have to do multiple logic updates. i.e. if we take 30ms to draw a frame, then each time round we do 2 logic updates for each draw.
piku wrote: So what you have to do is accept that on a slow machine the game is going to run slowly, and concentrate on making it not run out of control on fast machines. And to do that a delay needs calculating each frame, based on a desired framerate - "I want this to run at at least 30fps".
This copes with slow & fast just as well. The aim of this code is to give the same play experience on a wide range of PCs.
When its slow, we just run the logic (logic is usually much faster than rendering). If you cant process the logic fast enough, then increase TARGET_TIME.
piku wrote: In that case, how is it that Doom might run at a smooth 30fps on my 486, but on your Pentium it runs at an equally smooth 57fps? And yet your player isn't running around the screen any quicker than mine? Is that where you somehow disconnect screen drawing from game logic?
Yes.
The logic runs as though the game is running (for example) at 50fps all the time (thats what TARGET_TIME is for).The rendering is seperated so it draws at the speed the machine can cope with.
On fast machines the sleep at the end throttles the game back to 5fps, and on slower machines it runs the logic 50 times a second irrespective of the number of times we draw.

The code 'radio' posted is delta-timing. It works, but you have to make sure your logic is coded correctly. And it does not give fully 'deterministic' results - i.e. floating point error differences mean that a replay wont match the original.

The beauty of this system is that the ogic is fixed rate still so easier to code, you can build exact replays, and it runs on fast/slow machines perfectly.

Hope that all makes sense :D
<Blatant Plug>Feel free to try out any of the games on our site (link in sig.) Both the Java web games & the C++ downloads use this exact code to cope with PC speeds and we test on PCs ranging from 400 MHz PIIs up to Athlon64 3200s
- Dom
Play Games
<Haircuts are for wimps!>
User avatar
Flabio
Baaaaaaa!
Baaaaaaa!
Posts: 8717
Joined: Wed Mar 03, 2004 12:37 pm 0
Location: Guildford

Post by Flabio »

Istar wrote:Let's not forget VSYNC. It's not perfect, but it has its uses.
Bad coder. BAD.

Thats the reason most Japanese games end up with manky PAL conversions...

Ever tried porting a game locked to a 60Hz or 30Hz vsync? I've lost entire months of my life to it...


Anyway, the way it's done on most games I've worked on is to decouple the render from the game logic.

We process all the game logic once every x milliseconds (in BW2, its every 10). The renderer can go however fast it likes. The game logic is then nicely deterministic because it's always running at a fixed update rate. You do then have to use delta timing in your animation and movement, which you get around by having a logical (game side) position for the object, and an interpolated render side position.
Last edited by Flabio on Wed Jan 18, 2006 8:05 pm 0, edited 1 time in total.
Becky Rose
Baaa!
Baaa!
Posts: 114
Joined: Thu Jul 28, 2005 12:44 pm 0

Post by Becky Rose »

after the general theory and explanation, not a specific piece of code I can drop into my games.
Written for DBP and B3D, this tutorial explains the concepts pretty well. The float# being faster thing is pretty much a DBP only quirk though as far as I can work out: http://www.bansheestudios.com/stuff/tim ... ement.html
User avatar
teamonkey
Sheepie Whore
Sheepie Whore
Posts: 12082
Joined: Thu Jun 13, 2002 1:07 am 0
XBL Gamertag: itsthetaste
PSN: teamonkey_ca
Contact:

Post by teamonkey »

the2bears wrote:
RiK wrote:Piku, try a google for delta timing.
Avoid delta timing if the gameplay is 2D, go for a fixed frame rate. Easier to, have a fallback for slower machines (ie 60/30fps or 50/25fps).
I'd avoid it if you're going for 3D too. Fixed logic is just so much easier for physics.
User avatar
piku
Sheepie Whore
Sheepie Whore
Posts: 20960
Joined: Wed Apr 24, 2002 10:44 am 0
Website: Http://Www.ncot.uk
Location: Wakefield
Contact:

Post by piku »

teamonkey wrote:
the2bears wrote:
RiK wrote:Piku, try a google for delta timing.
Avoid delta timing if the gameplay is 2D, go for a fixed frame rate. Easier to, have a fallback for slower machines (ie 60/30fps or 50/25fps).
I'd avoid it if you're going for 3D too. Fixed logic is just so much easier for physics.
Yes, from the stuff I've managed to find (which was actually found by searching for "delta timing" :) ) physics simulations tend to go a little mental if their concept of time keeps changing.

What about putting my display code in a seperate thread? The physics code can then be locked to a specific speed and the display code can go and run off as fast as it can.
return (0);
User avatar
20thCenturyBoy
Baaaaa!
Baaaaa!
Posts: 1093
Joined: Fri Oct 01, 2004 8:16 am 0
Location: Perth, Australia

Post by 20thCenturyBoy »

Download the free Popcap framework from [url]developer.popcap.com[/url] and read the demo source code, which is very well commented (it's C++ which I think you use). They use a 100Hz Update function where your logic goes. It's called 100 times a second regardless of machine speed. The Draw() function itself is called at a variable rate depending on machine speed. There is also an UpdateF() function which takes a float as a parameter. This is used to compensate for different monitor refresh rates to reduce jittery sprite movement (most of their games are sprite based 2D). I'm not explaining it very well, their code comments make more sense...
User avatar
the2bears
Baaaaaa!
Baaaaaa!
Posts: 3686
Joined: Wed Oct 27, 2004 9:30 pm 0
Location: The City by the Bay
Contact:

Post by the2bears »

piku wrote:
teamonkey wrote:
the2bears wrote:
RiK wrote:Piku, try a google for delta timing.
Avoid delta timing if the gameplay is 2D, go for a fixed frame rate. Easier to, have a fallback for slower machines (ie 60/30fps or 50/25fps).
I'd avoid it if you're going for 3D too. Fixed logic is just so much easier for physics.
Yes, from the stuff I've managed to find (which was actually found by searching for "delta timing" :) ) physics simulations tend to go a little mental if their concept of time keeps changing.

What about putting my display code in a seperate thread? The physics code can then be locked to a specific speed and the display code can go and run off as fast as it can.
I'm a bit out of my league, not having done much in the way of 3D, but I do feel single-threaded is better. Excepting networking of course. I just tthink that complete determinism might be the way to go. I'd like to hear other thoughts though, those with more experience.

Bill
the2bears - the indie shmup blog
Post Reply