locked
How To Create Simple Gravity for a Game RRS feed

  • Question

  • Hello, 

    I am creating a game that has similar game play mechanics to Zock77's side-scroller. The main part I am trying to perfect is the jumping mechanic. How do I create a gravity effect after I initiate a jump with the space bar? I realize that I am trying to manipulate the y coordinate based on location and when the character reaches a pre-defined height then the gravity takes place and allows the character to fall down. Using my basic understanding of programming, I've come to a point where my character can jump but it's not accurate and feels rigid. This becomes a problem when I want to make my character to jump while running. When the character jumps while running, the character jumps in more of a rigid direction rather than float in the air. Can any one point me to the right direction with equations or reliable resources or any samples?

    p.s Just gravity. I don't need to focus on air friction or any other factors. I don't intend on mastering the physics. I just want to understand gravity from a mathematical perspective so I could program and manipulate the gravity with a better understanding.


    • Edited by Ezra94 Tuesday, September 9, 2014 8:21 PM Reword
    Tuesday, September 9, 2014 8:17 PM

Answers

  • Simple fire work.

    GraphicsWindow.BackgroundColor="Black"
    GraphicsWindow.Width=1200
    GraphicsWindow.Height=700 
    GraphicsWindow.BrushColor="Yellow"
    sc=shapes.AddEllipse(40,40)
    While "True"
      Y=750
      ddx= 100-math.GetRandomNumber(200)                        '  Launch  direction
      X=200*math.GetRandomNumber(3) +math.GetRandomNumber(200)  '  Launch place
      Shapes.Move(sc,X-15,Y)
      Gravity=9.8
      t0=Clock.ElapsedMilliseconds
      For L=1 To 52
        X=X+ddx/50
        Y=Y-30 + Gravity*Math.Power(Clock.ElapsedMilliseconds-t0,2)/2/1000000
        Shapes.Move(sc,X-15,Y)                                '  Launching
        Shapes.Zoom(sc,1-L/75,1-L/75)                         '  Scale down
        Program.Delay(80)
      EndFor
    EndWhile

    • Marked as answer by Ezra94 Thursday, September 11, 2014 1:18 AM
    Wednesday, September 10, 2014 4:11 AM
    Answerer
  • Here is the simple way of doing gravity:

    Grav =  3200
    Ball = Shapes.AddEllipse(10,10)
    YStart = 20
    While 1 = 1
      Program.Delay(5)
      Time = Time + 0.002
    
      y = (Grav /2 ) * (Time * Time) + YStart
      
      Shapes.Move(Ball,100,y)
      EndWhile


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    • Marked as answer by Ezra94 Thursday, September 11, 2014 1:18 AM
    Wednesday, September 10, 2014 2:47 PM
    Answerer
  • I know you said you didn't want the physics, but ...

    velocity (V) is rate of change of position (Y) therefore : Ynew = Ylast + V

    Acceleration (A) is rate of change of velocity (V) therefore : Vnew = Vlast + A

    Gravity is an acceleration down (positive in SB graphics)

    • To jump V is set to velocity up (negative)
    • Gravity acceleration (A) updates V each step : V = V+A
    • Position (Y) is updated each step : Y = Y+V
    • When the object returns to its starting position it lands and (V = 0, A = 0)


    So you need to store Y and V, updating them both in a loop as examples above show.

    • Marked as answer by Ezra94 Thursday, September 11, 2014 1:18 AM
    Wednesday, September 10, 2014 9:40 PM
  • I originally made the physics for this game, So i might be able to help you out :D

    intialize()
    
    'Main Loop
    While "True"
      frameStart = Clock.ElapsedMilliseconds
      
      MoveBall() 
      Bounce() 
      'TextWindow.WriteLine("y: " + y)
      'TextWindow.WriteLine("dy: " + dy) 
      'TextWindow.PauseWithoutMessage()
      controlBall()
      FrameDelay()
    Endwhile
    
    Sub intialize
      GraphicsWindow.Top = Desktop.Height / 2 - GraphicsWindow.Height / 2 - 50
      GraphicsWindow.left = Desktop.Width / 2 - GraphicsWindow.Width / 2
      GraphicsWindow.BrushColor = "black"
      GraphicsWindow.fillRectangle(0,GraphicsWindow.Height-3,GraphicsWindow.Width,3)
      
      GraphicsWindow.KeyDown = OnKeyDown
      GraphicsWindow.KeyUp = OnKeyUp
      
      x = GraphicsWindow.Width / 2 
      y = GraphicsWindow.Height - 10 
      dx = 0
      dy = -3.2
      delay = 15
      
      GraphicsWindow.PenWidth = 1
      GraphicsWindow.PenColor = "black"
      GraphicsWindow.BrushColor = "red"
      ball = Shapes.AddEllipse(10,10)
    EndSub
    
    Sub MoveBall
      'Update the location of the ball based on user input and current velocity
      x = x + dx
      y = y + dy 
      Shapes.Move(ball, x,y)
      dy = dy + 0.05 'Difference interval (moving upward and downward) 
      
      'Wrap past the edges
      If (x > GraphicsWindow.Width) Then
        x = 0
      ElseIf (x < 0) Then
        x = GraphicsWindow.Width
      EndIf
    EndSub
    
    Sub Bounce
      If (y > GraphicsWindow.height - 10) Then
        dY = 0',-3.2 'Height of bounce 
        CanBounce = "True"
      EndIf
    EndSub
    
    Sub controlBall
      If (keyDown["Right"]) Then 
        dx = 1 
      ElseIf (keyDown["Left"]) Then 
        dx = -1
      ElseIf (keyDown["Space"]) Then 
        If CanBounce = "True" Then
          dy = -3.2
          CanBounce = "False"
          EndIf
      EndIf 
    EndSub
    
    Sub FrameDelay
      While Clock.ElapsedMilliseconds - frameStart < delay
      EndWhile
      frameStart = Clock.ElapsedMilliseconds
    EndSub
    
    Sub OnKeyDown
      lastKey = GraphicsWindow.LastKey 
      keyDown[lastKey] = "True"
    EndSub
    
    Sub OnKeyUp 
      lastKey = GraphicsWindow.LastKey 
      keyDown[lastKey] = "False"
    EndSub 
    

    Now it will only bounce when you press spacebar


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    • Marked as answer by Ezra94 Thursday, September 11, 2014 5:30 PM
    Thursday, September 11, 2014 4:27 PM
    Answerer
  • Secondly, do you know how the " frameDelay " subroutine works? I don't understand how an empty loop affects the program. 

    The Clock.ElapsedMilleseconds changes every millisecond. The frameStart is subtracted and if the result becoms bigger than delay, the while-loop exits. Until then the while keeps repeating.


    Jan [ WhTurner ] The Netherlands

    • Marked as answer by Ezra94 Friday, September 12, 2014 2:45 PM
    Friday, September 12, 2014 9:21 AM
    Answerer
  •  By the way, how did you figure out the physics for this game? Did you eye it? Do you have background knowledge in physics? Did you just approached it from a logical view point? 

    I was 12 at the time, So I kind of Guessed from a logical point of view :D

    My Thoughts were that I needed to move the ball at a certain speed, and then increase that speed.

    So I started with something like this:

    Ball = Shapes.AddEllipse(10,10)
    x = 200
    y = 50
    dy = 0
    
    While 1 = 1 
      Program.Delay(5)
      dy = dy + 0.05
      y = y + dy 
      Shapes.Move(Ball,x,y)
      EndWhile

    And then Moved on from there.

    Also notice that although this isn't actually a physics formula, (Which would be: Y=(G/2)*(T^2) ) You can use it as one:

    Ball = Shapes.AddEllipse(10,10)
    x = 200
    y = 300
    dy = -3.2
    dx = 3
    While 1 = 1 
      Program.Delay(5)
      dy = dy + 0.05
      y = y + dy 
      x = x + dx
      Shapes.Move(Ball,x,y)
      EndWhile

    Basically all you have to do to get the ball to jump, Is to set "Dy" to -3.2

    Hope this helps!


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    • Marked as answer by Ezra94 Friday, September 12, 2014 5:46 PM
    Friday, September 12, 2014 3:24 PM
    Answerer
  • The Clock.ElapsedMilliseconds returns the number of millseconds since januari 1st, 1900

    Every time the program executes the command, it returns the value as read from the computerclock. If you set Framestart equal to the output from Clock.ElapsedMilliseconds and 50 milliseconds later you calculate Clock.ElapsedMillisecond - Framestart you get a value slightly larger than 50.

    This is a real number, with two digitsbehinf the decimal sign.


    Jan [ WhTurner ] The Netherlands

    • Marked as answer by Ezra94 Friday, September 12, 2014 6:40 PM
    Friday, September 12, 2014 5:16 PM
    Answerer
  • Yes, the difference is that Program.Delay will leave the cpu free for other things during the delay, but the continuous While loop will keep using cpu doing 100s or 1000s of loops using up cpu cycles.

    So best practice - if you can calculate what delay you want then call Program.Delay.

    • Marked as answer by Ezra94 Friday, September 12, 2014 7:42 PM
    Friday, September 12, 2014 6:51 PM
  • The code is fine, but I would change

    While Clock.ElapsedMilliseconds - frameStart < delay
    EndWhile

    to below for the reasons in my last post (sparing cpu cycles).

    msDelay = delay - (Clock.ElapsedMilliseconds-frameStart)
    If (msDelay > 0) Then
      Program.Delay(msDelay)
    EndIf

    Comments are just that and may not be so clear - the code is.  All the comments mean is that the more time spent doing presses etc, then the delay will be shorter to keep the frame rate roughly constant - i.e. just delay long enough to keep the frame rate constant.

    If we have a desired frame rate of 50 fps, then delay is 20ms.  If the action calculations, graphics update etc take 8 ms (Clock.ElapsedMilliseconds-frameStart), then we need to pause for 12ms before starting the next frame.


    • Marked as answer by Ezra94 Friday, September 12, 2014 8:10 PM
    • Edited by litdev Friday, September 12, 2014 8:10 PM typo
    Friday, September 12, 2014 7:52 PM

All replies

  • Simple fire work.

    GraphicsWindow.BackgroundColor="Black"
    GraphicsWindow.Width=1200
    GraphicsWindow.Height=700 
    GraphicsWindow.BrushColor="Yellow"
    sc=shapes.AddEllipse(40,40)
    While "True"
      Y=750
      ddx= 100-math.GetRandomNumber(200)                        '  Launch  direction
      X=200*math.GetRandomNumber(3) +math.GetRandomNumber(200)  '  Launch place
      Shapes.Move(sc,X-15,Y)
      Gravity=9.8
      t0=Clock.ElapsedMilliseconds
      For L=1 To 52
        X=X+ddx/50
        Y=Y-30 + Gravity*Math.Power(Clock.ElapsedMilliseconds-t0,2)/2/1000000
        Shapes.Move(sc,X-15,Y)                                '  Launching
        Shapes.Zoom(sc,1-L/75,1-L/75)                         '  Scale down
        Program.Delay(80)
      EndFor
    EndWhile

    • Marked as answer by Ezra94 Thursday, September 11, 2014 1:18 AM
    Wednesday, September 10, 2014 4:11 AM
    Answerer
  • Here is the simple way of doing gravity:

    Grav =  3200
    Ball = Shapes.AddEllipse(10,10)
    YStart = 20
    While 1 = 1
      Program.Delay(5)
      Time = Time + 0.002
    
      y = (Grav /2 ) * (Time * Time) + YStart
      
      Shapes.Move(Ball,100,y)
      EndWhile


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    • Marked as answer by Ezra94 Thursday, September 11, 2014 1:18 AM
    Wednesday, September 10, 2014 2:47 PM
    Answerer
  • I know you said you didn't want the physics, but ...

    velocity (V) is rate of change of position (Y) therefore : Ynew = Ylast + V

    Acceleration (A) is rate of change of velocity (V) therefore : Vnew = Vlast + A

    Gravity is an acceleration down (positive in SB graphics)

    • To jump V is set to velocity up (negative)
    • Gravity acceleration (A) updates V each step : V = V+A
    • Position (Y) is updated each step : Y = Y+V
    • When the object returns to its starting position it lands and (V = 0, A = 0)


    So you need to store Y and V, updating them both in a loop as examples above show.

    • Marked as answer by Ezra94 Thursday, September 11, 2014 1:18 AM
    Wednesday, September 10, 2014 9:40 PM
  • I wouldn't mind using physics. Before using the physics though, I would like to look at a particular program by codingCat. Here is the code BCL208-6.

    What attracted my attention was the gravity (the feel) when the ball jumps in the game (I'm not sure if codingCat used physics). It floats and falls. It has a natural feel. Also, the player can move the ball while it's in mid air. This is the kind of jump simulation I am trying to recreate. 

    Here is the code that represents just the jumping aspect of the program: 

    intialize()

    'Main Loop
    While "True"
      frameStart = Clock.ElapsedMilliseconds
      
      MoveBall() 
      Bounce() 
      'TextWindow.WriteLine("y: " + y)
      'TextWindow.WriteLine("dy: " + dy) 
      'TextWindow.PauseWithoutMessage()
      controlBall()
      FrameDelay()
    Endwhile

    Sub intialize
      GraphicsWindow.Top = Desktop.Height / 2 - GraphicsWindow.Height / 2 - 50
      GraphicsWindow.left = Desktop.Width / 2 - GraphicsWindow.Width / 2
      GraphicsWindow.BrushColor = "black"
      GraphicsWindow.fillRectangle(0,GraphicsWindow.Height-3,GraphicsWindow.Width,3)
      
      GraphicsWindow.KeyDown = OnKeyDown
      GraphicsWindow.KeyUp = OnKeyUp
      
      x = GraphicsWindow.Width / 2 
      y = GraphicsWindow.Height - 1 
      dx = 0
      dy = -3.2
      delay = 15
      
      GraphicsWindow.PenWidth = 1
      GraphicsWindow.PenColor = "black"
      GraphicsWindow.BrushColor = "red"
      ball = Shapes.AddEllipse(10,10)
    EndSub

    Sub MoveBall
      'Update the location of the ball based on user input and current velocity
      x = x + dx
      y = y + dy 
      Shapes.Move(ball, x,y)
      dy = dy + 0.05 'Difference interval (moving upward and downward) 
      
      'Wrap past the edges
      If (x > GraphicsWindow.Width) Then
        x = 0
      ElseIf (x < 0) Then
        x = GraphicsWindow.Width
      EndIf
    EndSub

    Sub Bounce
      If (y > GraphicsWindow.height) Then
        dY = -3.2 'Height of bounce 
      EndIf
    EndSub

    Sub controlBall
      If (keyDown["Right"]) Then 
        dx = 1 
      ElseIf (keyDown["Left"]) Then 
        dx = -1
      EndIf 
    EndSub

    Sub FrameDelay
      While Clock.ElapsedMilliseconds - frameStart < delay
      EndWhile
      frameStart = Clock.ElapsedMilliseconds
    EndSub

    Sub OnKeyDown
      lastKey = GraphicsWindow.LastKey 
      keyDown[lastKey] = "True"
    EndSub

    Sub OnKeyUp 
      lastKey = GraphicsWindow.LastKey 
      keyDown[lastKey] = "False"
    EndSub 


    I am currently trying to rewrite this code so the ball doesn't jump by itself. Instead, I want to initiate this kind of jump with the space key. I haven't had any luck so far but I'll keep you posted.

    Thursday, September 11, 2014 1:18 AM
  • I originally made the physics for this game, So i might be able to help you out :D

    intialize()
    
    'Main Loop
    While "True"
      frameStart = Clock.ElapsedMilliseconds
      
      MoveBall() 
      Bounce() 
      'TextWindow.WriteLine("y: " + y)
      'TextWindow.WriteLine("dy: " + dy) 
      'TextWindow.PauseWithoutMessage()
      controlBall()
      FrameDelay()
    Endwhile
    
    Sub intialize
      GraphicsWindow.Top = Desktop.Height / 2 - GraphicsWindow.Height / 2 - 50
      GraphicsWindow.left = Desktop.Width / 2 - GraphicsWindow.Width / 2
      GraphicsWindow.BrushColor = "black"
      GraphicsWindow.fillRectangle(0,GraphicsWindow.Height-3,GraphicsWindow.Width,3)
      
      GraphicsWindow.KeyDown = OnKeyDown
      GraphicsWindow.KeyUp = OnKeyUp
      
      x = GraphicsWindow.Width / 2 
      y = GraphicsWindow.Height - 10 
      dx = 0
      dy = -3.2
      delay = 15
      
      GraphicsWindow.PenWidth = 1
      GraphicsWindow.PenColor = "black"
      GraphicsWindow.BrushColor = "red"
      ball = Shapes.AddEllipse(10,10)
    EndSub
    
    Sub MoveBall
      'Update the location of the ball based on user input and current velocity
      x = x + dx
      y = y + dy 
      Shapes.Move(ball, x,y)
      dy = dy + 0.05 'Difference interval (moving upward and downward) 
      
      'Wrap past the edges
      If (x > GraphicsWindow.Width) Then
        x = 0
      ElseIf (x < 0) Then
        x = GraphicsWindow.Width
      EndIf
    EndSub
    
    Sub Bounce
      If (y > GraphicsWindow.height - 10) Then
        dY = 0',-3.2 'Height of bounce 
        CanBounce = "True"
      EndIf
    EndSub
    
    Sub controlBall
      If (keyDown["Right"]) Then 
        dx = 1 
      ElseIf (keyDown["Left"]) Then 
        dx = -1
      ElseIf (keyDown["Space"]) Then 
        If CanBounce = "True" Then
          dy = -3.2
          CanBounce = "False"
          EndIf
      EndIf 
    EndSub
    
    Sub FrameDelay
      While Clock.ElapsedMilliseconds - frameStart < delay
      EndWhile
      frameStart = Clock.ElapsedMilliseconds
    EndSub
    
    Sub OnKeyDown
      lastKey = GraphicsWindow.LastKey 
      keyDown[lastKey] = "True"
    EndSub
    
    Sub OnKeyUp 
      lastKey = GraphicsWindow.LastKey 
      keyDown[lastKey] = "False"
    EndSub 
    

    Now it will only bounce when you press spacebar


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    • Marked as answer by Ezra94 Thursday, September 11, 2014 5:30 PM
    Thursday, September 11, 2014 4:27 PM
    Answerer
  • This works really well. I haven't observed the code yet because I want to try to figure it out. Also, I still want to tweak it so I can move the ball freely and the ball doesn't bounce when the program starts. By the way, how did you figure out the physics for this game? Did you eye it? Do you have background knowledge in physics? Did you just approached it from a logical view point? I tried the latter but wasn't able to produce the same results. I know that I am trying to manipulate the y coordinate of the ball based on its location. No matter how hard I tried though, I couldn't get the feel when the ball falls. From studying the code of the original program, I learned that you added 0.05 to " dy ", change in " y ", after each loop, which in turn affected " y ". That was the key to create the floating effect. Thus, the ball would gradually slow down as it goes up and increase speed when falling. 
    Thursday, September 11, 2014 5:51 PM
  • Okay, I managed to rewrite the code so the ball can be moved with the arrow keys. Also, I made sure that the player can't hold the space button to keep jumping. I would like to note two things. One, I know there is probably more efficient way to rewrite this code but I think this one (and yours) will serve as suitable prototypes. Secondly, do you know how the " frameDelay " subroutine works? I don't understand how an empty loop affects the program. 

    intialize()
    
    'Main Loop
    While "True"
      frameStart = Clock.ElapsedMilliseconds
      
      MoveBall() 
      Bounce() 
      controlBall()
      FrameDelay()
    Endwhile
    
    Sub intialize
      GraphicsWindow.Top = Desktop.Height / 2 - GraphicsWindow.Height / 2 - 50
      GraphicsWindow.left = Desktop.Width / 2 - GraphicsWindow.Width / 2
      GraphicsWindow.BrushColor = "black"
      GraphicsWindow.fillRectangle(0,GraphicsWindow.Height-3,GraphicsWindow.Width,3)
      
      GraphicsWindow.KeyDown = OnKeyDown
      GraphicsWindow.KeyUp = OnKeyUp
      
      x = GraphicsWindow.Width / 2 
      y = GraphicsWindow.Height - 10
      dx = 0
      dy = -3.2
      delay = 15
      jump = "True" 
      
      GraphicsWindow.PenWidth = 1
      GraphicsWindow.PenColor = "black"
      GraphicsWindow.BrushColor = "red"
      ball = Shapes.AddEllipse(10,10)
    EndSub
    
    Sub MoveBall
      'Update the location of the ball based on user input and current velocity
      If (float = "True") Then 
        y = y + dy 
        dy = dy + 0.05 'Difference interval (moving upward and downward) 
        jump = "False" 
      EndIf 
      
      Shapes.Move(ball, x,y)
      
      'Wrap past the edges
      If (x > GraphicsWindow.Width) Then
        x = 0
      ElseIf (x < 0) Then
        x = GraphicsWindow.Width
      EndIf
    EndSub
    
    Sub Bounce
      If (y > GraphicsWindow.height-12) Then
        dY = -3.2 'Height of bounce 
        float = "False" 
      EndIf
    EndSub
    
    Sub controlBall
      If (keyDown["Right"]) Then 
        dx = 1 
        x = x + dx
        If (keyDown["Space"] And jump = "True") Then 
          float = "True" 
        ElseIf (keyDown["Space"] <> "True") Then 
          jump = "True"
        EndIf 
      ElseIf (keyDown["Left"]) Then 
        dx = -1
        x = x + dx
        If (keyDown["Space"] And jump = "True") Then 
          float = "True" 
        ElseIf (keyDown["Space"] <> "True") Then 
          jump = "True" 
        EndIf 
      ElseIf (keyDown["Space"] And jump = "True") Then 
        float = "True" 
      ElseIf (keyDown["Space"] <> "True") Then
        jump = "True" 
      EndIf 
    EndSub
    
    Sub FrameDelay
      While Clock.ElapsedMilliseconds - frameStart < delay
      EndWhile
      frameStart = Clock.ElapsedMilliseconds
    EndSub
    
    Sub OnKeyDown
      lastKey = GraphicsWindow.LastKey 
      keyDown[lastKey] = "True"
    EndSub
    
    Sub OnKeyUp 
      lastKey = GraphicsWindow.LastKey 
      keyDown[lastKey] = "False"
    EndSub



    • Edited by Ezra94 Thursday, September 11, 2014 9:22 PM Reword
    Thursday, September 11, 2014 9:07 PM
  • Secondly, do you know how the " frameDelay " subroutine works? I don't understand how an empty loop affects the program. 

    The Clock.ElapsedMilleseconds changes every millisecond. The frameStart is subtracted and if the result becoms bigger than delay, the while-loop exits. Until then the while keeps repeating.


    Jan [ WhTurner ] The Netherlands

    • Marked as answer by Ezra94 Friday, September 12, 2014 2:45 PM
    Friday, September 12, 2014 9:21 AM
    Answerer
  •  By the way, how did you figure out the physics for this game? Did you eye it? Do you have background knowledge in physics? Did you just approached it from a logical view point? 

    I was 12 at the time, So I kind of Guessed from a logical point of view :D

    My Thoughts were that I needed to move the ball at a certain speed, and then increase that speed.

    So I started with something like this:

    Ball = Shapes.AddEllipse(10,10)
    x = 200
    y = 50
    dy = 0
    
    While 1 = 1 
      Program.Delay(5)
      dy = dy + 0.05
      y = y + dy 
      Shapes.Move(Ball,x,y)
      EndWhile

    And then Moved on from there.

    Also notice that although this isn't actually a physics formula, (Which would be: Y=(G/2)*(T^2) ) You can use it as one:

    Ball = Shapes.AddEllipse(10,10)
    x = 200
    y = 300
    dy = -3.2
    dx = 3
    While 1 = 1 
      Program.Delay(5)
      dy = dy + 0.05
      y = y + dy 
      x = x + dx
      Shapes.Move(Ball,x,y)
      EndWhile

    Basically all you have to do to get the ball to jump, Is to set "Dy" to -3.2

    Hope this helps!


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    • Marked as answer by Ezra94 Friday, September 12, 2014 5:46 PM
    Friday, September 12, 2014 3:24 PM
    Answerer
  • I'm sure how the value of Clock.ElapsedMilliseconds is less than 15 though. I checked its value through a TextWindow.WriteLine() . . . I kept getting a large decimal. There are two digits after the decimal that always change such as, .16 or .70 or .06. I don't know how the value of Clock.ElapsedMilliseconds compares to a real number. 
    Friday, September 12, 2014 3:28 PM
  • The Clock.ElapsedMilliseconds returns the number of millseconds since januari 1st, 1900

    Every time the program executes the command, it returns the value as read from the computerclock. If you set Framestart equal to the output from Clock.ElapsedMilliseconds and 50 milliseconds later you calculate Clock.ElapsedMillisecond - Framestart you get a value slightly larger than 50.

    This is a real number, with two digitsbehinf the decimal sign.


    Jan [ WhTurner ] The Netherlands

    • Marked as answer by Ezra94 Friday, September 12, 2014 6:40 PM
    Friday, September 12, 2014 5:16 PM
    Answerer
  • So, the difference between frameStart and Clock.ElapsedMilliseconds will range between 1 to 100 milliseconds? If the difference is less than delay (15) then it will loop until the difference is greater than 15. Thus, creating a delay without using Program.Delay(). Correct?  

    • Edited by Ezra94 Friday, September 12, 2014 6:42 PM Reword
    Friday, September 12, 2014 6:40 PM
  • Yes, the difference is that Program.Delay will leave the cpu free for other things during the delay, but the continuous While loop will keep using cpu doing 100s or 1000s of loops using up cpu cycles.

    So best practice - if you can calculate what delay you want then call Program.Delay.

    • Marked as answer by Ezra94 Friday, September 12, 2014 7:42 PM
    Friday, September 12, 2014 6:51 PM
  • There will be situations when Program.Delay() is not the best option for delay? So the program by codingCat and Zock77 is an example of this exception? Here is the code: BCL208-6. Take note of the commentary for the sub routine FrameDelay from the program below:  

    Sub FrameDelay
      'Loop will continue until delay has been reached. Because frameStart
      'is set at the end of the last frame delay, the frames should be constantly
      'the same regardless of the processing in each individual frame. 
      '(ie shorter delays when more pressing is done)
      While Clock.ElapsedMilliseconds - frameStart < delay
      EndWhile
      frameStart = Clock.ElapsedMilliseconds
    EndSub
    I don't get the comment above completely. Is it saying that the key events affect the program delay?

    Friday, September 12, 2014 7:42 PM
  • The code is fine, but I would change

    While Clock.ElapsedMilliseconds - frameStart < delay
    EndWhile

    to below for the reasons in my last post (sparing cpu cycles).

    msDelay = delay - (Clock.ElapsedMilliseconds-frameStart)
    If (msDelay > 0) Then
      Program.Delay(msDelay)
    EndIf

    Comments are just that and may not be so clear - the code is.  All the comments mean is that the more time spent doing presses etc, then the delay will be shorter to keep the frame rate roughly constant - i.e. just delay long enough to keep the frame rate constant.

    If we have a desired frame rate of 50 fps, then delay is 20ms.  If the action calculations, graphics update etc take 8 ms (Clock.ElapsedMilliseconds-frameStart), then we need to pause for 12ms before starting the next frame.


    • Marked as answer by Ezra94 Friday, September 12, 2014 8:10 PM
    • Edited by litdev Friday, September 12, 2014 8:10 PM typo
    Friday, September 12, 2014 7:52 PM