5
\$\begingroup\$

I have begun creating a very simple game engine and I am trying to work out how to create a time scale for the game. By time scale I mean some way of increasing and decreasing the speed of the game (not the fps) like creating a slow motion effect.

I have no idea how this could be accomplished so any help would be appreciated.

If this helps I'm using windows, OpenGL, and C++ to make my game engine.

How my engine is setup:

I have an update and draw function. The update function is called 25 times per second while display is called as much as possible.

\$\endgroup\$

4 Answers 4

8
\$\begingroup\$

I'm not sure about the architecture of your game, so what's follow is a rather generic answer. Usually there are two different 'rates' in the game - the screen refresh or draw rate, and the logic update rate.

The draw rate is the FPS - some game engines will try to draw as often as possible, while others will try to draw at a fixed rate. Regardless, it is not a good idea to tie logic updating to the draw rate of your game.

Most game engines instead will have a update loop, which is governed by the logic update rate. This is the rate you you to speed up, slow down, or pause the game. To pause, you simple stop updating. To slow down, you increase the time between each update, and so on.

As an example, Unity3D has a variable called Time.timeScale, which affects the Time.deltaTime variable used by all most logic functions to determine the elapsed time each frame. By setting the time scale to 1.0f, the game runs at normal speed. Setting it at 0.5f will run it half the speed.

\$\endgroup\$
2
  • \$\begingroup\$ yeah i got the idea from unity and was hoping to be able to make something similar in my engine ill update my question and add a little more detail as to how my engine is setup.. And thanks for the help...i will attempt to implement it now :D \$\endgroup\$ Commented May 22, 2011 at 9:44
  • \$\begingroup\$ thanks for your help...i managed to implement time scale into my game :D. \$\endgroup\$ Commented May 24, 2011 at 10:13
1
\$\begingroup\$

I guess the easiest way is to change delta_time value you pass to your update functions, for example if you tell your update function that the last frame took 1/120s but having 1/60s for a real frame, it makes your game to update half normal speed (it doesn't affect controls but may cause some side effects like delaying buttons and etc.)

\$\endgroup\$
0
\$\begingroup\$

In Ogre3D, and I suspect many other engines, you'll get a callback when the latest frame has been queued to render. This can be (and often is) your source for relaying "time" to the rest of your application.

void frameRenderQueued(FrameEvent ev)
{
   double scaledTime = ev.timeSinceLastFrame * MY_TIME_SCALE;

   // Stuff that needs scaled time
   myAnimationManager->addTimeToAllAnimations(scaledTime);
   myParticleEffectsManager->addTimeToAllParticleEffects(scaledTime);
   myBuffManager->addTimeToAllBuffs(scaledTime);

   // Stuff that doesn't need scaled time
   myPopupManager->addTimeToPopups(ev.timeSinceLastFrame);
}

In the above code, you can see that you can pass scaled time along to the things you want to slow down (animations and such, and if your game operates in "real time", any effects like buffs that are designed to last a certain amount of time in-game), but use the unscaled time for things that you wouldn't expect to be affected, like a pop-up in your UI and such.

\$\endgroup\$
0
\$\begingroup\$

Assuming you use a delta time for updating. Just multiply it by a scalar modifier that reflects how fast or slow you want time to progress. For example, 1.0 for standard speed or 0.5 for half.

modifiedDeltaTime = deltaTime * scale; 
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .