{"id":270,"date":"2009-04-30T14:36:50","date_gmt":"2009-04-30T21:36:50","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1254"},"modified":"2009-04-30T14:36:50","modified_gmt":"2009-04-30T21:36:50","slug":"quick-box2d","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=270","title":{"rendered":"Quick Box2D"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n&#x5B;SWF(backgroundColor = 0x333333)]\r\n\r\nimport com.actionsnippet.qbox.*;\r\n\r\n\/\/ setting debug = true, will use the Box2D debug rendering\r\nvar sim:QuickBox2D=new QuickBox2D(this, {debug:false, gravityY:10});\r\n\r\n\/\/ creates static boxes on all sides of the screen\r\nsim.createStageWalls({lineAlpha:0, fillColor:0xFF9900});\r\n\r\n\/\/ add 25 boxes\r\nfor (var i:int = 0; i&lt;25; i++){\r\n\tvar xp:Number = 3 + (i % 5);\r\n\tvar yp:Number = 1 + int( i \/ 5);\r\n\tsim.addBox({x:xp, y:yp, width:1, height:1, fillColor: i * 10 &lt;&lt; 16, lineAlpha:0, angularDamping:5});\r\n}\r\n\r\nsim.addBox({x:7, y:10, width:3, height:.2, angle:-.3, density:0,lineAlpha:0, fillColor:0xFF9900});\r\n\r\nsim.addCircle({x:3, y:14, radius:2, fillColor:0xCC0000, lineColor:0x333333});\r\n\r\nsim.addPoly({x:13, y:5, verts:&#x5B;&#x5B;1,0,2,2,1,1.33],&#x5B;1,0,1,1.33,0,2]], angle: .4, density:1});\r\n\r\n\/\/ begins the simulation\r\nsim.start();\r\n\r\n\/\/ all non-static objects can be dragged\r\nsim.mouseDrag();\r\n<\/pre>\n<p>I&#8217;ve been messing with Box2D for the past few weeks &#8211; I decided to create a library (QuickBox2D) for quick prototyping. This snippet makes use of this library, so in order to run it you&#8217;ll need to download the zip included at the end of this post&#8230;<\/p>\n<p>If you don&#8217;t already know, Box2D is a great physics library created by <a href=\"http:\/\/www.gphysics.com\/\">Erin Catto<\/a>. It was ported to AS3 by Matthew Bush and John Nesky. You can download the AS3 version of the library <a href=\"http:\/\/sourceforge.net\/projects\/box2dflash\" target=\"blank\">here<\/a>.<\/p>\n<p>Today&#8217;s snippet creates this swf (click any image to view demo):<\/p>\n<p><a data-asruffle-linked=\"true\" href=\"https:\/\/actionsnippet.com\/qb2d\/test.html\" target=\"blank\"><img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/qb3d_test.gif\" \/><\/a> <span class=\"asruffle-link\" data-swf=\"{&quot;url&quot;:&quot;https:\\\/\\\/actionsnippet.com\\\/qb2d\\\/test.swf&quot;,&quot;title&quot;:&quot;test&quot;,&quot;width&quot;:780,&quot;height&quot;:600,&quot;missing&quot;:false,&quot;allowScriptAccess&quot;:false,&quot;backgroundColor&quot;:&quot;#333333&quot;}\"><button type=\"button\" hidden aria-expanded=\"false\">Play demo<span class=\"screen-reader-text\">: test<\/span><\/button><\/span><\/p>\n<h1>QuickBox2D<\/h1>\n<p>QuickBox2D simplifies Box2D instantiation and makes it easy to skin rigid bodies with Library assets&#8230; normally you need to do something like this to create a box in Box2D:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar bodyDef:b2BodyDef = new b2BodyDef();\r\nbodyDef.position.Set(3, 3);\r\nvar boxDef:b2PolygonDef = new b2PolygonDef();\r\nboxDef.SetAsBox(1, 1);\r\nboxDef.density = 1;\r\nvar body:b2Body = w.CreateBody(bodyDef);\r\nbody.CreateShape(boxDef);\r\nbody.SetMassFromShapes();\r\n<\/pre>\n<p>and this doesn&#8217;t include setting up the physics world and the main loop to run the simulation&#8230; after writing a good deal of repetitive code I decided to make QuickBox2D. After writing lots of QuickBox2D style instantiation I decided to create an editor so that I could just draw circles, boxes, polygons and joints and then test and save them in real time (will be posting this editor in the near future). The next few posts will be about this mini-library and I&#8217;ll be writing up some real documentation for it&#8230; as of now, it doesn&#8217;t have any&#8230;<\/p>\n<p>You can download Box2D <a href=\"http:\/\/sourceforge.net\/projects\/box2dflash\" target=\"blank\">here<\/a><\/p>\n<p>You can download QuickBox2D <a href=\"https:\/\/actionsnippet.com\/qb2d\/QuickBox2D_alpha101.zip\">here<\/a><\/p>\n<p>and you can view some AS3 Box2D demos <a href=\"http:\/\/box2dflash.sourceforge.net\/\" target=\"blank\">here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#x5B;SWF(backgroundColor = 0x333333)] import com.actionsnippet.qbox.*; \/\/ setting debug = true, will use the Box2D debug rendering var sim:QuickBox2D=new QuickBox2D(this, {debug:false, gravityY:10}); \/\/ creates static boxes on all sides of the screen sim.createStageWalls({lineAlpha:0, fillColor:0xFF9900}); \/\/ add 25 boxes for (var i:int = 0; i&lt;25; i++){ var xp:Number = 3 + (i % 5); var yp:Number = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,12],"tags":[3,4],"class_list":["post-270","post","type-post","status-publish","format-standard","hentry","category-motion","category-quickbox2d","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/270","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=270"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/270\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}