Question derived from this tweet:

@Vincent_Scheib, thoughts on using using two loops for a HTML5 game: one requestAnimationFrame for rendering, and one setInterval for logic?

  • 3
    \$\begingroup\$ There doesn't seem to be any sort of question here. If you're interested in a discussion, you're welcome to bring up your thoughts in the chat \$\endgroup\$ – thedaian Mar 12 '12 at 16:34

Experimentation and prototyping are likely the only way to get the right answer for a given game, so I encourage keeping code easy to re-work into different loop paradigms. My thoughts:

  1. Logic and Render in different loops will have many of the same issues that placing logic into a Web Worker will have. Listing them here:
  2. Logic and Render in different loops will add latency.
    • The updates must occur before the draw calls come, but there is nothing keeping the timing close together. So, there will be 'idle' time between the logic update and render code.
  3. Logic and Render in different loops may cause strobing or beat patterns.
    • A simple approach will simply try to update both at 60hz, but as the timers are uncorrelated they may drift and result in some frames with more than 1 logic update, or even no logic updates.
  4. Some logic doesn't need to run at 60Hz.
    • For example decorative physics and AI planning updates are often suggested to be run less frequently to reduce CPU load. They can either be rendered as moving less often, or animation can tween them to keep things moving smoothly.
  5. Network communication may not need to run at 60Hz, and might be necessary even when Request Animation Frame is not calling your draw function.
    • E.g. if a browser stops calling your draw function from R.A.F. you may still be receiving data via Web Sockets and want to keep a logic update ticking occasionally to deal with that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.