Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Perfect Timing!

Perfect Timing!

Scheduled Pinned Locked Moved C#
help
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pokabot
    wrote on last edited by
    #1

    Hi all, I need to move a sprite exactly 100 pixels every second regardless of the framerate. Any help would be great ..thx.

    J C H 3 Replies Last reply
    0
    • P pokabot

      Hi all, I need to move a sprite exactly 100 pixels every second regardless of the framerate. Any help would be great ..thx.

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      pokabot wrote:

      I need to move a sprite exactly 100 pixels every second regardless of the framerate

      The simple answer is to obtain the current framerate and move the sprite by 100/framerate but what happens if the framerate falls below 1 FPS?

      --- How to get answers to your questions[^]

      O 1 Reply Last reply
      0
      • J J4amieC

        pokabot wrote:

        I need to move a sprite exactly 100 pixels every second regardless of the framerate

        The simple answer is to obtain the current framerate and move the sprite by 100/framerate but what happens if the framerate falls below 1 FPS?

        --- How to get answers to your questions[^]

        O Offline
        O Offline
        originSH
        wrote on last edited by
        #3

        You could just use a heartbeat timer.

        1 Reply Last reply
        0
        • P pokabot

          Hi all, I need to move a sprite exactly 100 pixels every second regardless of the framerate. Any help would be great ..thx.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          All games work this way, they move the sprites based on time passed, and render as many frames as the processor lets them. So, the key answer is, don't write any code that changes any values just because a render event has occured.

          Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          P 1 Reply Last reply
          0
          • C Christian Graus

            All games work this way, they move the sprites based on time passed, and render as many frames as the processor lets them. So, the key answer is, don't write any code that changes any values just because a render event has occured.

            Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            P Offline
            P Offline
            pokabot
            wrote on last edited by
            #5

            So .. am i right in thinking if i fix the framerate to 60fps and then every loop move the sprite 100 / amount of ms passed then if any time is left draw the frame?

            1 Reply Last reply
            0
            • P pokabot

              Hi all, I need to move a sprite exactly 100 pixels every second regardless of the framerate. Any help would be great ..thx.

              H Offline
              H Offline
              Hessam Jalali
              wrote on last edited by
              #6

              you need an accurate timer (there is one in DXUtils class in DirectX SDK) and define your move formula depend on time and speed just like X=Vt+X0 for example if X0=0 and V=100px/sec then in 2Sec it would be 200px or in 0.01 sec it would be just 1px so it does not depend on the framerate you can take two approach depends on your need 1- X0 would be always zero so after 10 sec you pass the 10sec to formula 2-use time Elapsed method (use in most games) you can calculate the elapsed time using the timer and the previuos position would be X0. for example if X0=0 and V=100px/Sec after one sec X=100px and after 1.5 sec from beginning it would be X0=100px ,V=100px/sec,ElapsedTime=0.5 sec X=100*0.5+100=150px and if you are doing it in the 3D you must define each speed for every dimension or calculate them thruogh total speed using math and ofcourse you involve acceleration too. hope the post would be useful

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups