r/howdidtheycodeit • u/lqstuart • Jun 06 '22
Question How do they code clocks with 1/1000 second precision in a game?
I had this question a while ago about Forza specifically, but it goes for any racing game or pretty any game with a timer. It's not uncommon to see a difference in lap times of e.g. 0.005 seconds either in races themselves or on leader boards, but the game runs at 60 fps which I assume means the game is only capable of registering increments of 1/60 ~= 0.016s through normal means. How does it figure out if two cars finish within the same frame?
135
Upvotes
163
u/-manabreak Jun 06 '22
The game logic can run independent of the rendering (and usually does so). This means that the game logic (and thus the timers) can run as fast as they possibly can, and renderer does its thing once every 60th of a second. These things are often (but not always) running on separate threads; one thread processes the game logic, and another churns out stuff to the graphics card.
36
u/nvec ProProgrammer Jun 06 '22
As others have said you decouple the physics from the rendering but there's another technique which can be used in this particular case.
Every time the physics update is called for each car store the car's position along with a high-resolution timestamp. Now when the car crosses the finish line you can record the final position and timestamp. Now you have a 'before finish' and 'after finish' position you can use maths to work out where the vector between them intersects the finish line, and then use that (or the distance from it to the before/after positions) to calculate the exact time when the car finished.
Honestly though players won't notice this level of detail, they're stuck to a 60fps-or-so update fed into a notoriously slow to react system known as 'the brain'. They really wouldn't be able to tell the difference between this as tracking accurately to 1/60s and then adding a tiny random number between 0 and 1/60s to add some fake detail. As far as they're concerned the game is still coming up with these highly-accurate times and they're still having the same experience, equal fun but much easier to code.
8
u/rand1011101 Jun 06 '22
i'm surprised at all the answers focusing on frame rates.. like can't you just compute it based on the distance between the two cars?
even if both cars cross the finish line in the same frame, one's further ahead than the other, so you just gotta do a bit of math. am i missing something?
4
u/1-420-666-6969 Jun 06 '22
Here's a great read on the different ways you can approach game loops: https://gameprogrammingpatterns.com/game-loop.html
4
u/reality_boy Jun 07 '22
As others have mentioned, the answer is interpolation. The physics is taking snapshots of the world every 16ms, but the car is usually crossing a checkpoint at some time between snapshots. The trick is to snapshot the cars position and time before and after the checkpoint. Then do a linear interpolation to work out the exact time the car crossed the checkpoint.
This is the only reliable way to handle the timing, without it you can be off by a tenth of a second or more. A car going 200 kph will travel around 10 meters in 16 ms, so you can potentially have a 20 meter error in position if you don’t interpolate both sides of the sector.
This variability does not matter to the user watching the monitor. The monitor is only updating once every 16 ms as well and your eyes struggle to detect such a fast event.
7
u/moonshineTheleocat Jun 06 '22
OS's have API's for high precision based on the clock speeds of the processor.
1
u/qStigma Jun 17 '22
That's what I usually call Updates Per Second (UPS) , some might also call it Ticks Per Second (TPS) but that term is generally used in a game server main loop instead, though the game server probably updates at a higher rate in the background, it generally accepts inputs/commands/requests through networking at that given TPS rate.
Same with most modern games, no matter the engine, developers rarely lock down UPS to frame draw rate (FPS) but it used to be usual a few decades ago if I recall correctly.
Physics for instance is usually running on a different thread than the main loop one (a lot of subsystems are, depends heavily on the engine to be honest) if there is a temporary bottleneck in the CPU you could in theory see things slowing down (like you see in minecraft when the tick rate halts entirely and everything is slow) though most physics systems are time based, so calculations are based on variations between each update, therefore things are usually where they should be.
1
u/coding_all_day Jun 06 '22
The game logic and rendering can be made to run on different timescales on different threads. Also continues collision detection algorithms can give you exact time of collision regardless of framerate. e.g. the car was 1 meter away form the finish line collider in last frame it is past the collider on current frame. based on the vehicles speed and the position of the collision, one can work out the exact time of collision. You can use continues collision detection algorithms (for example swept shapes) to pull this off.
0
u/Giacomand Jun 06 '22
The operating system also has APIs to access a high precision timer.
0
u/Innominate8 Jun 06 '22
The computer has a real time clock. Regardless of what the game is doing all the game needs to do is save the start time, then grab the current time and subtract.
0
u/onebit Jun 06 '22 edited Jun 06 '22
i bet they're lying to you. my bathroom scale has .1 precision but it always displays even numbers.
if you took results of many races i bet the times would always be rounded to 1/60 or whatever the game engine is pegged to.
edit: i like the interpolation answer. when you cross a checkpoint it could interpolate back to see when you actually crossed.
-6
u/OffgridRadio Jun 06 '22
Often they use something called epoch time or unix time which can be checked by any code at any moment. This is represented by the number of seconds since Jan 1 1970 and also contains portions of a second to great accuracy.
*Also in video game context, the frames are known in the code, like in Unity it uses something called "Delta time" https://docs.unity3d.com/ScriptReference/Time.html
-1
u/Tom_Bombadil_Ret Jun 06 '22
As many people have stated in modern games this isn’t really an issue as the games back end processing can be much more precise regardless of how the game runs visually but this has not always been the case. Consider the Post Man Challenge in LoZ Majora’s Mask. It’s a mini game in which you are supposed to time out exactly 10 seconds and press the button at the right time without it showing you a timer. At the end of the mini game when it shows you your time it reads it off as if it was accurate down to the one hundredth of a second but that’s not actually true on the back end. If the game says you got 9.98 seconds that could actually be anywhere from 9.96-9.995 as the back end can’t actually measure that small of an interval of time accurately and instead just goes based on animation frames.