{"id":438,"date":"2009-11-09T12:34:30","date_gmt":"2009-11-09T19:34:30","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=2387"},"modified":"2009-11-09T12:34:30","modified_gmt":"2009-11-09T19:34:30","slug":"bounce-ball-inside-a-circle","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=438","title":{"rendered":"Bounce Ball Inside a Circle"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\npackage  {\r\n\timport flash.display.Shape;\r\n\timport flash.display.Sprite;\r\n\timport flash.geom.Point;\r\n\r\n\t\/**\r\n\t * Re: http:\/\/board.flashkit.com\/board\/showthread.php?t=797453\r\n\t * @author makc\r\n\t * @license WTFPLv2\r\n\t *\/\r\n\tpublic class BouncingBall extends Sprite{\r\n\t\tpublic function BouncingBall () {\r\n\t\t\tr = 10;\r\n\t\t\tball = new Shape;\r\n\t\t\tball.graphics.beginFill (0);\r\n\t\t\tball.graphics.drawCircle (0, 0, r);\r\n\t\t\taddChild (ball);\r\n\t\t\tv = new Point;\r\n\t\t\tv.x = Math.random ();\r\n\t\t\tv.y = Math.random ();\r\n\t\t\tV = 1 + 20 * Math.random ();\r\n\t\t\tv.normalize (V);\r\n\t\t\tR = 200; X = 465 \/ 2; Y = 465 \/ 2;\r\n\t\t\tgraphics.lineStyle (0);\r\n\t\t\tgraphics.drawCircle (X, Y, R);\r\n\t\t\tball.x = X + 100;\r\n\t\t\tball.y = Y - 100;\r\n\t\t\taddEventListener (&quot;enterFrame&quot;, loop);\r\n\t\t}\r\n\t\tprivate var r:Number;\r\n\t\tprivate var ball:Shape;\r\n\t\tprivate var v:Point;\r\n\t\tprivate var V:Number;\r\n\t\tprivate var R:Number;\r\n\t\tprivate var X:Number;\r\n\t\tprivate var Y:Number;\r\n\t\tprivate function loop (e:*):void {\r\n\t\t\tball.x += v.x;\r\n\t\t\tball.y += v.y;\r\n\t\t\t\/\/ R-r vector\r\n\t\t\tvar P:Point = new Point (X - ball.x, Y - ball.y);\r\n\t\t\tif (P.length &gt; Math.sqrt ((R - r) * (R - r))) {\r\n\t\t\t\t\/\/ normalize R-r vector\r\n\t\t\t\tP.normalize (1);\r\n\t\t\t\t\/\/ project v onto it\r\n\t\t\t\tvar vp:Number = v.x * P.x + v.y * P.y;\r\n\t\t\t\t\/\/ subtract projection\r\n\t\t\t\tv.x -= 2 * vp * P.x;\r\n\t\t\t\tv.y -= 2 * vp * P.y;\r\n\t\t\t\tv.normalize (V);\r\n\t\t\t\t\/\/ move away from bounding circle\r\n\t\t\t\tP = new Point (X - ball.x, Y - ball.y);\r\n\t\t\t\twhile (P.length &gt; Math.sqrt ((R - r) * (R - r))) {\r\n\t\t\t\t\tball.x += v.x;\r\n\t\t\t\t\tball.y += v.y;\r\n\t\t\t\t\tP = new Point (X - ball.x, Y - ball.y);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p><a href=\"http:\/\/makc3d.wordpress.com\/\">Makc3d <\/a> said I could choose one of his excellent <a href=\"http:\/\/wonderfl.net\/\" target=\"blank\">wonderfl.net<\/a> pieces and submit it to the contest. This snippet creates a ball that bounces off the inside of a circle. I thought this was a pretty unique way to go about doing this &#8211; and found it easy to add gravity and other cool features to it.<\/p>\n<p><a href=\"http:\/\/wonderfl.net\/code\/afc32b0f8b425c3643193c2d407f034f21a02725\" target=\"blank\">Have a look at the swf over at wonderfl&#8230;.<\/a><\/p>\n<p>Some Makc3d links:<br \/>\n<a href=\"http:\/\/makc3d.wordpress.com\/\">>> http:\/\/makc3d.wordpress.com\/<\/a><br \/>\n<a href=\"http:\/\/code.google.com\/p\/makc\/ \">>>http:\/\/code.google.com\/p\/makc\/ <\/a><br \/>\n<a href=\"http:\/\/wonderfl.net\/user\/makc3d\">>> http:\/\/wonderfl.net\/user\/makc3d<\/a><\/p>\n<p><a href=\"http:\/\/makc3d.wordpress.com\/\">Makc3d <\/a> elaborated on his code a bit via e-mail. He said that his technique sacrifices accuracy for simplicity&#8230; and that if you simplify too much it would be easy to break the code. &#8220;e.g. comment out piece of code where it says &#8220;move away from bounding circle&#8221;&#8221;&#8230;<\/p>\n<p>Here is a picture that Makc3d drew explaining why you need to multiply by 2:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/why2.gif\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>package { import flash.display.Shape; import flash.display.Sprite; import flash.geom.Point; \/** * Re: http:\/\/board.flashkit.com\/board\/showthread.php?t=797453 * @author makc * @license WTFPLv2 *\/ public class BouncingBall extends Sprite{ public function BouncingBall () { r = 10; ball = new Shape; ball.graphics.beginFill (0); ball.graphics.drawCircle (0, 0, r); addChild (ball); v = new Point; v.x = Math.random (); v.y = Math.random [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[3,14,141,4],"class_list":["post-438","post","type-post","status-publish","format-standard","hentry","category-motion","tag-actionscript","tag-as3","tag-ascontest","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/438","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=438"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/438\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}