Posted: 24th Oct 2003 18:19
Some code that will help you write games that run at same speed all pc's. What may run ok'ish on your pc may run too fast on a super-duper computer.

Should be ok for 2d and 3d games.

Simply gosub the init routine before main loop and gosub other routine as first thing in main loop.

The idea being that routine counts how many loops are run every second and modifies move# accordingly. It updates once per second for smoothness sake, but you can change t9 if you want it to update more regularly.

Have included some basic objects to show how to use move#

You can therefore force objects to move a certain amount in 1 second, rotate a set number of degrees in 1 second.

Enjoy!

+ Code Snippet
sync on : sync rate 0 : autocam off

` make some objects
make matrix 1,1000,1000,50,50
make object sphere 1,50 : position object 1,500,0,600 : set object wireframe 1,1
make object cube 2,40
make object cube 3,10 : position object 3,400,5,700

position camera 500,20,400
x#=0

gosub init_timer

do
   gosub timer_stuff

   ` player moves 40 units in 1 second and rotates 90 degrees in one second
   control camera using arrowkeys 0,40.0*move#,90.0*move#

   ` rotate sphere so it rotates 180 degrees in 1 second
   turn object left 1,180.0*move#

   ` bob cube up and down so it takes 1 second
   x#=wrapvalue(x#+(360.0*move#))
   position object 2,550,50.0*sin(x#),600

   ` make object chase you at rate of 20 units per second
   point object 3,camera position x(),camera position y(),camera position z()
   xrotate object 3,0
   move object 3,20.0*move#

   text 0,20,get time$()
   text 0,0,str$(screen fps())
   sync
loop

init_timer:
` t9 is how often to change speed (in milliseconds)
t1=timer() : t9=1000
return

timer_stuff:
` don't change any of this!
t2=timer() : inc t3
if t2-t1 >= t9
   move#=((t2-t1)/1000.0)/t3 : t1=t2 : t3=0 : t4=1
else
   if t4=0 then move#=((t2-t1)/1000.0)/t3
endif
return
Posted: 24th Oct 2003 21:51
Very useful - I've modified the t1-t9 variables for my program so they're in a structure.
Posted: 24th Oct 2003 22:15
My naming of variables is very dodgy! I get a good ear-bending at work because all my work programs written in php, coldfusion, etc are usually like a,b,x1,x2,e.t.c instead of nice descriptive names.

It's just that I'm lazy. Why call a variable playerxposition when I can just say x

It's only when I take time off and my workmates have to modify one of my programs that it really hits the fan!

Anyway people can change the variables to whatever they want, stick em in a structure. Make em global and use functions instead. Change t9 to be a constant, blah blah blah
Posted: 24th Oct 2003 22:46
It bet you used to have a C64...
Posted: 24th Oct 2003 22:51
Nah, spectrum 48k, spectrum+3, amiga 500, amiga 1200.

Work computer used to be DEC PDP-11 and because screen was only 80 characters wide (green on black), it made sense to use small variable names. As I was writing most of programs I made programs as illegible as possible so they couldn't sack me! Seems to work as I am in same just 15 years later!
Posted: 25th Oct 2003 1:25
Whatever works!