10.03.2019 - Round 1 results of our "A Love Letter For FreeBASIC" game dev competition have been published. Please be sure to check the results thread: http://games.freebasic.net/forum/index.php?topic=629.0. Don't forget that the competition is continuing with a round 2, lasting till 29th of April, 300 USD first prize. Stay tuned!

Author Topic: Time-based movement with an example (need your feedback)  (Read 6035 times)

Lachie Dazdarian

  • Double dipper
  • Administrator
  • Forum Sage
  • *****
  • Posts: 1308
    • Yahoo Instant Messenger - lachie13
    • View Profile
    • The Maker Of Stuff
    • Email
Time-based movement with an example (need your feedback)
« on: January 20, 2008, 11:21:47 AM »
This is a piece of code by Pritchard that should illustrate time-based movement.

Code: [Select]
#include "fbgfx.bi"
screenres 640, 480, 16

'' initialize variables - UDTs are better but this is shorter
dim as integer x, y
dim as double timerDif, timerCur = timer, timerLast = timer

dim as double box_size, StartTime, EndTime, ResultTime

box_size = 9
StartTime = - 2
x = 50
y = 200

screenlock

do
 
  '' IO here
    if multikey(FB.SC_LEFT) then x -= 200 * timerDif
    if multikey(FB.SC_RIGHT) then x += 200 * timerDif
    if multikey(FB.SC_UP) then y -= 200 * timerDif
    if multikey(FB.SC_DOWN) then y += 200 * timerDif
 
  '' simple animation connected with the timerDif
    box_size += 20 * timerDif
    IF box_size > 20 THEN box_size = 9
 
  '' graphics rendering here
    line (x, y) - step (box_size, box_size), rgb(255, 255, 255), bf
   
  '' time to pass the screen calculation related
  '' variables.
   
    '' If you pass the left edge of the screen reset
    '' StartTime to -1.
    IF x <= 0 AND StartTime = - 2 THEN StartTime = -1
    '' If you get back on the screen from the left
    '' side record the StartTime.
    IF x >= 0 AND StartTime = -1 THEN StartTime = timer
    ' If you pass the right edge of the screen
    ' record EndTime and the ResultTime. Lock the
    ' timer until the user passes the left edge
    ' of the screen again.
    IF x >= 640 AND StartTime > 0 THEN
        EndTime = timer
        ResultTime = EndTime - StartTime
        EndTime = 0
        StartTime = -2
    END IF
   
    Locate 1, 1
    Print "Time to pass the screen (left to right): "; ResultTime
 
  '' equivalent to a page flip
    screenunlock
    sleep 1, 1
    screenlock
    cls
 
  '' frame update here
    timerLast = timerCur
    timerCur = timer
    timerDif = (timerCur - timerLast)
 
loop until multikey(FB.SC_ESCAPE)
screenunlock

I implemented a test in it where the program records the time it took you to pass the screen from left to right. To test it, pass the left edge of the screen and then travel right until you pass the right edge.

I get times around 3.13 with different SLEEP amounts. You?
« Last Edit: January 20, 2008, 02:00:29 PM by Lachie Dazdarian »
"Things like Basic and Free Basic provide much-needed therapy and a return to sanity and a correct appreciation of people. The arrogant folk really hate a word like 'Basic' - fine, and good riddance." ~ pragmatist

SSC

  • Recruit
  • **
  • Posts: 32
  • R[a]wr
    • MSN Messenger - smithcosoft@yahoo.com
    • AOL Instant Messenger - smithcosoft
    • View Profile
    • SmithcoSoft Creations
    • Email
Re: Time-based movement with an example (need your feedback)
« Reply #1 on: January 20, 2008, 01:56:33 PM »
I tried this on my desktop and laptop, the results varried quite a bit.

With sleep at default of 1 ms i got
24.97 on desktop
7.71 on laptop

With sleep at 5 ms I got
4.03 on desktop
3.52 on laptop

I also noticied that timedif was higher on my desktop for some reason which is what threw it off so much

Oh and my desktop is about 10 to 20 times faster than my laptop.

Lachie Dazdarian

  • Double dipper
  • Administrator
  • Forum Sage
  • *****
  • Posts: 1308
    • Yahoo Instant Messenger - lachie13
    • View Profile
    • The Maker Of Stuff
    • Email
Re: Time-based movement with an example (need your feedback)
« Reply #2 on: January 20, 2008, 02:03:15 PM »
Hmm, any ideas what am I'm doing wrong?
"Things like Basic and Free Basic provide much-needed therapy and a return to sanity and a correct appreciation of people. The arrogant folk really hate a word like 'Basic' - fine, and good riddance." ~ pragmatist

hexdude!

  • Global Moderator
  • Novice
  • *****
  • Posts: 67
    • ICQ Messenger - 283467208
    • MSN Messenger - Malatestas_Ghost@hotmail.co.uk
    • AOL Instant Messenger - hhexadecimaldude
    • Yahoo Instant Messenger - jabrazavi
    • View Profile
    • Email
Re: Time-based movement with an example (need your feedback)
« Reply #3 on: January 20, 2008, 02:09:32 PM »
i get consistent times, ive tried sleep times from 1 to 500 ms, and allways get about 3 seconds

Dr_D

  • Forum Sage
  • *****
  • Posts: 257
    • Yahoo Instant Messenger - dr_davenstein
    • View Profile
    • Dr_D's apps
    • Email
Re: Time-based movement with an example (need your feedback)
« Reply #4 on: January 20, 2008, 03:15:57 PM »
Here's the most simple example I could muster up. People seem to think that doing this complicates things, or makes the program slower. Well, it adds a few extra calculations, so it actually does make the program slightly slower, but if it makes the game playable on 10 more computers this way, then it's worth it. That software pacman3d thing I did is playable even at like 8 FPS, and it's only because of this time based movement.


Arrow keys to add velocity
Q to increment delay between frames
A to decrement delay between frames
ESC to exit!  :o

Code: [Select]
#include "fbgfx.bi"
screenres 320,240,32

dim as double loop_time, this_time=timer, x, y, xdir, ydir, speed, sleep_time = 1

'nothing important here... just for the direction.
xdir  = 1
ydir  = 1


speed = 100 'units per second
'"desired_movement_per_second*loop_time" is what this uses



do
    loop_time = timer-this_time
    this_time = timer
    sleep sleep_time,1
   
    if multikey(FB.SC_Q) then
        sleep_time*=1.1
    end if
   
    if multikey(FB.SC_A) then
        sleep_time*=.9
    end if
   
    if multikey(FB.SC_UP) then
        ydir-=loop_time*5
    elseif multikey(FB.SC_DOWN) then
        ydir+=loop_time*5
    else
        ydir += ((-ydir*.995)*loop_time)
    end if
   
    if multikey(FB.SC_LEFT) then
        xdir-=loop_time*5
    elseif multikey(FB.SC_RIGHT) then
        xdir+=loop_time*5
    else
        xdir += ((-xdir*.995)*loop_time)
    end if
   
   
    x+=speed*xdir*loop_time
    y+=speed*ydir*loop_time
   
    if x<5 then
        x=5       
    end if
   
    if y<5 then
        y=5
    end if
   
    if x>314 then
        x=314
    end if
   
    if y>234 then
        y=234
    end if
   
    screenlock
    cls
    circle(x,y),5,&HFFFFFF
    screensync
    screenunlock
   
loop until multikey(FB.SC_ESCAPE)

btw: i need to be something other than amoeba! hmmm... how about, grandmaster pimp-daddy!
« Last Edit: January 20, 2008, 03:20:04 PM by Dr_D »

Lachie Dazdarian

  • Double dipper
  • Administrator
  • Forum Sage
  • *****
  • Posts: 1308
    • Yahoo Instant Messenger - lachie13
    • View Profile
    • The Maker Of Stuff
    • Email
Re: Time-based movement with an example (need your feedback)
« Reply #5 on: January 20, 2008, 04:41:25 PM »
Quote
btw: i need to be something other than amoeba! hmmm... how about, grandmaster pimp-daddy!

Oh, then just keep posting. :P

Anyway, nice example. I'm just not sure how much different in principle it is from Pritchard's code, except in the fact your features the speed variable. Of course, your example also features acceleration so the very acceleration needs to be connected with loop_time too.
"Things like Basic and Free Basic provide much-needed therapy and a return to sanity and a correct appreciation of people. The arrogant folk really hate a word like 'Basic' - fine, and good riddance." ~ pragmatist