REAL Timer based movement by Jaze15th 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 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() |