<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ClickPopMedia &#187; Box2DFlash</title>
	<atom:link href="http://www.clickpopmedia.com/tag/box2dflash/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.clickpopmedia.com</link>
	<description>ClickPopMedia is a great little design and illustration firm.</description>
	<lastBuildDate>Thu, 03 Dec 2009 17:28:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Changes to Box2DFlash “Hello World” example.</title>
		<link>http://www.clickpopmedia.com/2008/04/29/specific-changes-to-box2dflash-%e2%80%9chello-world%e2%80%9d-example/</link>
		<comments>http://www.clickpopmedia.com/2008/04/29/specific-changes-to-box2dflash-%e2%80%9chello-world%e2%80%9d-example/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 00:28:36 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Paul]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Box2DFlash]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Intro]]></category>
		<category><![CDATA[Physics]]></category>

		<guid isPermaLink="false">http://www.clickpopmedia.com/?p=281</guid>
		<description><![CDATA[I&#8217;ve talked about the basics of Box2DFlash v1.4.3 in my Introductory tutorial.  I showed how to create your world and how to introduce simple bodies into that world.  With the release of Box2DFlash v2.0.0 the basics remain mostly the same, there are a few details changed.  I need to go over these [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve talked about the basics of Box2DFlash v1.4.3 in my <a href="http://www.clickpopmedia.com/2008/03/17/physics-in-actionscript-3/">Introductory tutorial</a>.  I showed how to create your world and how to introduce simple bodies into that world.  With the release of Box2DFlash v2.0.0 the basics remain mostly the same, there are a few details changed.  I need to go over these small changes before I move on to the next step of adding joints and forces to the bodies.</p>
<p>The first and biggest change (although really a small thing) is that we no longer work in pixels!  We now work in units that by default equal 30 pixels to 1 unit (think of it as meters; 30px : 1m).  With this change our normal gravity is no longer (0, 300) but instead (1, 10).  This makes more sense to me at least because gravity in reality is 9.81 m/s/s.  This is going to effect almost everything you do, from applying forces to setting the width and height of a box.</p>
<p><span id="more-281"></span></p>
<p>The reason for such a change (I believe) is to make it easier for you to dynamically scale a project.  Just change the ratio of pixels per unit and everything should scale correctly.</p>
<p>The second change is the way we add objects to the world.  The creation of a new shape and object is still very much the same, but we now have the ability to adjust the center of mass.  For now, just leaving the center of mass to auto calculate we need to get the body object when adding the new bodyDef to the world.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">var</span> body:b2Body;<br />
<span class="kw2">var</span> bodyDef:b2BodyDef;<br />
<span class="kw2">var</span> boxDef:b2PolygonDef;</p>
<p><span class="co1">// Add ground body</span><br />
bodyDef = <span class="kw2">new</span> b2BodyDef<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
bodyDef.<span class="kw3">position</span>.<span class="kw3">Set</span><span class="br0">&#40;</span><span class="nu0">10</span>, <span class="nu0">12</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">//Small numbers&#8230; &nbsp;remember it&#8217;s scaled 30:1</span><br />
boxDef = <span class="kw2">new</span> b2PolygonDef<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
boxDef.<span class="me1">SetAsBox</span><span class="br0">&#40;</span><span class="nu0">30</span>, <span class="nu0">3</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">//THIS IS NEW!</span><br />
boxDef.<span class="me1">friction</span> = <span class="nu0">0.3</span>;</p>
<p><span class="co1">// Add sprite to body userData</span><br />
bodyDef.<span class="me1">userData</span> = <span class="kw2">new</span> PhysGround<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
bodyDef.<span class="me1">userData</span>.<span class="kw3">width</span> = <span class="nu0">30</span> * <span class="nu0">2</span> * <span class="nu0">30</span>;<br />
bodyDef.<span class="me1">userData</span>.<span class="kw3">height</span> = <span class="nu0">30</span> * <span class="nu0">2</span> * <span class="nu0">3</span>;<br />
addChild<span class="br0">&#40;</span>bodyDef.<span class="me1">userData</span><span class="br0">&#41;</span>;<br />
body = m_world.<span class="me1">CreateStaticBody</span><span class="br0">&#40;</span>bodyDef<span class="br0">&#41;</span>; &nbsp;<span class="co1">//THIS IS MOSTLY NEW</span><br />
body.<span class="me1">CreateShape</span><span class="br0">&#40;</span>boxDef<span class="br0">&#41;</span>; &nbsp;<span class="co1">//THIS IS NEW</span><br />
body.<span class="me1">SetMassFromShapes</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">//THIS IS NEW</span></div>
<p>Haha!  I guess I forgot to mention that we no longer have a b2BoxDef anymore&#8230;  we only have b2PolyDef (renamed b2PolygonDef).  We can quickly define it as a box though using the method <strong>public function SetAsBox(hx:Number, hy:Number):void</strong>.</p>
<p>Anyways, let us bring our attention back to adding the object to the world.  When you add the object to the world, you are creating it as either a static (unmoving) body or as a dynamic body (<strong>body = m_world.CreateDynamicBody(bodyDef);</strong>).<br />
It then returns the new body object BY REFRENCE.  It is not giving you a copy of that object.  This way when we make changes, such as add the shape and set the mass (center of mass really) we don&#8217;t have to go send it back again.</p>
<p>The method <strong>public function SetMassFromShapes():void</strong> is the way to auto calculate and set your center of mass.  Otherwise you have to us <strong>public function SetMass(massData:b2MassData):void</strong>, but this is a whole new complication.</p>
<p>OK!  With that, we are pretty much back up to speed after my last tutorial.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.clickpopmedia.com/2008/04/29/specific-changes-to-box2dflash-%e2%80%9chello-world%e2%80%9d-example/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Box2DFlash v2.0.0 Released!</title>
		<link>http://www.clickpopmedia.com/2008/04/21/box2dflash-v200-released/</link>
		<comments>http://www.clickpopmedia.com/2008/04/21/box2dflash-v200-released/#comments</comments>
		<pubDate>Mon, 21 Apr 2008 18:30:33 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Paul]]></category>
		<category><![CDATA[Box2D]]></category>
		<category><![CDATA[Box2DFlash]]></category>
		<category><![CDATA[Box2DFlashAS3]]></category>

		<guid isPermaLink="false">http://www.clickpopmedia.com/?p=264</guid>
		<description><![CDATA[Box2DFlash v2.0.0 was finally released on the 17th of this month (April).  Box2DFlash is the Flash AS3 port of Erin Catto&#8217;s C++ physics engine Box2D.  Erin Catto is a physics programmer at Blizzard Entertainment (WarCraaaft!).  I&#8217;ve been looking forward to this release since I found out about it while making a tutorial [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://box2dflash.sourceforge.net/">Box2DFlash v2.0.0</a> was finally released on the 17th of this month (April).  Box2DFlash is the Flash AS3 port of Erin Catto&#8217;s C++ physics engine <a href="http://www.box2d.org/">Box2D</a>.  <a href="http://www.gphysics.com/">Erin Catto</a> is a physics programmer at Blizzard Entertainment (WarCraaaft!).  I&#8217;ve been looking forward to this release since I found out about it while making a <a href="http://www.clickpopmedia.com/2008/03/17/physics-in-actionscript-3/">tutorial</a> for the previous release (v1.4.3) about a month ago.<br />
<span id="more-264"></span><br />
Some of the major changes/additions to this version are:</p>
<p>- <strong>Continuous physics</strong>.<br />
Continuous physics means that the physics is now independent of the frame rate.  This prevents bodies from passing through each other at higher speeds.  This feature can potentially make this version slower then previous versions, but it also makes it MUCH more accurate.<br />
- <strong>Shape create/destroy</strong>.</p>
<blockquote><p>You can destroy a shape on the parent body easily. You may do this to model a breakable object.</p></blockquote>
<p>In the past you couldn&#8217;t add or remove shapes from a body object that was already in the world object.<br />
- <strong>Joints defined in local coordinates</strong>.</p>
<blockquote><p>Box2D requires [joint anchor] points to be specified in local coordinates. This way the joint can be specified even when the current body transforms violate the joint constraint &#8212; a common ocurrence when a game is saved and reloaded.</p></blockquote>
<p>- <strong>Moveable center of mass</strong>.<br />
This is great because you can now have a hammer that&#8217;s center of mass is in the head, not the middle of the handle.<br />
- <strong>Sensors</strong>.</p>
<blockquote><p>Some times game logic needs to know when two shapes overlap yet there should be no collision response. This is done by using sensors. A sensor is a shape that detects collision but does not produce a response&#8230;</p></blockquote>
<p>- <strong>Contact listener callback</strong>.<br />
- <strong>Contact filter callback</strong>.<br />
These callbacks are essentially events.  It is now much easier to <em>do</em> something or other on collisions.  Say, cause damage to your spaceship when a meteor strikes it.<br />
- <strong>Revised manual</strong>.<br />
Sure.  Having an up to date manual is a good thing.  Currently the manual is only for the C++ version, but all the concepts are the same for the flash version and it will help a lot for understanding the example projects.<br />
- <strong>Doxygen documents</strong>.<br />
- <strong>Eliminated deferred destruction</strong>.<br />
This was a feature in previous versions of Box2D that was only needed because there was no contact callbacks.  It&#8217;s gone now so I&#8217;m not going to bother explaining what it was.<br />
- <strong>Improved testbed with more examples</strong>.<br />
<a href="http://box2dflash.sourceforge.net/">Check it out!</a> It&#8217;s really just the example project/code that they give you to learn some of the features.</p>
<p>With this release I will be getting back to making some tutorials on how to use this awesome resource.    So, keep checking back to see what&#8217;s new and I will hopefully not disappoint you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.clickpopmedia.com/2008/04/21/box2dflash-v200-released/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Cool Games Made with Box2DFlash</title>
		<link>http://www.clickpopmedia.com/2008/03/24/cool-games-made-with-box2dflash/</link>
		<comments>http://www.clickpopmedia.com/2008/03/24/cool-games-made-with-box2dflash/#comments</comments>
		<pubDate>Mon, 24 Mar 2008 05:31:11 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[Just for Fun!]]></category>
		<category><![CDATA[Paul]]></category>
		<category><![CDATA[Box2D]]></category>
		<category><![CDATA[Box2DFlash]]></category>
		<category><![CDATA[Games]]></category>

		<guid isPermaLink="false">http://www.clickpopmedia.com/2008/03/24/cool-games-made-with-box2dflash/</guid>
		<description><![CDATA[After writing my introduction tutorial to Erin Catto&#8217;s Box2D AS3 physics engine port, I found out that I was writing about version 1.4.3. Well a new version (v 2.0.0) is set to come out within the next few weeks, so I thought that I would put off any more tutorials on Box2DFlash till I find [...]]]></description>
			<content:encoded><![CDATA[<p>After writing my <a href="http://www.clickpopmedia.com/2008/03/17/physics-in-actionscript-3/">introduction tutorial</a> to Erin Catto&#8217;s <a href="http://www.box2d.org/">Box2D</a> AS3 physics engine port, I found out that I was writing about version 1.4.3. Well a new version (v 2.0.0) is set to come out within the next few weeks, so I thought that I would put off any more tutorials on Box2DFlash till I find out what has changed.</p>
<p>Until then, I&#8217;ve found a bunch of fun games made with the Box2D engine and I thought I would share them with you.</p>
<p><span id="more-174"></span></p>
<p><a href="http://brainclog.com/clog.shtml">Clog FLASH</a>: (http://brainclog.com/clog.shtml)<br />
Clog is a fun little flash game where you are some kind of white ice ball bumping and pushing 5 blue ice balls through an obstacle course.  I haven&#8217;t played very far in this game yet.  I keep getting distracted with other things around the fourth level.  Still it&#8217;s a good example of circular collisions in the engine.</p>
<p><a href="http://www.pokerblocks.com/index.html">Poker-Blocks C++</a>: (http://www.pokerblocks.com/index.html)</p>
<pre><object height="355" width="425"><param name="movie" value="http://www.youtube.com/v/dlmY_TloWHA&amp;hl=en"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/dlmY_TloWHA&amp;hl=en" type="application/x-shockwave-flash" wmode="transparent" height="355" width="425"></embed></object></pre>
<p>Poker Blocks is a commercial stand alone game that seems to be a mix between Tetris and poker.  It seems like a weird combo to me, but it also looks fun judging from the video.  I haven&#8217;t played this one personally even though there is a 30 day free trial.  I just wanted to include it because it looks like one of the better games made with the C++ version of Box2D.</p>
<p><a href="http://www.skt-products.com/contents/hikkoshi.html">Hikkoshi FLASH</a>: (http://www.skt-products.com/contents/hikkoshi.html)<a href="http://www.clickpopmedia.com/wp-content/uploads/2008/03/hikkoshi.jpg" title="Hikkoshi ScreenShot"><img src="http://www.clickpopmedia.com/wp-content/uploads/2008/03/hikkoshi.jpg" alt="Hikkoshi ScreenShot" /></a></p>
<p>I found this one to be very fun.  There really isn&#8217;t much to it.  Just click once to start the shot and then click again to let go, but I played it over and over again.  My best score is something around 12800+ (I only remember the first 3 digits).  If you have Flash Player 9 I recommend giving this one a try&#8230;  it only takes about 2 minutes to play from start to finish.<br />
<a href="http://piwna.pl/gry/pinball"></a></p>
<p><a href="http://piwna.pl/gry/pinball">Carona Pinnball FLASH</a>: (http://piwna.pl/gry/pinball)<br />
This is just a pinball game.  In some foreign language.  It&#8217;s a little slow too.  The reason I&#8217;m including it with the others is&#8230;  well it does use the engine.  And, it made the author money.  So, I find that inspirational.</p>
<p><a href="http://www.uberdruck.com/snowball/">Snowball FLASH</a>: (http://www.uberdruck.com/snowball/)<br />
This game was purchased by a radio station in England for their website.   The version I am linking to is the original, before it was fixed up to be suitable for the other site.  This is a really simple game actually. You are just throwing snowballs at (the prime minister?) a ragdoll and watching the physics make it fly about.</p>
<p>Hopefully I will have something cool of my own to show off by next week.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.clickpopmedia.com/2008/03/24/cool-games-made-with-box2dflash/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Physics in ActionScript 3: Box2DFlashAS3</title>
		<link>http://www.clickpopmedia.com/2008/03/17/physics-in-actionscript-3/</link>
		<comments>http://www.clickpopmedia.com/2008/03/17/physics-in-actionscript-3/#comments</comments>
		<pubDate>Mon, 17 Mar 2008 21:57:51 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Paul]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Box2DFlash]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Intro]]></category>
		<category><![CDATA[Physics]]></category>

		<guid isPermaLink="false">http://www.clickpopmedia.com/2008/03/17/physics-in-actionscript-3/</guid>
		<description><![CDATA[I wanted to try something fun with Flash this week, so I did a quick search for &#8220;Flash Physics Engine.&#8221; Lo and behold, I struck gold. Box2DFlashAS3 is an open source ActionScript 3.0 conversion of the C++ Physics Engine Box2D.  I&#8217;m very impressed with its well coded structure and easily implemented nature.  I [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to try something fun with Flash this week, so I did a quick search for &#8220;Flash Physics Engine.&#8221; Lo and behold, I struck gold. <a href="http://box2dflash.sourceforge.net/">Box2DFlashAS3</a> is an open source ActionScript 3.0 conversion of the C++ Physics Engine <a href="http://www.box2d.org">Box2D</a>.  I&#8217;m very impressed with its well coded structure and easily implemented nature.  I learned a few new things that will change how I code forever just by reading through their example files.<br />
<center><embed src="http://www.clickpopmedia.com/wp-content/uploads/2008/03/phystest.swf" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" play="true" loop="true" menu="true" height="320" width="640"></embed></center><br />
Still, they have limited resources right now as far as help documentation goes (&#8221;Please refer to the source code from the examples provided to get an idea of how to use Box2DFlash in your projects.&#8221; &#8212; that&#8217;s the ReadMe.txt that comes with it&#8230; not very helpful,huh).  I thought I might give you an example of my own and walk through it step by step.<br />
<span id="more-115"></span><br />
That was the thought, but it has taken me all weekend to understand the engine well enough to explain how it works.  The beauty of it though, is you don&#8217;t have to understand how it works to use it.Now to get right into it then! First of all, in order to do anything with Box2DFlash you&#8217;re going to have to include most (if not all) of the files.  If you have the 349KB folder &#8220;Box2D&#8221; in your project folder, then your includes will work just like this:</p>
<div class="dean_ch" style="white-space: wrap;"> <span class="co1">// Classes used in this example</span><br />
&nbsp;<span class="kw3">import</span> Box2D.<span class="me1">Dynamics</span>.<span class="me1">*</span>;<br />
&nbsp;<span class="kw3">import</span> Box2D.<span class="me1">Collision</span>.<span class="me1">*</span>;<br />
&nbsp;<span class="kw3">import</span> Box2D.<span class="me1">Collision</span>.<span class="me1">Shapes</span>.<span class="me1">*</span>;<br />
&nbsp;<span class="kw3">import</span> Box2D.<span class="me1">Dynamics</span>.<span class="me1">Joints</span>.<span class="me1">*</span>;<br />
&nbsp;<span class="kw3">import</span> Box2D.<span class="me1">Dynamics</span>.<span class="me1">Contacts</span>.<span class="me1">*</span>;<br />
&nbsp;<span class="kw3">import</span> Box2D.<span class="me1">Common</span>.<span class="me1">*</span>;<br />
&nbsp;<span class="kw3">import</span> Box2D.<span class="me1">Common</span>.<span class="kw3">Math</span>.<span class="me1">*</span>;</div>
<p>Easy.</p>
<p>Now you have to create a b2World object (source code of the class is in Box2D/Dynamics/b2World.as).  The world object is the entire body of the engine.  Everything is contained within it once your are done.</p>
<p><em>NOTE: The brain of the engine is b2BroadPhase.as and the heart is the Step() function withing the world object.  Don&#8217;t go messing with the brain (b2BroadPhase.as) ever.  You will totally mess up the engine.<br />
</em><br />
The world object constructor requires 3 things:<br />
<strong>1.)</strong> A coordinate system in the form of a b2AABB class object.<br />
<strong>2.)</strong> A vector that defines gravity.  That will be in the form of a b2Vec2 class object.<br />
<strong>3.)</strong> A boolean variable that defines whether objects &#8220;sleep&#8221; or not. (I recommend you make it <strong>true</strong>, that they can sleep)</p>
<div class="dean_ch" style="white-space: wrap;"><span class="co1">// Create world AABB</span><br />
<span class="kw2">var</span> worldAABB:b2AABB = <span class="kw2">new</span> b2AABB<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
worldAABB.<span class="me1">minVertex</span>.<span class="kw3">Set</span><span class="br0">&#40;</span><span class="nu0">-1000.0</span>, <span class="nu0">-1000.0</span><span class="br0">&#41;</span>;<br />
worldAABB.<span class="me1">maxVertex</span>.<span class="kw3">Set</span><span class="br0">&#40;</span><span class="nu0">1000.0</span>, <span class="nu0">1000.0</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">// Define the gravity vector</span><br />
<span class="kw2">var</span> gravity:b2Vec2 = <span class="kw2">new</span> b2Vec2<span class="br0">&#40;</span><span class="nu0">0.0</span>, <span class="nu0">300.0</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">// Allow bodies to sleep</span><br />
<span class="kw2">var</span> doSleep:<span class="kw3">Boolean</span> = <span class="kw2">true</span>;</p>
<p><span class="co1">// Construct a world object</span><br />
m_world = <span class="kw2">new</span> b2World<span class="br0">&#40;</span>worldAABB, gravity, doSleep<span class="br0">&#41;</span>;</div>
<p>My examples are not my own here.  The code I&#8217;m showing is an excerpt from the &#8220;Hello World&#8221; code they provided with the engine.</p>
<p>After creating a world object you have to bring it to life by setting its heart to beating:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="co1">// Add event for main loop</span><br />
addEventListener<span class="br0">&#40;</span>Event.<span class="me1">ENTER_FRAME</span>, Update, <span class="kw2">false</span>, <span class="nu0">0</span>, <span class="kw2">true</span><span class="br0">&#41;</span>;</p>
<p><span class="kw3">public</span> <span class="kw2">function</span> Update<span class="br0">&#40;</span><span class="kw3">e</span>:Event<span class="br0">&#41;</span>:<span class="kw3">void</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; m_world.<span class="me1">Step</span><span class="br0">&#40;</span>m_timeStep, m_iterations<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>Running the Step() function every frame will update all the Body Definitions you add to your world.<br />
As of right now this world is empty though.  In order to add balls and boxes and any strange polygonal shapes you can think of, we need to create Body Definitions for them.</p>
<p>A Body Definition consists of 2, 3, or 4 things.<br />
<strong> 1.)</strong> A Shape Definition.<br />
<strong> 2.) </strong>An (x,y) position.<br />
<em>optional:</em><br />
<strong> 3.) Rotation (in radians)<br />
4.) </strong>A pre-made Sprite object.</p>
<p>In the example flash movie at the beginning of this post you will notice that all the shapes are simple.  That&#8217;s because everything is being redrawn every frame with only lines and no fill.  That&#8217;s right,  EVERYTHING is made in code.  Nothing was drawn by hand.<br />
If you want your game, or whatever, to have a little more character then that example movie, then you will probably want to associate hand made Sprites with your Body Definitions.</p>
<p>In the &#8220;Hello World&#8221; example, they use Sprites to display their objects.  You could leave them invisible too if you really wanted. Either way they will still be accounted for in the calculations.</p>
<p>On to something very important.  What is a Shape Definition!?  We have 3 types of shape definition and they all extend the base b2ShapeDef class.</p>
<p>First we have the b2BoxDef class.  The b2BoxDef has 4 important properties:<br />
<strong> 1.) </strong>Extents &#8211; this is a vector that essentially goes from one corner of the box to the exact center.  In other words, half the width and hight. (box is a rectangle)<br />
<strong> 2.)</strong> Density &#8211; in the collision equations we use density * area = mass<br />
A density of 0 (zero) or null will make the object static and it will never move in the case of a collision or gravity.<br />
<strong> 3.) </strong>Friction &#8211; this is used to calculate the friction between 2 objects&#8230;  you should keep it between 0.0 and 1.0<br />
<strong> 4.) </strong>Restitution &#8211; this is the bounciness of the object.  Should probably also stay between 0.0 and 1.0</p>
<p>The b2CircleDef has only one differences in it&#8217;s properties.  Instead of Extents it has Radius, which is easy to remember.<br />
The b2PolyDef has an array of (max 8) vertices instead of Extents or Radius.  These vertices are just b2Vec2 vector objects.</p>
<p>Now Adding a bunch of objects to our world should be easy:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">var</span> bodyDef:b2BodyDef;<br />
<span class="kw2">var</span> boxDef:b2BoxDef;<br />
<span class="kw2">var</span> circleDef:b2CircleDef;</p>
<p><span class="co1">// Add ground body</span><br />
bodyDef = <span class="kw2">new</span> b2BodyDef<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
boxDef = <span class="kw2">new</span> b2BoxDef<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
boxDef.<span class="me1">extents</span>.<span class="kw3">Set</span><span class="br0">&#40;</span><span class="nu0">1000</span>, <span class="nu0">100</span><span class="br0">&#41;</span>;<br />
boxDef.<span class="me1">friction</span> = <span class="nu0">0.3</span>;<br />
<span class="coMULTI">/*Notice that the ground object has no density like the later<br />
definitions. &nbsp;That&#8217;s because it is static and we don&#8217;t want it<br />
effected by any forces.*/</span><br />
bodyDef.<span class="kw3">position</span>.<span class="kw3">Set</span><span class="br0">&#40;</span><span class="nu0">320</span>, <span class="nu0">400</span><span class="br0">&#41;</span>;<br />
bodyDef.<span class="me1">AddShape</span><span class="br0">&#40;</span>boxDef<span class="br0">&#41;</span>;</p>
<p><span class="co1">// Add sprite to body userData</span><br />
<span class="coMULTI">/*We have a Sprite object in the library called PhysGround. &nbsp;Here<br />
we are associating that with our body definition.*/</span><br />
bodyDef.<span class="me1">userData</span> = <span class="kw2">new</span> PhysGround<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
bodyDef.<span class="me1">userData</span>.<span class="kw3">width</span> = boxDef.<span class="me1">extents</span>.<span class="me1">x</span> * <span class="nu0">2</span>;<br />
bodyDef.<span class="me1">userData</span>.<span class="kw3">height</span> = boxDef.<span class="me1">extents</span>.<span class="me1">y</span> * <span class="nu0">2</span>;<br />
addChild<span class="br0">&#40;</span>bodyDef.<span class="me1">userData</span><span class="br0">&#41;</span>;<br />
m_world.<span class="me1">CreateBody</span><span class="br0">&#40;</span>bodyDef<span class="br0">&#41;</span>;</p>
<p><span class="co1">// Add some objects</span><br />
<span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> i:<span class="kw3">int</span> = <span class="nu0">1</span>; i &lt; <span class="nu0">20</span>; i++<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/*We are going to re-use the same bodyDef from before.<br />
&nbsp; &nbsp; &nbsp; &nbsp; It doesn&#8217;t matter now, because it&#8217;s already been copied and stored<br />
&nbsp; &nbsp; &nbsp; &nbsp; in our world object.*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; bodyDef = <span class="kw2">new</span> b2BodyDef<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Box</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw3">Math</span>.<span class="kw3">random</span><span class="br0">&#40;</span><span class="br0">&#41;</span> &lt; <span class="nu0">0.5</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boxDef = <span class="kw2">new</span> b2BoxDef<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boxDef.<span class="me1">extents</span>.<span class="kw3">Set</span><span class="br0">&#40;</span><span class="kw3">Math</span>.<span class="kw3">random</span><span class="br0">&#40;</span><span class="br0">&#41;</span> * <span class="nu0">15</span> + <span class="nu0">10</span>, <span class="kw3">Math</span>.<span class="kw3">random</span><span class="br0">&#40;</span><span class="br0">&#41;</span> * <span class="nu0">15</span> + <span class="nu0">10</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boxDef.<span class="me1">density</span> = <span class="nu0">1.0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boxDef.<span class="me1">friction</span> = <span class="nu0">0.5</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boxDef.<span class="me1">restitution</span> = <span class="nu0">0.2</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bodyDef.<span class="me1">AddShape</span><span class="br0">&#40;</span>boxDef<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/*We have a Sprite object in the library called PhysBox.*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bodyDef.<span class="me1">userData</span> = <span class="kw2">new</span> PhysBox<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bodyDef.<span class="me1">userData</span>.<span class="kw3">width</span> = boxDef.<span class="me1">extents</span>.<span class="me1">x</span> * <span class="nu0">2</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bodyDef.<span class="me1">userData</span>.<span class="kw3">height</span> = boxDef.<span class="me1">extents</span>.<span class="me1">y</span> * <span class="nu0">2</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Circle</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circleDef = <span class="kw2">new</span> b2CircleDef<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circleDef.<span class="me1">radius</span> = <span class="kw3">Math</span>.<span class="kw3">random</span><span class="br0">&#40;</span><span class="br0">&#41;</span> * <span class="nu0">15</span> + <span class="nu0">10</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circleDef.<span class="me1">density</span> = <span class="nu0">1.0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circleDef.<span class="me1">friction</span> = <span class="nu0">0.5</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circleDef.<span class="me1">restitution</span> = <span class="nu0">0.2</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bodyDef.<span class="me1">AddShape</span><span class="br0">&#40;</span>circleDef<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/*We have a Sprite object in the library called PhysCircle.*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bodyDef.<span class="me1">userData</span> = <span class="kw2">new</span> PhysCircle<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bodyDef.<span class="me1">userData</span>.<span class="kw3">width</span> = circleDef.<span class="me1">radius</span> * <span class="nu0">2</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bodyDef.<span class="me1">userData</span>.<span class="kw3">height</span> = circleDef.<span class="me1">radius</span> * <span class="nu0">2</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; bodyDef.<span class="kw3">position</span>.<span class="me1">x</span> = <span class="kw3">Math</span>.<span class="kw3">random</span><span class="br0">&#40;</span><span class="br0">&#41;</span> * <span class="nu0">400</span> + <span class="nu0">120</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; bodyDef.<span class="kw3">position</span>.<span class="me1">y</span> = <span class="kw3">Math</span>.<span class="kw3">random</span><span class="br0">&#40;</span><span class="br0">&#41;</span> * <span class="nu0">100</span> + <span class="nu0">50</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; m_world.<span class="me1">CreateBody</span><span class="br0">&#40;</span>bodyDef<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; addChild<span class="br0">&#40;</span>bodyDef.<span class="me1">userData</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>That&#8217;s great isn&#8217;t it?  It&#8217;s really not that hard to add a whole bunch of objects to your world. You should know that the Step() function that we added earlier will only take care of moving and rotating our body definitions.  If we have sprites to represent those definitions then we need to update them manually&#8230;  they made the code for that easy and you can copy and paste it almost exactly into every one of your projects.<br />
We just have to rewrite our Update function from before:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw3">public</span> <span class="kw2">function</span> Update<span class="br0">&#40;</span><span class="kw3">e</span>:Event<span class="br0">&#41;</span>:<span class="kw3">void</span><span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; m_world.<span class="me1">Step</span><span class="br0">&#40;</span>m_timeStep, m_iterations<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Go through body list and update sprite positions/rotations</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> bb:b2Body = m_world.<span class="me1">m_bodyList</span>; bb; bb = bb.<span class="me1">m_next</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>bb.<span class="me1">m_userData</span> is Sprite<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bb.<span class="me1">m_userData</span>.<span class="me1">x</span> = bb.<span class="me1">m_position</span>.<span class="me1">x</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bb.<span class="me1">m_userData</span>.<span class="me1">y</span> = bb.<span class="me1">m_position</span>.<span class="me1">y</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bb.<span class="me1">m_userData</span>.<span class="me1">rotation</span> = bb.<span class="me1">m_rotation</span> * <span class="br0">&#40;</span><span class="nu0">180</span>/<span class="kw3">Math</span>.<span class="kw3">PI</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="coMULTI">/*This is an extreemly clever way to do this, and I suggest you copy it exactly in your own projects.*/</span></p>
<p><span class="br0">&#125;</span></div>
<p>That&#8217;s it for now.  We have objects in a world that can collide with each other and are affected by gravity.  There is still a lot more to talk about, but I am just walking you through the &#8220;Hello World&#8221; first.  We still need to cover Joints, Pulleys, Gears, Compound Shapes, Forced rotation (like cars tires), and a lot more&#8230;  I doubt I will be able to cover all these topics anytime soon, but I will definitely write more tutorials on this physics engine in the future.</p>
<p>You can download my copy of the &#8220;Hello World&#8221; example project with all of my comments included (there is almost no commenting in the original).</p>
<p><strong>Download</strong> <a href="http://www.clickpopmedia.com/wp-content/uploads/2008/03/hello-world.zip" title="Hello World with comments">Hello World w/ comments</a>.</p>
<p>You can get all of the library files needed for <a href="http://box2dflash.sourceforge.net/">Box2DFlash</a> at their site.  It&#8217;s free.</p>
<p>Credits:<br />
Box2D &#8211; Erin Catto ( <a href="http://gphysics.com/">http://gphysics.com/</a> )<br />
Box2DFlashAS3 &#8211; Matthew Bush (Flash ActionScript 3.0 port of Erin Catto&#8217;s work)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.clickpopmedia.com/2008/03/17/physics-in-actionscript-3/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
	</channel>
</rss>

