tick or time based game loop?

This forum is for discussing shmup development, tools, engines and techniques.
Posts: 2
Joined: Sun Jul 28, 2013 7:02 pm

tick or time based game loop?

Postby clay » Mon Jul 29, 2013 5:03 am

Hi! I've just got back into playing vertical bullet hell shmups and I'm itching to create something myself now,

I can't decide on how to do the game loop for animation and input. I'd like to start a discussion on these issues as well as write out what I know so far to get my thoughts clear - hope that's welcome here! There are two main options and for each of those the details can vary quite a bit:

  • tick based: speeds are just given as distances. a tick is a fixed time step and every object is moved along each tick
  • time based: speeds are measured in pixels per second. every frame, objects are moved speed*time where time measures how long ago the previous frame was.

Animation and Lag:

My biggest worry with tick based is, for example if you wanted to have 60 ticks a second that might animate smoothly and run well on a fast computer but on a slower computer there might be moments where the computer can't render or animate each tick fast enough so the game slows down in an unwanted way. The idea of time based is that everything will be moving at the correct speed no matter what the frame rate is, so if there are moments where a slow computer can't keep up the bullets will still have moved the same distance they would have.

I think there is a potential problem with time based animation that different framerates could cause slightly different relative positions of objects depending on random events like how fast the computer feels like running on that day, although maybe if it's not a problem and reproducability is fine? I'd be interested in other peoples experiences with this.

In a tick based system it might make sense to have a game running at say 30 frames per second, but update several ticks animations per frame. On the other hand, trying to do too many ticks per frame could swamp the computer with unnecessary calculations - I don't know the correct way to balance this out. Any thoughts on what works well?

Slowdown:

There are two types of slowdown worth keeping note of: First if the game itself slows down because the computer can't keep it and secondly intentional Cave style slowdown where it feels like time is slowing down when there's so many bullets on screen. It's simple to implement Cave style slowdown in either system so there's no need to discuss that much.

I think it would be important for a game running on a wide variety of personal computers (as opposed to a console) to keep track of slowdown so that you can tell if the game is running as it should be. I'm not sure how the game should actually respond to slowdown though, should it just measure it? Should it try to execute more ticks per frame at a lower framerate until it can go back to normal? In a time based system should it be only lowering the frame rate (so that bullets jump across the screen) or should there be some limit at which it also slows the game down to be fair to the player?

Input and Control:

It's also not clear to me how to best handle input in both systems. The main thing I'm considering is the smallest possible movement the player can do by tapping a key. It would be horrible if it became impossible to do a small movement just because of lag or something!

If input comes asynchoniously (i.e. a subroutine separate from the main loop is called when inputs are given) then in a time based system it might make most sense to note down the times keys were pressed and released to move the player based on that, in tick based system we might do the same but round that number to a tick number rather than keeping it in seconds - alternatively inputs may be polled from the game loop. I think that would avoid the need to do any rounding in a tick based system but it doesn't seem to mesh well with time.

One thing that doesn't need much discussion but needs to be kept in mind during programming is how to make the game save replays and be able to pla them back correctly. It's definitely going to be easier in a tick based system though.

User avatar
Posts: 34
Joined: Sun Jul 29, 2012 4:44 pm

Re: tick or time based game loop?

Postby kdmiller3 » Tue Jul 30, 2013 12:21 am

I prefer a time-based system with fixed simulation update frequency and visual interpolation (as seen in Fix Your Timestep!) with speeds measured in in distance units per second (be they meters or pixels or whatever). It's a very general approach that can be overkill for simple arcade-style games but can get you a lot of neat benefits. (In particular, you can speed up or slow down time without affecting your simulation.)

Since the time step used by the simulation is constant, you can do input recordings and playback just as easily as with tick-based simulation.

(This approach is ultimately the same as the tick-based approach but hides the tick-based nature from the underlying game logic.)

Posts: 2
Joined: Sun Jul 28, 2013 7:02 pm

Re: tick or time based game loop?

Postby clay » Tue Jul 30, 2013 5:26 am

kdmiller3 wrote:I prefer a time-based system with fixed simulation update frequency and visual interpolation (as seen in Fix Your Timestep!) with speeds measured in in distance units per second (be they meters or pixels or whatever). It's a very general approach that can be overkill for simple arcade-style games but can get you a lot of neat benefits. (In particular, you can speed up or slow down time without affecting your simulation.)

Since the time step used by the simulation is constant, you can do input recordings and playback just as easily as with tick-based simulation.

(This approach is ultimately the same as the tick-based approach but hides the tick-based nature from the underlying game logic.)


That article is really awesome, thanks a lot! It seems like a hybrid approach that gives you the best of both worlds! It's very useful how he names things (like the Spiral of Death and Temporal Aliasing). I think I'll implement things in that way, and try to make it detect if a spiral of death is occuring so the game can pause itself rather than swamp out your computer.

User avatar
Posts: 34
Joined: Sun Jul 29, 2012 4:44 pm

Re: tick or time based game loop?

Postby kdmiller3 » Tue Jul 30, 2013 3:35 pm

You can avoid the spiral of death by capping the number of updates per frame. Simulated time will slow down but the frame rate won't take a dive. I don't think you'll have to worry about it. :)

Posts: 3
Joined: Wed May 22, 2013 6:16 am

Re: tick or time based game loop?

Postby moechofe » Tue Aug 06, 2013 12:14 pm

I'm using a frame based loop. When a player got a low frame rate or if the A.I. decides to reduce the FPS of the game, the speed of all ships and bullets are updated in the same way.

Also, this insure that no frames would be skipped.

Return to Development

Who is online

Users browsing this forum: No registered users and 6 guests