Codesnippets > Math

Constant Game Speed independent of Variable FPS

(1/5) > >>

bigsofty:
Here is my take on deWitters famous game loop code. It makes for a smoother game, runs you code at same speed on any platform too.

This removes the need for your program to update its logic at the same time as the frame is rendered (why check for a key press at 60 fps for example). Also, your code does not need to wait till a frame is rendered to continue. Another advantage is predicted interpolation, this means for much smoother graphics with a more powerful platforms or "best fit" with low powered platforms. On top of that you can choose at what frame speed your game runs at, without worrying about the speed the graphics are being rendered at, great for physics simulations for example. The chances are that if you have seen a really smooth game running on different platforms, then it uses code like this.

About the demo:

The demo code is based on a small code listing from here, http://www.glbasic.com/forum/index.php?topic=8425.msg71420#msg71420
I simply converted it, to show you how a traditional gameloop piece of code would be converted to run with independent game logic. Its not the best example (the juddering when the two boxes meet is because of the original code) but I did not want to change it further to add confusion

Important: Set "Maximum Frame rate [hz]" to -1 in your project options

Also on some GFX cards you may have to "Vertical Sync" off if it is forced on in your driver options.


Read This: http://www.koonsolo.com/news/dewitters-gameloop/

This explains it better than I ever could.


New Game Loop Lib:

--- Code: (glbasic) ---// --------------------------------- //
// Project: gameloop (Ian Thompson - Sept 12)
// Start: Wednesday, September 05, 2012
// IDE Version: 11.001

// Based on "deWiTTERS Game Loop" ( see http://www.koonsolo.com/news/dewitters-gameloop/ )

GLOBAL  interpolation#

FUNCTION GameLoop:
    CONSTANT TICKS_PER_SECOND% = 30 // Runs at 30fps
    CONSTANT SKIP_TICKS% = 1000 / TICKS_PER_SECOND
    CONSTANT MAX_FRAMESKIP% = 5

    LOCAL next_game_tick% = GETTIMERALL()
    LOCAL loops%

    LOCAL game_is_running% = TRUE
    WHILE( game_is_running )

        loops% = 0
        WHILE( GETTIMERALL() > next_game_tick% AND loops < MAX_FRAMESKIP%)
            Update()

            next_game_tick = next_game_tick + SKIP_TICKS
            INC loops
        WEND

        interpolation# = ( GETTIMERALL() + SKIP_TICKS - next_game_tick ) / SKIP_TICKS

        Render()
    WEND
ENDFUNCTION


FUNCTION Interpolate#: cur#, old#, interp#
RETURN old# + (cur# - old#) * interp#
ENDFUNCTION
--- End code ---


Demo:


--- Code: (glbasic) ---// --------------------------------- //
// Project: Constant Game Speed independent of Variable FPS demo (Ian Thompson - Sept 12)
// Start: Wednesday, September 05, 2012
// IDE Version: 11.001

GLOBAL x#=100,Ox#
GLOBAL y#=100,Oy#
GLOBAL angle#=0
GLOBAL speed#=4

GLOBAL mx#,my#,Omx#,Omy#,b1,b2

SETSCREEN 640,480,0

WHILE TRUE
GameLoop()
WEND

// These two routines are the basis of your new game loop

// Only for game logic updating
FUNCTION Update:
LimitMouse()
MoveObjectToMouse()
ENDFUNCTION

// Only for drawing
FUNCTION Render:
DRAWRECT Interpolate(mx,Omx,interpolation#),Interpolate(my,Omy,interpolation#),32,32,RGB(255,0,0) // Mouse#
DRAWRECT Interpolate(x,Ox,interpolation#),Interpolate(y,Oy,interpolation#),32,32,RGB(255,255,255) // Object
PRINT "Rendering at "+getfps()+" FPS",10,10
SHOWSCREEN
ENDFUNCTION

FUNCTION LimitMouse:
Omx=mx;Omy=my // Cache anything that moves(or scales, rotates) before they change for frame by frame interpolation.
MOUSESTATE mx, my,b1, b2
IF mx<0 THEN mx=0
IF mx>640-32 THEN mx=640-32
IF my<0 THEN my=0
IF my>480-32 THEN my=480-32
// SETMOUSE mx, my
ENDFUNCTION

FUNCTION MoveObjectToMouse:
Ox=x;Oy=y // Cache anything that moves(or scales, rotates) before they change for frame by frame interpolation.
angle = ATAN(my - y, mx - x)
x=x+COS(angle)*speed
y=y+SIN(angle)*speed
ENDFUNCTION


//Frames per sec calc
FUNCTION getfps:
STATIC fps_time,fps_counter,fps,fps_temp
    fps_time = GETTIMERALL()
    fps_counter = fps_counter + 1
        IF (fps_time-fps_temp)>1000
            fps_temp = fps_time
            fps = fps_counter
fps_counter = 0
        ENDIF
    RETURN fps
ENDFUNCTION

// Orginal frame based code ( see http://www.glbasic.com/forum/index.php?topic=8425.msg71420#msg71420 )
//GLOBAL x#=100
//GLOBAL y#=100
//GLOBAL angle#=0
//GLOBAL speed#=20
//
//GLOBAL mx#,my#,b1,b2
//
//SETSCREEN 640,480,0
//
//WHILE TRUE
//
// PRINT mx,10,10
// PRINT my,10,20
//
// LimitMouse()
//
// DRAWRECT mx,my,32,32,RGB(255,0,0) // Mouse#
// DRAWRECT x,y,32,32,RGB(255,255,255) // Object
//
// MoveObjectToMouse()
//
// SHOWSCREEN
//
//WEND
//
//FUNCTION LimitMouse:
// MOUSESTATE mx, my,b1, b2
// IF mx<0 THEN mx=0
// IF mx>640-32 THEN mx=640-32
// IF my<0 THEN my=0
// IF my>480-32 THEN my=480-32
// SETMOUSE mx, my
//ENDFUNCTION
//
//FUNCTION MoveObjectToMouse:
// angle = ATAN(my - y, mx - x)
// x=x+COS(angle)*speed
// y=y+SIN(angle)*speed
//ENDFUNCTION

--- End code ---

I hope this of use to someone.


Ian



[attachment deleted by admin]

r0ber7:
The article is very interesting. Got me thinking...
Right now, the benefits of implementing this in my game are negligable. However, if I am to write a multiplayer mode, it might reduce the network traffic. Sending a packet every frame is unnecessary and might cause problems on slower connections. This solution could improve multiplayer performance, no? Hmm...

Thanks for the read. Bookmarked. :)

Slim:
Hi bigsofty,

Thanks for this code.

bigsofty:
Your very welcome.

Since the game timing is no longer dependant of a fixed frame rate then I think networking should be easier, knowing that all the various clients are running at the same speed.

One strange unexpected thing I found was that I had a generally faster game with this technique. The amount of time that I was spending repeatedly doing the same task unnecessary was reduced, freeing up more time for interpolated rendering or other tasks. The update() will steal back time from the rendering if it needs the CPU time, this will be pretty unnoticeable though due to the interpolation smoothing this transition out.

EDIT: BTW I built this using V11 beta, I think the -1 project option is able on V10?

erico:
This looks so great! Must bookmark, even though it is going to be a couple years before I funnly understand. :good:
Thanks!

Navigation

[0] Message Index

[#] Next page

Go to full version