I have made a simple HTML5 racing game. The laps are timed. I am running the game are 40 frames per second, each frame represents 40 milliseconds. There for my timer will increase in 0.025 increments.

My question is how can I get my timer to record lap times more accurately, without running at 100 frames per second! How do other games do it? Could it be to do with gauging the distance from the car to the finish line in the frame before and after it crosses the finish line?

  • 1
    \$\begingroup\$ At 40fps your timesteps are 0.025 second increments. \$\endgroup\$ – user744 Jul 7 '11 at 23:40
  • \$\begingroup\$ Fixed, sorry about that! Question still remains though, thanks for spotting \$\endgroup\$ – Moz Jul 7 '11 at 23:46

Your own proposed answer is spot on; to determine the finish time for a car, you take its position on the frame before finishing and its position on the frame after finishing, and determine at what point during that frame it actually crossed the finish line.

If (for example) it crossed the finish line at 40% of the way from its previous position to its next position, then you only add 40% of the timestep value to determine the finishing time.

Assuming that your finish line is a straight line, and treating the player's movement from one frame to the next as a straight line, you can easily find where (and then, when during the frame) they crossed by solving a line intersection test.

| improve this answer | |

Okay first off, I don't know how you think you are running 40 frames per second with each frame being 40 milliseconds. 1 second = 1000ms. 40ms per frame gives you an FPS of 25. If you want to actually be at 40 FPS you are going to need to have a timestep of 25ms per frame.

Most racing games run with the car physics at a really high frame rate. One example would be Forza 3, it more sim like than most games, and its physics runs at 360 hertz.

So using a frame rate that is higher than your update rate could work, although with java-script you may be limited with performance.

If you really need to run at 40 FPS, I see no problem with creating a function that you can plug in the current time, and the distance, and have it return an estimated time.

| improve this answer | |
  • \$\begingroup\$ Downvote? Did I say something wrong? Tell me what I should change? \$\endgroup\$ – AttackingHobo Jul 8 '11 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.