Earth Time -- Game Time

Started by
19 comments, last by Halsafar 18 years, 9 months ago
To simulate realism you often want a passing day. You however do not need a passing day to take a full 24hr's earth time. It is unlikely that a player would be satisied by that... (unles you matched it up to there clock thus when u play at night, the game would be night, thats a neat idea.).. Anyway, curious how one would clock passing time accuratly. I know how much time is passing between frames, that is my basis on time passing. Using floats to contain the numbers is where I am having a crisis. It is unwise to use comparison greater that or less than on floats you know will be big and contain many decimal points. Using ints to contain the numbers means a huge loss in accuracy. I suppose you could cast the floats to ints and just compare the number outside the decimal place... So how can one go about this using floats and avoiding comparisons of floating points numbers?
Advertisement
I'm not quite sure I know where these comparisons you are worried about are coming into effect? For a large amount of "time passing" effects you can get away with using only basic math (+-*/) and maybe only a few comparisons to mark significant points of the progression. Anyway, these comparison errors shouldn't be causing too many problems, I can't think for the life of me of where they would cause an error that wouldn;t get resolved in the next cycle.

Also, what are you using for timing? The basic timing system usually isn't floating point. That means that usually you will be using longs to get the time from the system, and that might be the best way for you to use the time in your game. No comparison errors at all this way.
Turring Machines are better than C++ any day ^_~
No, see I already have a QueryPerformanceCounter timer in use... Ever since I started game design. Yes, you use LARGE_INTEGER values.

To record the time which pass's between frames you end up recording a float. TimeDelta in a game is represented as a float.

As for floating point comparisons' I have read I believe that some systems will evaluate a float different than another might especially when the numbers get small. 1.0000000000002f > 1.0f on some comps may be true and may not...
Quote:Original post by Halsafar
No, see I already have a QueryPerformanceCounter timer in use... Ever since I started game design. Yes, you use LARGE_INTEGER values.

To record the time which pass's between frames you end up recording a float. TimeDelta in a game is represented as a float.

Why use a float inatead of using the integer values you are getting out of the counter? You aren't gaining any accuracy by converting between integers and floating point, it doesn't seem to make much sense in this case either.
Turring Machines are better than C++ any day ^_~
....have you used the performance counters?
Do you honestly think a 'whole number' of milliseconds pass each frame?

void SetStart() {QueryPerformanceCounter((LARGE_INTEGER*)&Start);} //Get start time
void SetFinish() {QueryPerformanceCounter((LARGE_INTEGER*)&Finish);} //Set the finish time
void SetDelta() {Delta = (Finish.QuadPart - Start.QuadPart) / (float)Frequency.QuadPart * 1000;} //Set Delta
Quote:Original post by Halsafar
....have you used the performance counters?
Do you honestly think a 'whole number' of milliseconds pass each frame?

void SetStart() {QueryPerformanceCounter((LARGE_INTEGER*)&Start);} //Get start time
void SetFinish() {QueryPerformanceCounter((LARGE_INTEGER*)&Finish);} //Set the finish time
void SetDelta() {Delta = (Finish.QuadPart - Start.QuadPart) / (float)Frequency.QuadPart * 1000;} //Set Delta

No, but a whole number of ticks certainly did. Like I said, converting to a float you are just losing the precision. If you are actually worried about that, then don't do the conversion. I've already said that I don't think it should be an issue though.
Turring Machines are better than C++ any day ^_~
Okay, well I guess this thread is not going how I want it.
Then new question:

How would you write a timer to keep track of game time as 2.5min = 1 game hour?
Quote:Original post by Halsafar
How would you write a timer to keep track of game time as 2.5min = 1 game hour?


__int64 game_time, real_time, real_time_start, game_time_start;

game_time = 24 * (real_time - real_time_start ) + game_time_start;

BTW, floating-point has a greater range than integers, but sacrifices precision for that range.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
I'm not following how that will assist me.
But it is what I've been getting closer to.
Maybe I'm just reading it wrong.
I would suggest that you instead use the time() function to keep track of "real" time, and continue using your performance counter for time between frames.

If you use the performance counter, you may end up with drift, and there would probably be some problems with processors that alter their step-size for reduced power consumption (mostly laptops, but also some desktop CPUs, now)

The time() function, however, is related directly to real passing time and covers a larger range of values (it increments once a second and indicates the passing time since 1/1/1970)... it's more appropriate for what you're trying to do. Oh, and you don't need to query it every frame, maybe once every hundred frames or so, since it has per-second resolution.

Check out the rest of the time.h header for convenient helper functions and especially the localtime() function.

HTH
Greenspun's Tenth Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden slow implementation of half of Common Lisp."

This topic is closed to new replies.

Advertisement