{"id":258,"date":"2015-09-21T17:35:24","date_gmt":"2015-09-21T21:35:24","guid":{"rendered":"http:\/\/julianvidal.com\/blog\/?p=258"},"modified":"2015-09-21T17:35:24","modified_gmt":"2015-09-21T21:35:24","slug":"deltatime-corona-sdk-plugin-for-time-based-animations","status":"publish","type":"post","link":"https:\/\/julianvidal.com\/blog\/deltatime-corona-sdk-plugin-for-time-based-animations\/","title":{"rendered":"deltaTime: Corona SDK plugin for time based animations"},"content":{"rendered":"<p>I&#8217;ve recently published a small but handy plugin in the Corona plugin directory. The <a href=\"https:\/\/store.coronalabs.com\/plugin\/deltatime\" target=\"_blank\">deltaTime<\/a>\u00a0plugin allows developers to turn regular animations into time-based animations that will play back at the same speed regardless of framerate variations.\u00a0This is important because you always want to have your objects move at the same speed regardless of what the framerate is. Even when you set your app&#8217;s framerate at a fixed number it never really runs at exactly that speed; sometimes it&#8217;s faster and sometimes it&#8217;s slower. Even worse, the framerate varies considerably in cellphones as other processes are most likely running in the background and using CPU cycles for something other than your app. Using this library helps in making all animations run at the same apparent speed even when your framerate varies. After this library is properly set up (don&#8217;t worry it&#8217;s incredibly easy) you should be able to change your framerate from 30 to 60 or vice versa and your animations should run at exactly the same speed.<\/p>\n<p>Usually you would animate a Corona DisplayObject like this:<\/p>\n<pre class=\"lang:lua decode:true \">local speed = 5\r\nobj.x = obj.x + speed<\/pre>\n<p>Or like this:<\/p>\n<pre class=\"lang:default decode:true \">local speed = 5\r\nobj:translate(speed, 0)<\/pre>\n<p>And that&#8217;s fine, except that every frame your object will move a\u00a0<strong>fixed<\/strong> amount of points. If your framerate slows down then your object will move slower and if your framerate goes up then the object will move faster.<\/p>\n<p>With the deltaTime plugin you can make this animation run at the same speed regardless of your framerate. But first you need to make sure your Corona project is ready for it. First you need to go to the <a href=\"https:\/\/store.coronalabs.com\/plugin\/deltatime\" target=\"_blank\">Corona Plugin Directory<\/a> and activate the plugin (click the green Free icon). It&#8217;s absolutely free and you need to do this only once per Corona account even if you use the plugin for many projects.<\/p>\n<p>Second you need to add the plugin to the <em>build.settings<\/em> file so that the Corona server\u00a0adds it to your build.<\/p>\n<pre class=\"lang:lua decode:true\">settings = {\r\n    -- You may of course have other keys before, after \r\n    -- or even inside your \"plugins\" key.\r\n    plugins = {\r\n        [\"plugin.deltatime\"] = {\r\n            publisherId = \"com.julianvidal\",\r\n        },\r\n    },      \r\n}<\/pre>\n<p>And\u00a0lastly you need to\u00a0load the\u00a0plugin just like any module and start it.<\/p>\n<pre class=\"lang:lua decode:true\">local deltatime = require \"plugin.deltatime\"\r\ndeltatime.start()<\/pre>\n<p>Now instead of making your objects move by a fixed amount you have to make sure that your movements take into account the time multiplier. This is very straight forward:<\/p>\n<pre class=\"lang:lua decode:true\">local speed = 5 * deltatime.multiplier()\r\nobject:translate(speed, 0)<\/pre>\n<p>That&#8217;s it! If your framerate drops, say by half, the multiplier will double so your speed variable will double too and your animations will continue to run at the same speed.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve recently published a small but handy plugin in the Corona plugin directory. The deltaTime\u00a0plugin allows developers to turn regular animations into time-based animations that will play back at the same speed regardless of framerate variations.\u00a0This is important because you always want to have your objects move at the same speed regardless of what the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":262,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[18,2],"tags":[38,40,42,41],"class_list":["post-258","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-featured","category-programming","tag-corona-sdk","tag-delta-time","tag-framerate-independent-animations","tag-time-based-animations"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/julianvidal.com\/blog\/wp-content\/uploads\/deltaTime_icon.png?fit=450%2C300&quality=80&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/palO2H-4a","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/posts\/258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/comments?post=258"}],"version-history":[{"count":4,"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/posts\/258\/revisions"}],"predecessor-version":[{"id":263,"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/posts\/258\/revisions\/263"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/media\/262"}],"wp:attachment":[{"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/media?parent=258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/categories?post=258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/tags?post=258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}