Hey again...
I was bored the last 30 minutes, so I created a small system for timerbased movement. I was so frustrated, that it was finished with just 15 lines, that I created two additional, nearly senseless functions and a much bigger example.
I think the system is working and used in many games in a similar way (correct me if I am wrong.. I just thought this kind of timerbased movement etc. would make most sense?). It's very easy to use...
Like always you should call the init-function at the beginning of your program (tm_init()), and in the mainloop the updatefunction (tm_update()). This updatefunction returns a factor, which should be used to change your timerbased variables like speed etc. as shown in the example.
Please don't mark this thread as spam, maybe it helps some people, who weren't aware that such a method of speedcontrolling is existing...
Really, I just want to help!
Here the whole code with example:
rem Example-Code
set display mode 1024,768,32
sync on
sync rate 0
rem Speed-Decreasing-Values for this example. Decrease them to reach a higher speed, increase them for slower program
#constant Cubes = 1500
#constant Calculations = 200000
rem Create player
make object cube 1, 10
position camera 500,12,500
autocam off
rem Create world around
make matrix 1, 1000,1000, 20,20
rem Create many cubes around
for c = 2 to Cubes
make object cube c, 3
rotate object c, rnd(360),rnd(360),rnd(360)
position object c, rnd(1000),rnd(50),rnd(1000)
next c
rem Toggle-Variables
Toggle as boolean = 0
Visible as boolean = 1
rem Start Timerbased Movement-Functions
tm_init()
tm_SetMultiplicator(0.1)
rem Mainloop
do
rem Show info
set cursor 0,0
print "FPS: ", screen fps()
rem Controls
f# = tm_update()
control camera using arrowkeys 0, f#,0.5*f#
position object 1, camera position x(), camera position y(), camera position z()
set object to camera orientation 1
move object 1, 40
rem Show/Hide cubes
print "Press v to toggle cube-visibility"
if keystate(47)=1
if Toggle = 0
Toggle = 1
Visible = 1 - Visible
for c = 2 to Cubes
if Visible = 0
hide object c
else
show object c
endif
next c
endif
else
Toggle = 0
endif
rem do some time-intensive stuff if objects are visible
if Visible = 1
for x = 1 to Calculations
y = rnd(255)
next x
endif
sync
loop
rem End of example
remstart
Functions for timerbased movement
by Mr Kohlenstoff
How to use:
-Call tm_init() at the beginning of your program
-Define the Multiplicator by calling tm_SetMultiplicator or directly changing the variable tm_Factor
-Call tm_Update() one time in your mainloop, it returns the resulting factor, which should be mutliplicated
with all your speed-depending variables (like move-speed etc.)
-You can also get this factor by calling tm_GetFactor() or reading from the variable tm_Result (float)
remend
function tm_init()
global tm_Last as dword `<- timer-value of last update-function-call
global tm_Difference as integer `<- Difference between actual and last function-call in ms
global tm_Factor as float `<- Result-Speed-Value is multiplicated with this
global tm_Result as float `<- the resulting factor
global tm_timer as dword `<- stores the timer-value for exact calculations
tm_timer = timer()
tm_Last = tm_timer
tm_Factor = 1
endfunction
function tm_Update()
tm_timer = timer()
tm_Difference = tm_timer - tm_Last
tm_Last = tm_timer
tm_Result = tm_Difference * tm_Factor
endfunction tm_Result
function tm_SetMultiplicator(f#)
tm_Factor = f#
endfunction
function tm_GetFactor()
f# = tm_Result
endfunction f#
And here just the functions.
remstart
Functions for timerbased movement
by Mr Kohlenstoff
How to use:
-Call tm_init() at the beginning of your program
-Define the Multiplicator by calling tm_SetMultiplicator or directly changing the variable tm_Factor
-Call tm_Update() one time in your mainloop, it returns the resulting factor, which should be mutliplicated
with all your speed-depending variables (like move-speed etc.)
-You can also get this factor by calling tm_GetFactor() or reading from the variable tm_Result (float)
remend
function tm_init()
global tm_Last as dword `<- timer-value of last update-function-call
global tm_Difference as integer `<- Difference between actual and last function-call in ms
global tm_Factor as float `<- Result-Speed-Value is multiplicated with this
global tm_Result as float `<- the resulting factor
global tm_timer as dword `<- stores the timer-value for exact calculations
tm_timer = timer()
tm_Last = tm_timer
tm_Factor = 1
endfunction
function tm_Update()
tm_timer = timer()
tm_Difference = tm_timer - tm_Last
tm_Last = tm_timer
tm_Result = tm_Difference * tm_Factor
endfunction tm_Result
function tm_SetMultiplicator(f#)
tm_Factor = f#
endfunction
function tm_GetFactor()
f# = tm_Result
endfunction f#
If someone has ideas to improve the system (or knows that the system is completely useless because it has a general mistake I didn't notice so far.. who knows), just tell me, thanks.
Visit the DBPro - Speed up your game-Thread. http://forum.thegamecreators.com/?m=forum_view&t=88661&b=1