Calculate factor for real-world time into game time

2.1k Views Asked by At

I'm working on a game engine (text-based games) that supports day/night transitions in it. Each world has it's own calendar that manages how many days make up a year, what time zone offsets the world is in from the games universe clock among other things.

I want to make the transitions from day/night configurable, by letting each world define the ratio between a day in the game, to 1 hour in real-world time. An example would be a ratio of 15 minutes in the real-world and represents 1 full day in the game. The reason for this is because the engine has an internal timer that runs when the engine starts up. So I'll always need to convert real-world time into game time anytime I display the time of day in-game. I'm not sure what the math behind this would be - I was looking at 24 game-hours / 15 real-minutes = adjustment factor.

The intent being that i want to apply this adjustment factor to any real-world time from the engines internal stopwatch, and determine the game-time. That might be Total Hours elapsed * factor or Total Days elapsed * factor. Am I barking up the wrong tree with this? Math is not my strongest skill set (obviously!) which is hindering me here, and why i'm writing a text-based engine rather than anything with graphics. Is there a formula I could use that would give me a factor I could use for total elapsed days, hours, seconds interchangeably? Or do i need a factor per unit of time measurement?

1

There are 1 best solutions below

4
On BEST ANSWER

What you need to be careful of is units. In your example with $15$ minutes, convert that to $0.25$ hours so as to calculate how much faster the in-game time is compared to real time:

$$\frac{24\,h}{0.25\,h}=96.$$

So if you know that some time has passed in real time, you have multiply this time by $96$ to get the in-game time. For instance, if half an hour has passed in real time, $0.5\,h\times 96=48$ hours, i.e. two days, has passed in-game.