
{"id":749,"date":"2009-01-11T02:26:33","date_gmt":"2009-01-11T01:26:33","guid":{"rendered":"http:\/\/www.matthijskamstra.nl\/blog\/?p=749"},"modified":"2009-02-14T14:40:43","modified_gmt":"2009-02-14T13:40:43","slug":"hello-box2d-part-1","status":"publish","type":"post","link":"https:\/\/www.matthijskamstra.nl\/blog\/2009\/01\/11\/hello-box2d-part-1\/","title":{"rendered":"Hello box2D &#8211; part 1"},"content":{"rendered":"<p>In programming the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Hello_world_program\">Hello world<\/a> is probably the first you will ever make in a new language.<br \/>\nIn the box2D wiki you can find a &#8220;Hello world&#8221; in box2DFlash style: <a href=\"http:\/\/www.box2d.org\/wiki\/index.php?title=Manual\/AS3#Hello_Box2D\">AS3: Hello Box2D<\/a>.<\/p>\n<p>Easy as it should be &#8230; or is it?<br \/>\nThis is not the &#8220;Hello world&#8221; I expected, so I&#8217;ll post one that helps you more on your way.<\/p>\n<div class=\"update\">As <a href=\"http:\/\/www.matthijskamstra.nl\/blog\/index.php\/2009\/01\/11\/hello-box2d-part-1\/comment-page-1\/#comment-11372\">irongleet<\/a> mentions in the comments, this is not really a tutorial. There is already a tutorial on the page I mention above <a href=\"http:\/\/www.box2d.org\/wiki\/index.php?title=Manual\/AS3#Hello_Box2D\">AS3: Hello Box2D<\/a>. It&#8217;s not a bad tutorial, but you need to have some knowledge of AS3 to follow it <strong>and<\/strong> I personally don&#8217;t like the end result. So this is the first part of a series which will help you make more out of the previously mentioned tutorial. But I&#8217;m not going to copy past the tutorial written somewhere else and present it here. That&#8217;s why you should read the links to the source for more information. And the next <a href=\"http:\/\/www.matthijskamstra.nl\/blog\/index.php\/2009\/01\/12\/hello-box2d-part-2\/\"><strong>Hello box2d &#8211; part 2<\/strong><\/a> will have some more input from me.<\/div>\n<h3>Hello_Box2D<\/h3>\n<p><em>source: <a href=\"http:\/\/www.box2d.org\/wiki\/index.php?title=Manual\/AS3#Hello_Box2D\">Hello_Box2D<\/a><\/em><\/p>\n<p>Lets start with a Flash document width 640px, height 480px, backgroundColor=&#8221;#000000&#8243; and a framerate 60.<br \/>\nAnd add a document class: <code>HelloWorld<\/code>.<br \/>\nSave this .fla (for example <code>HelloWorldBox2d.fla<\/code>).<\/p>\n<p>Now get your favorite code-editor (I suggest <a href=\"http:\/\/www.flashdevelop.org\">FlashDevelop<\/a>) and create that document class, name it <code>HelloWorld.as<\/code>:<br \/>\n[as]<br \/>\npackage {<\/p>\n<p>\timport flash.display.Sprite;<br \/>\n\timport flash.events.Event;<br \/>\n\t\/\/ Classes used in this example<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>\tpublic class HelloWorld extends Sprite{<\/p>\n<p>\t\t\/\/ constructor<br \/>\n\t\tpublic function HelloWorld() {<br \/>\n\t\t\ttrace( &#8220;HelloWorld.HelloWorld&#8221; );<br \/>\n\t\t}<\/p>\n<p>\t} \/\/ end class<\/p>\n<p>} \/\/ end package<br \/>\n[\/as]<br \/>\nand save it next to the <code>HelloWorldBox2d.fla<\/code>.<\/p>\n<p>You can see that I already add the import needed for Box2D.<\/p>\n<p>The last thing you need to do is download the box2D classes: <a href=\"http:\/\/sourceforge.net\/projects\/box2dflash\">http:\/\/sourceforge.net\/projects\/box2dflash<\/a><br \/>\nExtract the zip file and copy the folder <code>box2d<\/code> next to the <code>HelloWorldBox2d.fla<\/code> and the <code>HelloWorld.as<\/code><\/p>\n<p>And now you are set&#8230;<\/p>\n<h3>Creating_a_World<\/h3>\n<blockquote><p>Every Box2D program begins with the creation of a world object. This is the physics hub that manages memory, objects, and simulation. <\/p><\/blockquote>\n<p><em>source: <a href=\"http:\/\/www.box2d.org\/wiki\/index.php?title=Manual\/AS3#Creating_a_World\">Creating_a_World<\/a><\/em><\/p>\n<p>The next piece of code can be placed in the constructor (public function HelloWorld() )<br \/>\n[as]<br \/>\n\/\/ Creat world AABB<br \/>\nvar worldAABB:b2AABB = new b2AABB();<br \/>\nworldAABB.lowerBound.Set(-100.0, -100.0);<br \/>\nworldAABB.upperBound.Set(100.0, 100.0);<\/p>\n<p>\/\/ Define the gravity vector<br \/>\nvar gravity:b2Vec2 = new b2Vec2 (0.0, -10.0); <\/p>\n<p>\/\/ Allow bodies to sleep<br \/>\nvar doSleep:Boolean = true;<\/p>\n<p>\/\/ Construct a world object<br \/>\nvar world:b2World = new b2World(worldAABB, gravity, doSleep);<br \/>\n[\/as]<\/p>\n<h3>Creating_a_Ground_Box<\/h3>\n<p><em>source: <a href=\"http:\/\/www.box2d.org\/wiki\/index.php?title=Manual\/AS3#Creating_a_Ground_Box\">Creating_a_Ground_Box<\/a><\/em><\/p>\n<p>[as]<br \/>\nvar groundBodyDef:b2BodyDef = new b2BodyDef();<br \/>\ngroundBodyDef.position.Set(0.0, -10.0);<\/p>\n<p>var groundBody:b2Body = world.CreateBody(groundBodyDef);<\/p>\n<p>var groundShapeDef:b2PolygonDef = new b2PolygonDef();<br \/>\ngroundShapeDef.SetAsBox(50.0, 10.0);<\/p>\n<p>groundBody.CreateShape(groundShapeDef);<br \/>\n[\/as]<\/p>\n<h3>Creating_a_Dynamic_Body<\/h3>\n<blockquote><p>We can use the same technique to create a dynamic body. The main difference, besides dimensions, is that we must establish the dynamic body&#8217;s mass properties. <\/p><\/blockquote>\n<p><em>source: <a href=\"http:\/\/www.box2d.org\/wiki\/index.php?title=Manual\/AS3#Creating_a_Dynamic_Body\">Creating_a_Dynamic_Body<\/a><\/em><\/p>\n<p>[as]<br \/>\nvar bodyDef:b2BodyDef = new b2BodyDef();<br \/>\nbodyDef.position.Set(0.0, 4.0);<br \/>\nvar body:b2Body = world.CreateBody(bodyDef);<\/p>\n<p>var shapeDef:b2PolygonDef = new b2PolygonDef();<br \/>\nshapeDef.SetAsBox(1.0, 1.0);<br \/>\nshapeDef.density = 1.0;<br \/>\nshapeDef.friction = 0.3;<br \/>\nbody.CreateShape(shapeDef);<br \/>\nbody.SetMassFromShapes();<br \/>\n[\/as]<\/p>\n<h3>Simulating_the_World<\/h3>\n<blockquote><p>So we have initialized the ground box and a dynamic box. Now we are ready to set Newton loose to do his thing. We just have a couple more issues to consider. <\/p><\/blockquote>\n<p><em>source: <a href=\"http:\/\/www.box2d.org\/wiki\/index.php?title=Manual\/AS3#Simulating_the_World_.28of_Box2D.29\">Simulating_the_World<\/a><\/em><br \/>\n[as]<br \/>\nvar timeStep:Number \t= 1.0 \/ 60.0;<br \/>\nvar iterations:Number \t= 10;<\/p>\n<p>for (var i:Number = 0; i < 60; ++i) {\n\tworld.Step(timeStep, iterations);\n\tvar position:b2Vec2 = body.GetPosition();\n\tvar angle:Number = body.GetAngle();\n\ttrace(position.x +','+ position.y +','+ angle);\n}\n[\/as]\n\n\n\n\n<h3>End result<\/h3>\n<p>The document class (<code>HelloWorld.as<\/code>) will look like this:<\/p>\n<div class=\"update\">I didn&#8217;t wrote the code for this tutorial, so this is collection of code copy\/paste from the original <a href=\"http:\/\/www.box2d.org\/wiki\/index.php?title=Manual\/AS3#Hello_Box2D\">AS3: Hello Box2D<\/a> tutorial. But I did, as many probably did, the original tutorial and I was a little disappointed at the end result&#8230; so my addition to the original tutorial can be read in the next part of this series (<a href=\"http:\/\/www.matthijskamstra.nl\/blog\/index.php\/2009\/01\/12\/hello-box2d-part-2\/\"><strong>Hello box2d &#8211; part 2<\/strong><\/a>) <\/div>\n<p>[as]<br \/>\npackage {<\/p>\n<p>\timport flash.display.Sprite;<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>\tpublic class HelloWorld2 extends Sprite {<\/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\tvar world:b2World = 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\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\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#Simulating_the_World_.28of_Box2D.29<br \/>\n\t\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\t\t\tvar timeStep:Number \t= 1.0 \/ 60.0;<br \/>\n\t\t\tvar iterations:Number \t= 10;<\/p>\n<p>\t\t\tfor (var i:Number = 0; i < 60; ++i) {\n\t\t\t\tworld.Step(timeStep, iterations);\n\t\t\t\tvar position:b2Vec2 = body.GetPosition();\n\t\t\t\tvar angle:Number = body.GetAngle();\n\t\t\t\ttrace(position.x +','+ position.y +','+ angle);\n\t\t\t}\n\t\t}\n\t\t\n\t} \/\/ end class\n\n} \/\/ end package\n[\/as]\n\nSo all you need to do is CTRL+ENTER.\n\nNice effect isn't it? No? Only some numbers in the output panel?\nYeah I know that was not very useful...\n\nSo next post will be a better\/improved version of the <strong>Hello world box2D style<\/strong> >> read <a href=\"http:\/\/www.matthijskamstra.nl\/blog\/index.php\/2009\/01\/12\/hello-box2d-part-2\/\"><strong>Hello box2d &#8211; part 2<\/strong><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In programming the Hello world is probably the first you will ever make in a new language. In the box2D wiki you can find a &#8220;Hello world&#8221; in box2DFlash style: AS3: Hello Box2D. Easy as it should be &#8230; or is it? This is not the &#8220;Hello world&#8221; I expected, so I&#8217;ll post one that [&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-749","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\/749","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=749"}],"version-history":[{"count":10,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts\/749\/revisions"}],"predecessor-version":[{"id":764,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts\/749\/revisions\/764"}],"wp:attachment":[{"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/media?parent=749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/categories?post=749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/tags?post=749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}