Diffrent speeds

Blitz3D Forums/Blitz3D Beginners Area/Diffrent speeds

Terry B.(Posted 2008) [#1]
So I have a Lan airplane game I have been working on for about a week, when I realized that If I run it and put the airplanes to the same speeds, one goes faster then the other. I think its because one of the computers is faster. Is there a way to get around this?


Knight #51(Posted 2008) [#2]
The function "Delay" right where your game loop starts. Set it as you please. This will restrict the loop from iterating until that set time is met. Note that faster computers can not speed up a "Delay".


Hope that helps ;o)


Terry B.(Posted 2008) [#3]
I tried delaying it depending on the FPS I wanted, but doesnt work if the second computer is too slow, you cant delay a negative number.


Matty(Posted 2008) [#4]
Don't use delay,

what you are after is either frame rate limiting or something like 'delta timing' or rendertweening.

Basically you want your game to run at the same rate on any machine.
I will explain delta time in a little detail as I know that one better than either of the other two methods.

Each frame calculate how long, in milliseconds, the frame took to complete. Assuming you want a frame to take a certain amount of time eg 30 fps would equate to 1000 ms / 30 fps = 33 ms per frame.

Okay so you have your desired frame rate at 33 ms per frame (although you can set it to whatever you want), so now you want to adjust all your movement and animations so that if the game runs at say 20fps (which would equate to 50 ms per frame) you would want o increase all the movement and animations by an appropriate amount. Alternatively if it runs very fast at 50fps for example you want to reduce the distances and animation speeds on a per-frame basis to account for this also.

At start of game loop:

we have : DesiredFrameTime = 33 (for example)
ActualFrameTime = x (calculated from length of previous frame)
Then we need our 'Delta' value:
Delta = ((ActualFrameTime+1) / DesiredFrameTime)
'Delta' needs to be a floating point variable, and so should usually be calculated with "Float(ActualFrameTime+1)/Float(DesiredFrameTime)" to ensure the maths works out ok.
Now technically the +1 shouldn't be there but it's something I use to account for the chance that a frame may take 0 milliseconds. I know this is not strictly the right way of doing it but it will do in most cases)


Now we have our 'Delta' value.

Either make it a global variable or pass it directly to any function that performs movement.

Then,

when you are moving your aircraft simply do this:

MoveEntity 0,0,AircraftSpeed * Delta

And for animating entities, at the updateworld call updateworld with the Delta passes as a parameter.

Hope that is of some use.


Zethrax(Posted 2008) [#5]
As Matty said, you need to use some method of regulating the logic framerate of your game.

Delta timing is a popular option, but personally I don't recommend it. It's not a set-and-forget solution, and is very fiddly, and very easy to get wrong. Implementing it incorrectly can lead to some hard to trackdown bugs.

Render tweening is a better solution, although it has some issues which need to be taken into account. The main issue is that if you move something over a large distance, then ghost images of it will appear between its starting and ending point. The solution for this is to HideEntity the item before the move, and ShowEntity it after the move is done. You may need to experiment to get it right.

You can find tutorials on both of these methods in the tutorials areas on this forum.

You can find an example of rendertweening in the Blitz3D program folder at: C:\Program Files\Blitz3D\samples\mak\castle