<?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; Physics</title>
	<atom:link href="http://www.clickpopmedia.com/tag/physics/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>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>

