{"id":5,"date":"2011-07-04T13:05:31","date_gmt":"2011-07-04T20:05:31","guid":{"rendered":"http:\/\/gigglenuts.com\/blog\/?p=5"},"modified":"2011-07-04T13:05:31","modified_gmt":"2011-07-04T20:05:31","slug":"game-loops-and-frametime","status":"publish","type":"post","link":"https:\/\/gigglenuts.com\/blog\/?p=5","title":{"rendered":"Game Loops and Frametime"},"content":{"rendered":"<p>Kwasi Mensah wrote a blog on #altdevblogaday entitled\u00a0<a title=\"Game Loops on IOS\" href=\"http:\/\/altdevblogaday.com\/2011\/06\/27\/game-loops-on-ios\/\" target=\"_blank\">Game Loops on IOS<\/a> that I skimmed through. I'm currently not dealing with game loops, frametime, nor IOS right now, so this post is just a little reminder for my future self should I find myself tumbling down that wormhole.<\/p>\n<p>Glenn Fiedler's article <a title=\"Fix Your Timestep\" href=\"http:\/\/gafferongames.com\/game-physics\/fix-your-timestep\/\" target=\"_blank\">Fix Your Timestep<\/a> is the oft-referenced article on the topic for good reason, and is always worth another read through.<\/p>\n<p>The summary for Kwasi's article:<\/p>\n<ul>\n<li>He has a function that gets called when there was a vsync<\/li>\n<li>If a vsync happens while he is in the function, the function will get called again immediately after it exits<\/li>\n<li>Most of the time you should be hitting 60 fps (or 30 fps) and occasionally your frametime might go over<\/li>\n<li>What you don't want is to spiral out of control if you occasionally miss a frame. If you are consistently missing frames than you should drop your goal frametime to the next level (60 -&gt; 30)<\/li>\n<li>Start by timing how long it took to run the \"DoFrame\" function, call this time <em>elapsed<\/em>.<\/li>\n<li>If <em>elapsed<\/em> took longer than your goal frametime, then change the value of <em>elapsed<\/em> to your goal frametime plus how much you overran the vsync: <strong><em>fmod(elapsed,frametime)<\/em><\/strong><\/li>\n<li>Store <em>elapsed<\/em> in a persistent variable, called <em>bank<\/em>, for the next call to \"DoFrame\".<\/li>\n<li>At the start of \"DoFrame\" subtract your goal frametime from the persistent <em>bank<\/em> variable.<\/li>\n<li>If <em>bank<\/em> is higher than zero, that means your last processing through \"DoFrame\" lasted longer than your goal frametime, and therefore, you missed a vsync (or more). In this case he suggests just forgoing the processing of \"DoFrame\" for this frame and just wait until the next vsync (the article explains why). So\u00a0exit out of \"DoFrame\" until next time.<\/li>\n<li>Otherwise <em>bank<\/em> is less than, or equal to, zero. Which means that the last processing through \"DoFrame\" was quicker than your goal frametime (or, just as long). Reset <em>bank<\/em> to zero because we are starting a new, and do the processing of \"DoFrame\".<\/li>\n<\/ul>\n<p>So, occasionally you'll have to eat some time to wait for the next vsync should you miss a goal vsync. But the idea is, if you don't, another vsync will come along, and if you miss that (as you are already under a sub-vsync interval of time remaining due to the miss) than you are going to throw off things even more. I guess this will cause a, probably\u00a0unnoticeable, slow down in time for when you miss a vsync.<\/p>\n<p>I have to give some thought to all of this, but until then...<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Kwasi Mensah wrote a blog on #altdevblogaday entitled\u00a0Game Loops on IOS that I skimmed through. I&#8217;m currently not dealing with game loops, frametime, nor IOS right now, so this post is just a little reminder for my future self should I find myself tumbling down that wormhole. Glenn Fiedler&#8217;s article Fix Your Timestep is the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/gigglenuts.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5"}],"collection":[{"href":"https:\/\/gigglenuts.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gigglenuts.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gigglenuts.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gigglenuts.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5"}],"version-history":[{"count":3,"href":"https:\/\/gigglenuts.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5\/revisions"}],"predecessor-version":[{"id":8,"href":"https:\/\/gigglenuts.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5\/revisions\/8"}],"wp:attachment":[{"href":"https:\/\/gigglenuts.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gigglenuts.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gigglenuts.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}