{"id":6664,"date":"2016-03-04T09:00:16","date_gmt":"2016-03-04T14:00:16","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=6664"},"modified":"2016-02-29T04:51:30","modified_gmt":"2016-02-29T09:51:30","slug":"bouncing-ball-animation-using-events-with-ode","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/03\/04\/bouncing-ball-animation-using-events-with-ode\/","title":{"rendered":"Bouncing Ball Animation &#8211; Using Events with ODE"},"content":{"rendered":"\r\n\r\n<div class=\"content\"><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/54445-bouncing-ball--tutorial-for-ode45-events\">\"Bouncing Ball: Tutorial for <tt>ode45<\/tt> events\"<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2853760\">Matthew Kelly<\/a>.<\/p><p>Using animation is a great way to convey information. By animating something physical, it has an added effect of tying concepts with experience or intuition. This tutorial by Matthew is about using events with ordinary differential equation solver <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/ode45.html\"><tt>ode45<\/tt><\/a>, or any other ODE solver for that matter. He uses a bouncing ball example to show the effect of events, represented as the act of the ball coming into contact with the ground. Since a ball typically bounces over and over again, he repeatedly calls <tt>ode45<\/tt> with each calculation separated by an event (contact). The simulation takes into account the energy loss when the ball bounces, defined by the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Coefficient_of_restitution\">coefficient of restitution<\/a>. Add to that a nice terrain defined by a few sinusoids, then you get a nice animation of the ball bouncing all over the place.<\/p><p>\r\n<video controls> <source src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_BouncingBall\/bounceAnimation0.mp4\" type=\"video\/mp4\">Your browser does not support the video tag.<\/video>\r\n<\/p><p>Matthew has documented the code very well, so it's easy to pick up and understand. He's modularized the code so that you can customize it to your needs. For example, he has a single function <tt>Set_Parameters<\/tt> that, well... sets your parameters. The function <tt>groundHeight<\/tt> defines the terrain, and the function <tt>Animate<\/tt> creates the animation.<\/p><p>I added a bit of interactivity to the terrain creation, and here are some interesting terrains I tested.<\/p><p>\r\n<video controls> <source src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_BouncingBall\/bounceAnimation.mp4\" type=\"video\/mp4\">Your browser does not support the video tag.<\/video>\r\n<\/p><p>If you found this entry interesting, Matthew has contributed <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2853760-matthew-kelly?detail=fileexchange\">quite a bit<\/a> to the File Exchange. Be sure to check it out.<\/p><p><b>Comments<\/b><\/p><p>Let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=6664#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/54445-bouncing-ball--tutorial-for-ode45-events#comments\">comment<\/a> for Matthew.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_56234d35397742fe9c8aec547ef91edd() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='56234d35397742fe9c8aec547ef91edd ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 56234d35397742fe9c8aec547ef91edd';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_56234d35397742fe9c8aec547ef91edd()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2015b<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2015b<br><\/p><\/div><!--\r\n56234d35397742fe9c8aec547ef91edd ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/54445-bouncing-ball--tutorial-for-ode45-events \"Bouncing\r\n% Ball: Tutorial for |ode45| events\"> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2853760 Matthew\r\n% Kelly>.\r\n%\r\n% Using animation is a great way to convey information. By animating\r\n% something physical, it has an added effect of tying concepts with\r\n% experience or intuition. This tutorial by Matthew is about using events\r\n% with ordinary differential equation solver\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/ode45.html |ode45|>, or any\r\n% other ODE solver for that matter. He uses a bouncing ball example to show\r\n% the effect of events, represented as the act of the ball coming into\r\n% contact with the ground. Since a ball typically bounces over and over\r\n% again, he repeatedly calls |ode45| with each calculation separated by an\r\n% event (contact). The simulation takes into account the energy loss when\r\n% the ball bounces, defined by the\r\n% <https:\/\/en.wikipedia.org\/wiki\/Coefficient_of_restitution coefficient of\r\n% restitution>. Add to that a nice terrain defined by a few sinusoids, then\r\n% you get a nice animation of the ball bouncing all over the place.\r\n%\r\n% <html>\r\n% <video controls> <source src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_BouncingBall\/bounceAnimation0.mp4\" type=\"video\/mp4\">Your browser does not support the video tag.<\/video>\r\n% <\/html>\r\n%\r\n% Matthew has documented the code very well, so it's easy to pick up and\r\n% understand. He's modularized the code so that you can customize it to\r\n% your needs. For example, he has a single function |Set_Parameters| that,\r\n% well... sets your parameters. The function |groundHeight| defines the\r\n% terrain, and the function |Animate| creates the animation.\r\n%\r\n% I added a bit of interactivity to the terrain creation, and here are some\r\n% interesting terrains I tested.\r\n%\r\n% <html>\r\n% <video controls> <source src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_BouncingBall\/bounceAnimation.mp4\" type=\"video\/mp4\">Your browser does not support the video tag.<\/video>\r\n% <\/html>\r\n%\r\n% If you found this entry interesting, Matthew has contributed\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2853760-matthew-kelly?detail=fileexchange\r\n% quite a bit> to the File Exchange. Be sure to check it out.\r\n%\r\n% *Comments*\r\n%\r\n% Let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=6664#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/54445-bouncing-ball--tutorial-for-ode45-events#comments comment> for Matthew.\r\n\r\n##### SOURCE END ##### 56234d35397742fe9c8aec547ef91edd\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n\r\nJiro's pick this week is \"Bouncing Ball: Tutorial for ode45 events\" by Matthew Kelly.Using animation is a great way to convey information. By animating something physical, it has an added effect... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/03\/04\/bouncing-ball-animation-using-events-with-ode\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6664"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=6664"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6664\/revisions"}],"predecessor-version":[{"id":6666,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6664\/revisions\/6666"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=6664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=6664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=6664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}