Skip to main content
gamedev.net
🔒 Locked

Calculating Time

Started by Permafried- Jul 11, 2004 at 7:20 PM 5 replies 1.2k views
Original Post
Permafried-
Permafried-
Hey, I'm having a little issue with a small calculation in my game and was hoping someone could help me out. To start, I read in the timelimit for the match from a config file and save it in a variable called m_fInGameTime. This is done on server initialization and then reset whenever changing levels. I then convert the value to milliseconds and save it in a seperate variable called m_fTimeLimit which will be compared vs the m_iServerStartTime to decide if the match has gone too long. Each frame move I want to calculate the new real in-game time as follows:

   // get the current game time
   m_fCurrentGameTime = static_cast<float>( timeGetTime() *
                                            0.001 );

   // get the elapsed time since last frame
   m_fElapsedFrameTime = ( m_fCurrentGameTime - 
                           m_fOldGameTime );

   // update the in-game clock if we need to
   if( ( m_fCurrentGameTime - m_fLastClockUpdateTime ) > 1 )
   {
	// update the game clock
	m_fInGameTime -= 0.01f;

	// set the last update time
	m_fLastClockUpdateTime = m_fCurrentGameTime;
   }

   // set this game time as the old game time
   m_fOldGameTime = m_fCurrentGameTime;

The problem with my calculation (as I'm sure you'll see immediately) is the following: // update the game clock m_fInGameTime -= 0.01f; The problem here is that it will take a value like 25.00 and when I subtract 0.01f, the value then becomes 24.99 when it should be 24.59. I'm sure there is a better way of doing this but as of right now I'm a little stuck on it and think maybe a pair of fresh eyes might help ^_^. Thanks, Permafried-
ray_intellect
ray_intellect
I don't understand why you need to subtract 0.01 from the value.
Why don't you just update the clock with a new floating point value every frame and ensure that it is synchronised to 60 FPS before rendering?
Permafried-
Permafried-
I want to subtract the value from the in-game time because this is used in calculating the actual in-game time on the server side (this is NOT a client) so I can have a timer which ticks down on-screen informing the client of how much time is left in the game (this value is sent to the client side), and is not used in calculating the current app time which I receive via timeGetTime().

I'd prefer not to limit my server to an internal frame rate of 60fps for the simple fact that this would cause an extreme overflow in the way I handle my network messages and thus would force me to force my client to render at no more than 60fps which is not what I am looking to do. I have looked into the possibility though and if it would make my life easier I'll switch it over so I receive perhaps two client packets/server frame which with a little re-working wouldn't pose too much of a problem.

Permafried-
lonesock
lonesock
Well, if you need to store it as minutes & seconds, (or hours & minutes), maybe you could just keep the smaller of the variables as an integer:

minutes--;displayHours = minutes / 60;displayMinutes = minutes % 60;


otherwise you need to subtract 1.0/60.0, not 0.01 (1.0 / 100.0).
ray_intellect
ray_intellect
Quote:
Original post by Permafried-
... so I can have a timer which ticks down on-screen informing the client of how much time is left in the game (this value is sent to the client side)...
Permafried-


In that case just subtract the elapsed time from the total game time. using a value of 0.01 would not be accurate.

float elapsed_time = timeGetTime() - previous_time;float game_timer_value = total_time - elapsed_time;total_time -= elapsed_time;


[edit]
forgot to update total_time
[/edit]
Permafried-
Permafried-
Thanks for all the help guys, I knew I was doing something terribly wrong but just couldn't put my finger on it, it's much appreciated.

I've tried both approaches and notice with Lonesock's that once I get below a value of .10 that I don't get the expected value. For example, I had my timelimit set to 25.00 (which I then converted to seconds and stored in m_iServerTimeLimit) and am extracting the value as follows:

   // decrease the time by 1 second   m_iServerTimeLimit--;   cout << ( m_iServerTimeLimit / 60 ) << ":";   cout << ( m_iServerTimeLimit % 60 ) << endl;   // set the last update time   m_fLastClockUpdateTime = m_fCurrentGameTime;


The output of that would look like the following:

24.13
24.12
24.11
24.10
24.9 <-- This is wrong, should be .09
24.8 <-- Ditto, should be .08

I guess at this point I need to perform a calculation if ( time % 60 ) < 10 to add that extra 0 in there to keep it consistent like a clock.

Thanks again,

Permafried-
VolkerG
VolkerG
That's just an output problem.
Use this:
#include <iomanip>...std::cout << std::setw (2) << std::setfill ('0') << ( m_iServerTimeLimit % 60 ) << std::endl

Usually, leading 0's won be written to the stream.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.