{"id":382,"date":"2009-08-17T06:12:17","date_gmt":"2009-08-17T13:12:17","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=2014"},"modified":"2009-08-17T06:12:17","modified_gmt":"2009-08-17T13:12:17","slug":"quickbox2d-unicycler","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=382","title":{"rendered":"QuickBox2D Unicycler"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nimport com.actionsnippet.qbox.*;\r\nimport Box2D.Common.Math.*;\r\nimport Box2D.Dynamics.Joints.*;\r\n&#x5B;SWF (backgroundColor=0x000000, width=700, height=600, frameRate=60)]\r\nvar sim:QuickBox2D = new QuickBox2D(this, {debug:false,gravityY:10, renderJoints:false});\r\nsim.setDefault({fillColor:0xCC0000,lineAlpha:0});\r\nsim.createStageWalls();\r\n\r\nfor (var i:int = 0; i&lt;20; i++){\r\n\tsim.addCircle({x:1+i * 0.8, y:1, radius:0.1+Math.random()*0.1, restitution:0.3});\r\n}\r\n\r\nsim.setDefault({fillColor:0xEFEFEF, fillAlpha:0.5, lineColor:0xFFFFFF, groupIndex:-2});\r\nvar ancA:QuickObject = sim.addCircle({x:20, y:12, radius:0.5, density:0, skin:&quot;none&quot;});\r\nvar ancB:QuickObject = sim.addCircle({x:20, y:2, radius:0.5, density:0, skin:&quot;none&quot;});\r\nvar head:QuickObject = sim.addCircle({x:0, y:-0.6, radius:0.5});\r\nvar neck:QuickObject = sim.addBox({x:0, y:1, width:0.5, height:2});\r\nvar spindle:QuickObject = sim.addBox({x:0, y:3, width:0.1, height:2})\r\nspindle.userData.visible = false;\r\nvar top:QuickObject = sim.addGroup({objects:&#x5B;head, neck, spindle], x:10, y:5, fixedRotation:true});\r\nvar upperLegA:QuickObject = sim.addBox({x:10, y:7.5, width:0.25, height:1.5});\r\nvar lowerLegA:QuickObject = sim.addBox({x:10, y:9, width:0.25, height:1.5});\r\nvar wheel:QuickObject = sim.addCircle({x:10, y:9, radius:1});\r\nvar upperLegB:QuickObject = sim.addBox({x:10, y:7.5, width:0.25, height:1.5});\r\nvar lowerLegB:QuickObject = sim.addBox({x:10, y:9, width:0.25, height:1.5});\r\nvar upperArmA:QuickObject = sim.addBox({x:10.7, y:5.25, width:1.5, height:0.25});\r\nvar lowerArmA:QuickObject = sim.addBox({x:11.8, y:5.25, width:1.2, height:0.25});\r\nvar upperArmB:QuickObject = sim.addBox({x:9.5, y:5.25, width:1.5, height:0.25});\r\nvar lowerArmB:QuickObject = sim.addBox({x:8.4, y:5.25, width:1.2, height:0.25});\r\nvar anchor:b2Vec2 = new b2Vec2();\r\nfunction connect(a:QuickObject, b:QuickObject, lower:Number, upper:Number, offX:Number=0, offY:Number = 0):QuickObject{\r\n\t var min:Number = Math.min(a.y, b.y);\r\n     var max:Number = Math.max(a.y, b.y);\r\n     anchor.y = min + (max - min) * 0.5 + offY;\r\n     min = Math.min(a.x, b.x);\r\n     max = Math.max(a.x, b.x);\r\n     anchor.x = min + (max - min) * 0.5 + offX;\r\n\t return sim.addJoint({type:&quot;revolute&quot;, a:a.body, b:b.body, x1:anchor.x, y1:anchor.y, lowerAngle:lower, upperAngle:upper});\r\n}\r\nsim.addJoint({type:&quot;revolute&quot;, a:wheel.body, b:top.body, x1:wheel.x, y1:wheel.y});\r\nsim.addJoint({type:&quot;revolute&quot;, a:lowerLegA.body, b:wheel.body, x1:lowerLegA.x, y1:lowerLegA.y + 0.7});\r\nsim.setDefault({enableLimit:true});\r\nconnect(top, upperArmA, 0, 3, -.3, .1);\r\nconnect(upperArmA, lowerArmA, -2.5, -1, 0, 0);\r\nconnect(top, upperArmB, -3, 0, .3, .1);\r\nconnect(upperArmB, lowerArmB, -2.5, -1, 0, 0);\r\nsim.addJoint({type:&quot;distance&quot;, a:lowerArmA.body, b:ancA.body, frequencyHz:0.3});\r\nsim.addJoint({type:&quot;distance&quot;, a:lowerArmB.body, b:ancB.body, frequencyHz:0.3});\r\nsim.setDefault({enableLimit:true});\r\nconnect(top, upperLegA, -6.28, 6, 0, 0.8);\r\nconnect(upperLegA, lowerLegA, .1, 6);\r\nwheel.angle = Math.PI;\r\nconnect(top, upperLegB, -6.28, 6, 0, 0.8);\r\nconnect(upperLegB, lowerLegB, .1, 6);\r\nsim.setDefault({enableLimit:false, type:&quot;revolute&quot;});\r\nsim.addJoint({a:lowerLegB.body, b:wheel.body, x1:lowerLegB.x, y1:lowerLegB.y + 0.7});\r\n\r\nsim.start();\r\nsim.mouseDrag();\r\n\r\nvar sd:Number = 5, s:Number = 0, t:Number = 0;\r\naddEventListener(Event.ENTER_FRAME, function():void{\r\n\ttop.angle = Math.cos(t += 0.05) * 0.2;\r\n    if (top.x &gt; 19) sd = -5\r\n\tif (top.x &lt; 5) sd = 5;\r\n\ts += (sd - s) \/ 8;\r\n\twheel.body.SetAngularVelocity(s);\r\n});\r\n<\/pre>\n<p>This chunk of code uses QuickBox2D to create a simple looking character riding a unicycle&#8230; <\/p>\n<p><a data-asruffle-linked=\"true\" href=\"http:\/\/www.actionsnippet.com\/swfs\/unicycler.html\" target=\"blank\"><\/p>\n<p>Have a look at the swf&#8230;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/cycler.gif\" \/><\/a> <span class=\"asruffle-link\" data-swf=\"{&quot;url&quot;:&quot;https:\\\/\\\/actionsnippet.com\\\/swfs\\\/unicycler.swf&quot;,&quot;title&quot;:&quot;unicycler&quot;,&quot;width&quot;:700,&quot;height&quot;:600,&quot;missing&quot;:false,&quot;allowScriptAccess&quot;:false,&quot;backgroundColor&quot;:&quot;#000000&quot;}\"><button type=\"button\" hidden aria-expanded=\"false\">Play demo<span class=\"screen-reader-text\">: unicycler<\/span><\/button><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>import com.actionsnippet.qbox.*; import Box2D.Common.Math.*; import Box2D.Dynamics.Joints.*; &#x5B;SWF (backgroundColor=0x000000, width=700, height=600, frameRate=60)] var sim:QuickBox2D = new QuickBox2D(this, {debug:false,gravityY:10, renderJoints:false}); sim.setDefault({fillColor:0xCC0000,lineAlpha:0}); sim.createStageWalls(); for (var i:int = 0; i&lt;20; i++){ sim.addCircle({x:1+i * 0.8, y:1, radius:0.1+Math.random()*0.1, restitution:0.3}); } sim.setDefault({fillColor:0xEFEFEF, fillAlpha:0.5, lineColor:0xFFFFFF, groupIndex:-2}); var ancA:QuickObject = sim.addCircle({x:20, y:12, radius:0.5, density:0, skin:&quot;none&quot;}); var ancB:QuickObject = sim.addCircle({x:20, y:2, radius:0.5, density:0, skin:&quot;none&quot;}); var [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,13,12],"tags":[3,14,15,4,16],"class_list":["post-382","post","type-post","status-publish","format-standard","hentry","category-box2d","category-motion","category-quickbox2d","tag-actionscript","tag-as3","tag-box2d","tag-flash","tag-quickbox2d"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/382","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=382"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/382\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}