Speed on a 60Hz or 85Hz monitor

Blitz3D Forums/Blitz3D Beginners Area/Speed on a 60Hz or 85Hz monitor

Active03(Posted 2006) [#1]
Hi,

I want to make a 2d game.
But my game runs faster on a computer using 85Hz than on a 60Hz monitor.

Is there a way 2 make the game run at the same speed?


JBR(Posted 2006) [#2]
timer = CreateTimer(60)

While Not KeyDown(1)
WaitTimer(timer)
; do your stuff
flip
wend


jfk EO-11110(Posted 2006) [#3]
Using CreateTimer is one method. You may combine this with tweening. Personally I'd rather suggest to use a simple and proper Delta timing.

Delta works like this: It doesn't matter how fast the monitor refreshes, all moves, animations etc. are multiplied by a "elapsed-time" factor. So the higher the monitor rate, the smaller the factor.

You would use a hypothetic rate of 60Hz for a factor of 1.0. with 30 Hz it would then be 2.0, at 85 Hz it's: (60/85)...
The factor can be calculated easily.

sample:
mystep#= 0.7
t=millisecs()-17

while not keydown(1) ; mainloop
 ; determine elapsed ms / 16.667 ( is 1.0 at 60hz)
 old_t=t
 t=millisecs()
 delta#=float(t-old_t)/16.667
 ; controls, eg:
 if keydown(200) then moveentity player,0,0,mystep# * delta#
 ; ...
 updateword(delta#)
 renderworld()
 flip
wend


The good thing is you don't have to alter the animation speed of animated meshes, Updateworld allows to use delta directly.

There is one exception where you don't have to use delta, that's when you're already working with realtime related values, returnd (example given) by MouseXSpeed etc.


Active03(Posted 2006) [#4]
Thnx 4 your reply.

Can i use renderworld in a 2d game ?
I don't want to use 3d, its all 2d grafix.

I will try it when i get home.


Matty(Posted 2006) [#5]
Same logic applies in a 2d game, except you obviously don't need renderworld or updateworld, just flip.


jfk EO-11110(Posted 2006) [#6]
For plain 2D there are some problems with both methods. you will get smooth movement of sprites only when you are using integer steps (eg. x=x +2). If you use the described correction codes, x would maybe incremented like this: x=x + 1.32. so the sprite would move only every so and so-th time, resulting in jerky motion. You may decide to staticly sync your game to 60 Hz and please the user in the readme to set the monitor to 60 Hz for ultimate fun. If he plays at 85 HZ, it will only run to fast. There has been some code to force a new frequency, but I think that's really unfriendly to the user, and even dangerous for the hardware.

a solution for this dilemma is to use virtual 2D using 3D Sprites. They can be moved by steps smaller than 1.0. This system also allows a lot of powerfull things like fast rotation and scaling, as well as semitransparent things and additive blend modes for pyro fx etc.

There are several libraries for virtual 2D, some of them offer a list of commands that are replacing their 2D counterparts.


WolRon(Posted 2006) [#7]
Here's some delta-timing code:
http://myweb.arvig.net/rwolbeck1/programmingtutorial/reference/framelimiter.htm

Check out the rest of the website too.