TGC Codebase Backup



REAL Timer based movement by Jaze

15th Dec 2003 20:18
Summary

REAL Timer based Movement Loop



Description

This simple little demo is the ground work I'm using for making timer() based movement in my games or whatever. Run the Snip, turn right a little - and see the white square slowly
move away from the other squares. (Even when your framerate...like mine...was 300fps!!!)

Extra work? Yes.. benefit? Same Gameply on any computer - and the better the machine - the smoother the GFX. (In my opinion)

Some say 30fps or so is all human can see.... but slamming higher rates means your EYES can create the BLUR factor because your "scene" moves smooth.... can get "blurs" (according to your eyes) with this kind of set up... and its amazingly smooth.

REMEMBER: The camera is there just as a quick way to navigate - the camera is not
timer based in this example - though the structure is designed to handle moving cameras
the same way it does objects so it could be made to move the same way the "cube" in this example does... by using the velocity amount. You'd just have to make a record in "arR()" that is iType=cnCamera - and set the camera id=0 for it to control the main one :)


I didn't translate what VELOCITY means in metric - just how much to move per millisec - so depending on what you are "creating" the velocity that is appropriate for your project may be leaps and bounds different for another scene or project. Hope this is helpful. I saw another TIMER codebase entry that was nothing more than a HARDCODED delay - "Don't do nothing until computer counts to 100...." which will count to 100 faster on a fast machine and slower on a slow machine - hence - your game would appear to go as fast as computer wished - not how you wished it to behave. If you only care about running it on your own machine... than that kind of timer is fine I guess. IF you want people to try your stuff out and have it run the same to them as it does on your machine... well... This is the way to go.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Rem Project: Current
Rem Created: 12/15/03 6:47:56 PM

Rem ***** Main Source File *****
#constant cnTypeEmpty 0
#constant cnTypeObject 1
#constant cnTypeImage 2
#constant cnTypeSound 3
#constant cnTypeSprite 4
#constant cnTypeCamera 5
` Record of Type "E"ntity
type rtE
  id ` it needs its own id :)
  iType ` cnTypeObject cnTypeImage cnTypeSound cnTypeSprite
  x# ` Coords
  y#
  z#
  xo# ` Offset
  yo#
  zo#
  xv# ` Velocity or SPEED per MILLISECOND in DB 3D Space
  yv#
  zv#
  xa# ` Angles
  ya#
  za#
  iLastTime
  iElapsedTime
  iMediaID ` Like a sprite's source image id
endtype
#constant cnMaxE 50
dim arE(cnMaxE) as rtE ` "A"rray of "R"ecord type "E"
global giTimer ` at beginning of Loop used for time calcs cuz globals are faster


` Test it out
for t = 1 to 30
  make object cube t, 1
  are(t).id = t
  are(t).iType=cnTypeObject
  are(t).x#= rnd(20)
  are(t).y#= rnd(20)
  are(t).z#= rnd(20)
  are(t).xo#=0 ` Offset
  are(t).yo#=0
  are(t).zo#=0
  are(t).xv#=0 ` Velocity or SPEED per MILLISECOND in DB 3D Space
  are(t).yv#=0
  are(t).zv#=0
  are(t).xa#=0 ` Angles
  are(t).ya#=0
  are(t).za#=0
  are(t).iLastTime=0
  are(t).iElapsedTime=0
  are(t).iMediaID=0 ` Like a sprite's source image id
next t

are(1).xv#=0.001 ` Set Velocity per MilliSec
set object light are(1).id, 0
set object emissive are(1).id, rgb(255,0,0)

` Main Loop
sync on: sync rate 0 ` Fast as possible :)
repeat
  control camera using arrowkeys 0,.1,1
  for t = 1 to cnMaxE
    giTimer = TimeR()
    if are(t).iLastTime = 0 then are(t).iLastTime = giTimer
    are(t).iElapsedTime = giTimer - are(t).iLastTime
    are(t).iLastTime = giTimer
    ` All entities can have motion via x#,y#,z# and xv#, yv#, zv#
    if (are(t).xv#<>0) or (are(t).yv#<>0) or (are(t).zv#<>0)
      are(t).x# = are(t).x# + (are(t).xv# * are(t).iElapsedTime)
      are(t).y# = are(t).y# + (are(t).yv# * are(t).iElapsedTime)
      are(t).z# = are(t).z# + (are(t).zv# * are(t).iElapsedTime)
    endif
    select are(t).iType
    case cnTypeEmpty:
    endcase
    case cnTypeObject:
      position object are(t).id, _
         are(t).x#, _
         are(t).y#, _
         are(t).z#
      rotate object are(t).id, _
         are(t).xa#, _
         are(t).ya#, _
         are(t).za#
    endcase
    case cnTypeImage:
    endcase
    case cnTypeSound:
      position sound are(t).id, _
         are(t).x#, _
         are(t).y#, _
         are(t).z#
    endcase
    case cnTypeSprite:
      sprite are(t).id, _
         are(t).x#, _
         are(t).y#, _
         are(t).iMediaID
      rotate sprite are(t).id, are(t).xa#
    endcase
    case cnTypeCamera:
      position camera are(t).id, _
         are(t).x#, _
         are(t).y#, _
         are(t).z#
      rotate camera are(t).id, _
         are(t).xa#, _
         are(t).ya#, _
         are(t).za#
    endcase
    endselect
  next t
  set cursor 1,1
  print "FPS:", screen fps()
  sync
until escapekey()