game timing schemes

BlitzMax Forums/BlitzMax Programming/game timing schemes

Robert Cummings(Posted 2005) [#1]
Hiya,

I realise it's been covered before but I would like to know if anyone's done more research into this?


rdodson41(Posted 2005) [#2]
Strict

Global fps:Float = 60
Global time:Float = 1000 / fps

Local timer:Float = Millisecs() + time

While(Not Keyhit(1))
    Local ms:Float = Millisecs()
    If(ms >= timer)
        timer = ms + time
        'Update()
        'Render()
    Endif
Wend

Try a simple test, like replacing Update() and Render() with Print("Hello World!") and make fps 1.


Hotcakes(Posted 2005) [#3]
I'm just using Max's default timing system and to hell with it. =]


Will(Posted 2005) [#4]
I like frame-rate independent timing. Sushimastas example works great when the game is running nice and fast - but lets say some process boots up in the background and slows it down, the frame-rate decreases - and with it so does the game speed! The idea behind independent timing is someone can be kicking it at 60FPS, and when something in the background pushes it to 30 fps, the actual game still runs at the same speed. This is the method I prefer.


Global deltaT#, lasttime = MilliSecs()

While Not(KeyHit(key_escape))
	DeltaT = Float(MilliSecs() - lasttime)/1000.0; lasttime = MilliSecs()
	
	game.update()
	game.draw()
	
	Flip; Cls; FlushMem
Wend

' An example moving a player

player.xVelocity = player.xVelocity + player.xAcceleration * DeltaT
player.x = player.x + player.xVelocity * DeltaT




TartanTangerine (was Indiepath)(Posted 2005) [#5]
Yeah we did, we posted a big piece on here with our Delta Timing code. Some people like it some don't. I've made my choice and it works great for me.


Robert Cummings(Posted 2005) [#6]
Thanks everyone

The one thing I've always been confused about with delta timing is when to apply it.

For example...

velx = velx + (mouseXspeed() * 0.25) * deltaT
x = x + velx * deltaT
velx = velx * 0.02 * deltaT

Is this correct? the first line creates my velocity, the second adds it to my position and the third dampens it again. Just an example, but it shows where I get confused by adding deltaT to everything in sight!

Some do's and don'ts for delta would be very welcome!


Robert Cummings(Posted 2005) [#7]
I'll assume that you use delta time with everything...?


tonyg(Posted 2005) [#8]
That'd probably be wise.


GW(Posted 2005) [#9]
IMO Delta time gets really messy when games get complicated..
I use a modified version of the code Mark posted long ago.
Has anyone experienced problems with that?


Robert Cummings(Posted 2005) [#10]
Whats that code - do you have it to hand so I can take a look please?


Dreamora(Posted 2005) [#11]
I don't like the way mark is doing it (tweening for "smooth" movement) ...
it calculates millions of times a frame before displaying it which costs lots of useless cpu time which I can use for other things (because tweening and timed coding / event based code don't like each other while delta time works)


Robert Cummings(Posted 2005) [#12]
start a lobby, I'm sure it'll help.


Will(Posted 2005) [#13]
Here are a few great reasons to use DeltaT - you can freeze your game! slow down and speed up time!

Just add if keydown(key_space) then deltaT = deltaT * .1 and you run everything at 1/10th the time! This is enormously useful for me as a development tool. Being able to watch something that is fast in the final game in slow motion as I develop is great. You can also just set DeltaT to 0 and freeze time. I also use it for varying time between objects - if the player gets a speed boost powerup I can modify deltaT before they update, and set it back before the ai updates.

Its very easy to use and know where to add the DeltaTs if you practice it, and I think it has enormous advantages over locked framerate timing.


Robert Cummings(Posted 2005) [#14]
Thats a fantastic idea. You're really winning me over :)
Yep it's probably a case of practising it quite a bit till you get used to where it goes...


Ferminho(Posted 2005) [#15]
I use Will's method too from some time ago for the same reason; I think game speed should be independant on framerate...

That way you can easily distort time, too, adding a percent multiplier to the 'time that has passed in last loop'.

I use a bit more complex version for my game and I'm getting good results.


Robert Cummings(Posted 2005) [#16]
this recommends I do fixed time step and deltatime as well - can you explain why?

http://www.gaffer.org/articles/Timestep.html


Shambler(Posted 2005) [#17]
Because

1) Your game runs at the same speed on all computers

2) Your physics simulation runs in accurate enough steps so that it doesn't 'explode'...stops things like missing collisions etc.


Dreamora(Posted 2005) [#18]
With procentual factor / delta timing, you have just to make sure, that you "catch" 0 divisions and 0 like divisions which ends in infinite values or errors. If thats ensured, then they normally work great.

I even do procentual factor + timed rendering which gives me for fixed 60 FPS several 10000 mainloop runs for other usefull things than rendering or wait for rendering.


Warren(Posted 2006) [#19]
Sorry to resurrect this thread, but I stumbled over it while searching for timing information. Just wanted to say that if you guys are struggling with fixed timing, consider buying my Time module - it makes it all pretty painless. Here's some sample code that demonstrates how fixed time stepping would work:

http://www.pixelfist.com/samplecode.php?modname=time&id=2


H&K(Posted 2006) [#20]
http://www.blitzbasic.com/codearcs/codearcs.php?code=1721
Or just us my code entry, which is free


Warren(Posted 2006) [#21]
Sure, if the free one does everything you need.


Grey Alien(Posted 2006) [#22]
well if we're selling stuff ... my framework uses fixed rate logic + delta time for ultra smooth stuf and it does much much more...


H&K(Posted 2006) [#23]
WE ARE NOT SELLING STUFF.

@Warren,

No, my free one does hardly anything. It is in fact just a wrapper for the blr timer function. The reason I posted it, was because I dont have the power to BAN you for blatent forum trawling in aid of advertising your time module.

This is the second time this week you have posted just to advertise, and I personaly find it unaceptable. Post in the tools setction, post in the showcase section, at a push join in current threads but do not reserect threads just to advertise.

If you must, put a big advert as your sig, alla Grey.

@Grey,

I know you are taking the piss, but stop it. (Mind you, you are no better sometimes)


Grey Alien(Posted 2006) [#24]
lol, I know, sorry. Well at least I don't create "zombie" threads ;-) Anyway, I try to do it subtly (most of the time) and people do find my framework useful and keep on buying it...


Warren(Posted 2006) [#25]
H&K

Sorry to offend. If a moderator wants to delete these posts, that's cool.


Grey Alien(Posted 2006) [#26]
they won't delete them, it's not that big a deal. H&K just thinks he's the forum police, despite having a shady past himself ;-)


H&K(Posted 2006) [#27]
Once he was on the Dark Side, But thanks to the love of a beautiful cup of coffee, he now fights on the Side of Justice he is Heroic and Knowing man. ;)


Chroma(Posted 2006) [#28]
Personally I use the delta timing code. Works great and makes sure everything is happening at the same time on all breeds of computers.