I recently been playing a Hat in Time. One of the hats you can get can slow down time, which makes basically everything except the players. It made me wonder, how do time slow down effects work on a technical side? Does every individual thing besides the play have to be individually programmed to slow down, or are there ways to more cleanly get everything but the player into slow motion?
We usually call this sort of thing “timescale” or “time scaling”.
One of the things about using computers is that things don’t really run in real time. We are limited by the speed of things like processors, memory access, etc. so there’s a minimum amount of time it takes for operations to run. In video games, we usually break it down into frames - each visual update for the screen. Whenever we update the frame, we typically also update all of the other stuff in the game that we need to - where things are, what effects are playing, what the character’s health is, what their cooldowns are doing, etc.
We don’t have to tie these updates directly to frame rate, that’s just how often we run the update. We can adjust for time. We can figure out exactly how much time (in milliseconds) has passed between the last update and the current one, and use that information to update where things are and should be in game, effectively decoupling the frame rate from the update rate. Think of it like this - if you throw a ball up into the air and have a camera is taking snapshots of the ball’s trajectory as fast as you can press the button, you won’t necessarily get evenly spaced shots of the ball’s flight. However, the ball will still move uniformly and independently of the speed at which you press the camera button. That’s what I’m talking about happening in the game - we’re updating the ball based on how much time passes between each camera shot, not the number of camera shots. The motion of the ball as simulated in game will match the time it took, not the camera’s shutter speed.
Once we can do this, we can also do things like apply factors to it. We can speed things up or slow them down by manipulating the amount of time that we say happened in between each shot. For example, if we get a frame time of 100ms but want things to appear to run half as fast, we can tell the simulation only 50ms has passed. If we want things to appear to run twice as fast, we can tell the simulation that 200ms has passed. We can also apply this selectively - only objects flagged for slow updates get updated at half speed. Objects flagged for fast speed get updated at double speed. We can dynamically adjust the speed so that it updates on a curve, rather than a flat amount. We can temporarily freeze updates. There’s a lot of potential gameplay from this.
The FANTa Project is currently on hiatus while I am crunching at work too busy.
[What is the FANTa project?] [Git the FANTa Project]
Got a burning question you want answered?
Short questions: Ask a Game Dev on Twitter
Long questions: Ask a Game Dev on Tumblr
Frequent Questions: The FAQ