
{"id":783,"date":"2009-01-15T09:00:32","date_gmt":"2009-01-15T08:00:32","guid":{"rendered":"http:\/\/www.matthijskamstra.nl\/blog\/?p=783"},"modified":"2009-02-14T14:53:14","modified_gmt":"2009-02-14T13:53:14","slug":"hello-box2d-part-4","status":"publish","type":"post","link":"https:\/\/www.matthijskamstra.nl\/blog\/2009\/01\/15\/hello-box2d-part-4\/","title":{"rendered":"Hello box2D &#8211; part 4"},"content":{"rendered":"<p>In my <a href=\"http:\/\/www.matthijskamstra.nl\/blog\/index.php\/2009\/01\/13\/hello-box2d-part-3hello-box2d-part-3\/\">previous post<\/a> I update the &#8220;Hello world&#8221; for box2D so that you can see something visual interesting.<br \/>\nYou can find the original here: <a href=\"http:\/\/www.box2d.org\/wiki\/index.php?title=Manual\/AS3#Hello_Box2D\">AS3: Hello Box2D<\/a>.<\/p>\n<p>Today I will post the same code but now how it should be.<br \/>\nKeeping the <a href=\"http:\/\/opensource.adobe.com\/wiki\/display\/flexsdk\/Coding+Conventions\">coding conventions<\/a> in mind and making it easier to reuse code.<\/p>\n<p>Still keeping in mind the original <a href=\"http:\/\/www.box2d.org\/wiki\/index.php?title=Manual\/AS3#Hello_Box2D\">Hello world<\/a><\/p>\n<p>[as]<br \/>\npackage {<\/p>\n<p>\timport flash.display.*;<br \/>\n\timport flash.events.*;<br \/>\n\t\/\/ Box2D Classes used in this &#8220;Hello world&#8221;<br \/>\n\timport Box2D.Dynamics.*;<br \/>\n\timport Box2D.Collision.*;<br \/>\n\timport Box2D.Collision.Shapes.*;<br \/>\n\timport Box2D.Common.Math.*;<\/p>\n<p>\t\/\/[SWF(width=&#8221;640&#8243;, height=&#8221;480&#8243;, backgroundColor=&#8221;#000000&#8243;, frameRate=&#8221;60&#8243;)]<br \/>\n\tpublic class HelloWorld extends MovieClip {<\/p>\n<p>\t\tpublic var world:b2World;<br \/>\n\t\tpublic var iterations:int \t\t= 10;<br \/>\n\t\tpublic var timeStep:Number \t\t= 1.0 \/ 60.0;<br \/>\n\t\tpublic var worldScale:Number \t= 30.0;<\/p>\n<p>\t\t\/\/ to see what you have created<br \/>\n\t\tprivate var debugDraw:b2DebugDraw;<br \/>\n\t\tprivate var isDebugDrawing:Boolean = true;\t\/\/ change to false and there is no debugDraw<\/p>\n<p>\t\t\/\/ constructor<br \/>\n\t\tpublic function HelloWorld() {<br \/>\n\t\t\t\/\/trace( &#8220;HelloWorld.HelloWorld&#8221; );<br \/>\n\t\t\tinit();<br \/>\n\t\t}<\/p>\n<p>\t\t\/**<br \/>\n\t\t * lets start building our &#8220;Hello world&#8221; box2D program<br \/>\n\t\t *\/<br \/>\n\t\tprivate function init():void {<br \/>\n\t\t\ttrace( &#8220;HelloWorld.init&#8221; );<\/p>\n<p>\t\t\tcreateWorld();<br \/>\n\t\t\tcreatingGroundBox();<br \/>\n\t\t\tcreatingDynamicBody();<\/p>\n<p>\t\t\tsetupDebugDraw();<\/p>\n<p>\t\t\t\/\/ Add event for main loop<br \/>\n\t\t\taddEventListener(Event.ENTER_FRAME, onUpdateHandler, false, 0, true);<br \/>\n\t\t}<\/p>\n<p>\t\t\/**<br \/>\n\t\t * http:\/\/www.box2d.org\/wiki\/index.php?title=Manual\/AS3#Creating_a_World<br \/>\n\t\t *\/<br \/>\n\t\tprivate function createWorld():void {<br \/>\n\t\t\t\/\/ Creat world AABB<br \/>\n\t\t\tvar worldAABB:b2AABB = new b2AABB();<br \/>\n\t\t\tworldAABB.lowerBound.Set(-100.0, -100.0);<br \/>\n\t\t\tworldAABB.upperBound.Set(100.0, 100.0);<\/p>\n<p>\t\t\t\/\/ Define the gravity vector<br \/>\n\t\t\tvar gravity:b2Vec2 = new b2Vec2 (0.0, -10.0); <\/p>\n<p>\t\t\t\/\/ Allow bodies to sleep<br \/>\n\t\t\tvar doSleep:Boolean = true;<\/p>\n<p>\t\t\t\/\/ Construct a world object<br \/>\n\t\t\tworld = new b2World(worldAABB, gravity, doSleep);<br \/>\n\t\t}<\/p>\n<p>\t\t\/**<br \/>\n\t\t * http:\/\/www.box2d.org\/wiki\/index.php?title=Manual\/AS3#Creating_a_Ground_Box<br \/>\n\t\t *\/<br \/>\n\t\tprivate function creatingGroundBox():void {<br \/>\n\t\t\tvar groundBodyDef:b2BodyDef = new b2BodyDef();<br \/>\n\t\t\tgroundBodyDef.position.Set(0.0, -9.0);<\/p>\n<p>\t\t\tvar groundBody:b2Body = world.CreateBody(groundBodyDef);<\/p>\n<p>\t\t\tvar groundShapeDef:b2PolygonDef = new b2PolygonDef();<br \/>\n\t\t\tgroundShapeDef.SetAsBox(50.0, 10.0);<\/p>\n<p>\t\t\tgroundBody.CreateShape(groundShapeDef);<br \/>\n\t\t}<\/p>\n<p>\t\t\/**<br \/>\n\t\t * http:\/\/www.box2d.org\/wiki\/index.php?title=Manual\/AS3#Creating_a_Dynamic_Body<br \/>\n\t\t *\/<br \/>\n\t\tprivate function creatingDynamicBody ():void {<br \/>\n\t\t\tvar bodyDef:b2BodyDef = new b2BodyDef();<br \/>\n\t\t\tbodyDef.position.Set(10.0, 10.0);<br \/>\n\t\t\tvar body:b2Body = world.CreateBody(bodyDef);<\/p>\n<p>\t\t\tvar shapeDef:b2PolygonDef = new b2PolygonDef();<br \/>\n\t\t\tshapeDef.SetAsBox(1.0, 1.0);<br \/>\n\t\t\tshapeDef.density = 1.0;<br \/>\n\t\t\tshapeDef.friction = 0.3;<br \/>\n\t\t\tshapeDef.restitution = 0.8;<br \/>\n\t\t\tbody.CreateShape(shapeDef);<br \/>\n\t\t\tbody.SetMassFromShapes();<\/p>\n<p>\t\t}<\/p>\n<p>\t\t\/\/ extra to visualize<br \/>\n\t\tprivate function setupDebugDraw():void {<br \/>\n\t\t\tif(isDebugDrawing) {<br \/>\n\t\t\t\tdebugDraw = new b2DebugDraw();<br \/>\n\t\t\t\tdebugDraw.m_sprite = new Sprite();<\/p>\n<p>\t\t\t\taddChild(debugDraw.m_sprite);<\/p>\n<p>\t\t\t\tdebugDraw.m_drawScale = 30;<br \/>\n\t\t\t\tdebugDraw.m_fillAlpha = .25;<br \/>\n\t\t\t\tdebugDraw.m_lineThickness = 1;<br \/>\n\t\t\t\tdebugDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit ;<br \/>\n\t\t\t\tworld.SetDebugDraw (debugDraw);<br \/>\n\t\t\t}<br \/>\n\t\t}<\/p>\n<p>\t\t\/**<br \/>\n\t\t * http:\/\/www.box2d.org\/wiki\/index.php?title=Manual\/AS3#Simulating_the_World_.28of_Box2D.29<br \/>\n\t\t *\/<br \/>\n\t\tpublic function onUpdateHandler(e:Event):void{<br \/>\n\t\t\tworld.Step(timeStep, iterations);<br \/>\n\t\t}<\/p>\n<p>\t} \/\/ end class<\/p>\n<p>} \/\/ end package<br \/>\n[\/as]<\/p>\n<p>So all you need to do is CTRL+ENTER.<\/p>\n<p>And you will have something like this:<\/p>\n<div class=\"flash\">[swf]http:\/\/www.matthijskamstra.nl\/blog\/wp-content\/uploads\/helloworldbox2d_nice.swf,520, 390[\/swf]<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In my previous post I update the &#8220;Hello world&#8221; for box2D so that you can see something visual interesting. You can find the original here: AS3: Hello Box2D. Today I will post the same code but now how it should be. Keeping the coding conventions in mind and making it easier to reuse code. Still [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,3],"tags":[408,230,231,398,159],"class_list":["post-783","post","type-post","status-publish","format-standard","hentry","category-as3","category-flash","tag-as3","tag-box2d","tag-box2dflash","tag-flash","tag-flashdevelop"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts\/783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/comments?post=783"}],"version-history":[{"count":4,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts\/783\/revisions"}],"predecessor-version":[{"id":869,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts\/783\/revisions\/869"}],"wp:attachment":[{"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/media?parent=783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/categories?post=783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/tags?post=783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}