
{"id":771,"date":"2009-01-13T09:00:29","date_gmt":"2009-01-13T08:00:29","guid":{"rendered":"http:\/\/www.matthijskamstra.nl\/blog\/?p=771"},"modified":"2009-01-12T13:06:44","modified_gmt":"2009-01-12T12:06:44","slug":"hello-box2d-part-3","status":"publish","type":"post","link":"https:\/\/www.matthijskamstra.nl\/blog\/2009\/01\/13\/hello-box2d-part-3\/","title":{"rendered":"Hello box2D &#8211; part 3"},"content":{"rendered":"<p>In my <a href=\"http:\/\/www.matthijskamstra.nl\/blog\/index.php\/2009\/01\/12\/hello-box2d-part-2\/\">previous post<\/a> I update the &#8220;Hello world&#8221; for box2D so that you can see something visual happening.<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 make it a little bit more attractive (remove trace).<\/p>\n<p>First we change some settings to make it more fun to watch at!<\/p>\n<p>The <code>groundBodeDef<\/code> (strange name for something that is the ceiling) is moved more down so you can see where the <code>bodyDef<\/code> is bouncing off.<br \/>\n[as]<br \/>\n\/\/groundBodyDef.position.Set(0.0, -10.0);   \/\/ [mck]: old code<br \/>\ngroundBodyDef.position.Set(0.0, -9.0);<br \/>\n[\/as]<\/p>\n<p>The <code>bodyDef<\/code> is moved to the center and dropped a little higher<br \/>\n[as]<br \/>\n\/\/bodyDef.position.Set(0.0, 4.0); \t\/\/ [mck]: old code<br \/>\nbodyDef.position.Set(10.0, 10.0);<br \/>\n[\/as]<\/p>\n<p>And the <code>shapeDef<\/code> is bouncy so that you can see what box2D can do:<br \/>\n[as]<br \/>\nshapeDef.restitution = 0.8;   \/\/ [mck]: extra new code<br \/>\n[\/as]<\/p>\n<p>And delete the trace:<br \/>\n[as]<br \/>\nvar position:b2Vec2 = body.GetPosition();<br \/>\nvar angle:Number = body.GetAngle();<br \/>\ntrace(position.x +&#8217;,&#8217;+ position.y +&#8217;,&#8217;+ angle);<br \/>\n[\/as]<\/p>\n<h3>End result<\/h3>\n<p>The document class (<code>HelloWorld.as<\/code>) will look like this:<\/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 Sprite {<\/p>\n<p>\t\tprivate var body:b2Body;<br \/>\n\t\tprivate var world:b2World;<\/p>\n<p>\t\t\/\/ constructor<br \/>\n\t\tpublic function HelloWorld() {<br \/>\n\t\t\ttrace( &#8220;HelloWorld.HelloWorld&#8221; );<\/p>\n<p>\t\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\t\t\t\/\/ http:\/\/www.box2d.org\/wiki\/index.php?title=Manual\/AS3#Creating_a_World<br \/>\n\t\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\t\t\t\/\/ Create 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\t\/\/ var world:b2World = new b2World(worldAABB, gravity, doSleep);  \/\/ [mck]: old code<br \/>\n\t\t\tworld = new b2World(worldAABB, gravity, doSleep);<\/p>\n<p>\t\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\t\t\t\/\/ http:\/\/www.box2d.org\/wiki\/index.php?title=Manual\/AS3#Creating_a_Ground_Box<br \/>\n\t\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\t\t\tvar groundBodyDef:b2BodyDef = new b2BodyDef();<br \/>\n\t\t\tgroundBodyDef.position.Set(0.0, -10.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);<\/p>\n<p>\t\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\t\t\t\/\/ http:\/\/www.box2d.org\/wiki\/index.php?title=Manual\/AS3#Creating_a_Dynamic_Body<br \/>\n\t\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\t\t\tvar bodyDef:b2BodyDef = new b2BodyDef();<br \/>\n\t\t\tbodyDef.position.Set(0.0, 4.0);<br \/>\n\t\t\t\/\/ var body:b2Body = world.CreateBody(bodyDef);   \/\/ [mck]: old code<br \/>\n\t\t\tbody = 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\tbody.CreateShape(shapeDef);<br \/>\n\t\t\tbody.SetMassFromShapes();<\/p>\n<p>\t\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\t\t\t\/\/ http:\/\/www.box2d.org\/wiki\/index.php?title=Manual\/AS3#Debug_Drawing<br \/>\n\t\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\t\t\tvar debugDraw:b2DebugDraw = new b2DebugDraw();<br \/>\n\t\t\tdebugDraw.m_sprite = new Sprite();<\/p>\n<p>\t\t\taddChild(debugDraw.m_sprite);<\/p>\n<p>\t\t\tdebugDraw.m_drawScale = 30;<br \/>\n\t\t\tdebugDraw.m_fillAlpha = .25;<br \/>\n\t\t\tdebugDraw.m_lineThickness = 1;<br \/>\n\t\t\tdebugDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit ;<br \/>\n\t\t\tworld.SetDebugDraw (debugDraw);<\/p>\n<p>\t\t\t\/\/<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#Simulating_the_World_.28of_Box2D.29<br \/>\n\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\t\tpublic function onUpdateHandler(e:Event):void{<\/p>\n<p>\t\t\tvar timeStep:Number = 1.0 \/ 60.0;<br \/>\n\t\t\tvar iterations:Number = 10;<\/p>\n<p>\t\t\tworld.Step(timeStep, iterations);<\/p>\n<p>\t\t}<\/p>\n<p>\t} \/\/ end class<\/p>\n<p>} \/\/ end package<\/p>\n<p>[\/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<p>Is this it? No, lets make it better.<br \/>\nTomorrow more<\/p>\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 happening. You can find the original here: AS3: Hello Box2D. Today I will make it a little bit more attractive (remove trace). First we change some settings to make it more fun to watch at! The groundBodeDef [&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-771","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\/771","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=771"}],"version-history":[{"count":7,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts\/771\/revisions"}],"predecessor-version":[{"id":774,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts\/771\/revisions\/774"}],"wp:attachment":[{"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/media?parent=771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/categories?post=771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/tags?post=771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}