<?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>The Stem &#187; ActionScript 3</title>
	<atom:link href="http://blog.thestem.ca/archives/category/actionscript-3/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.thestem.ca</link>
	<description>Brett Forsyth's professional work showcase, photos, and ramblings.</description>
	<lastBuildDate>Tue, 22 Feb 2011 18:00:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Tentacle(s)</title>
		<link>http://blog.thestem.ca/archives/117</link>
		<comments>http://blog.thestem.ca/archives/117#comments</comments>
		<pubDate>Tue, 22 Feb 2011 18:00:47 +0000</pubDate>
		<dc:creator>Brettf</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://blog.thestem.ca/?p=117</guid>
		<description><![CDATA[To force myself to finish my tentacle experiment I am going to post what I have achieved so far. I have to give credit where credit is due though. I didn&#8217;t really do much of the hard work Grant Skinner, Keith Peters and Jelger Muylaert are to thank for that. I still want to add [...]]]></description>
			<content:encoded><![CDATA[<p>To force myself to finish my tentacle experiment I am going to post what I have achieved so far. I have to give credit where credit is due though. I didn&#8217;t really do much of the hard work <a href="http://gskinner.com/blog">Grant Skinner</a>, <a href="http://www.bit-101.com">Keith Peters</a> and <a href="http://labs.review.one-agency.be/labs/curvetest/">Jelger Muylaert</a> are to thank for that. I still want to add joint limits and elastics. Source code to come.</p>
<div id="tentacle">If you are seeing this you need to enable javascript.</div>
<p><script type="text/javascript">/*<!--*/var so = new SWFObject("http://blog.thestem.ca/wp-content/flash/tentaclev1.swf", "tentacle", "400", "400", "10", "#ffffff"); so.write("tentacle");/*-->*/</script></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thestem.ca/archives/117/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Setting Up Popforge for Flex Newbies</title>
		<link>http://blog.thestem.ca/archives/106</link>
		<comments>http://blog.thestem.ca/archives/106#comments</comments>
		<pubDate>Mon, 06 Oct 2008 18:00:27 +0000</pubDate>
		<dc:creator>Brettf</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Source Code]]></category>

		<guid isPermaLink="false">http://blog.thestem.ca/?p=106</guid>
		<description><![CDATA[I have to admit that I have been shying away from Flex for a long time now. I think this has to do with a general hatred for making forms based applications. Now before people tell me Flex isn&#8217;t just about forms based applications, I know, I know. Flex is actually pretty cool but in [...]]]></description>
			<content:encoded><![CDATA[<p>I have to admit that I have been shying away from <a href="http://www.adobe.com/products/flex/?promoid=BPDEQ">Flex</a> for a long time now. I think this has to do with a general hatred for making forms based applications. Now before people tell me Flex isn&#8217;t just about forms based applications, I know, I know. Flex is actually pretty cool but in general the types of work I see people using Flex for I don&#8217;t want a part of. I also know that I have been missing out on a better development IDE in Flex or <a href="http://fdt.powerflasher.com/">FDT</a> or <a href="http://www.flashdevelop.org/">FlashDevelop</a> for that matter. And in the case of <a href="http://code.google.com/p/popforge/">Popforge</a>, a really cool set of libraries by <a href="http://blog.andre-michelle.com/">Andre Michelle</a> and <a href="http://blog.joa-ebert.com/">Joa Ebert</a>, Flex is a requirement for use. </p>
<p><strong>Easy Popforge Setup</strong><br />
I will start by showing you what turns out to be an easy way to get started with the popforge library.<br />
Step 1. Get a SVN Client. On the mac I suggest you check out <a href="http://versionsapp.com/">Versions</a> and on the PC there is the old faithful <a href="http://tortoisesvn.tigris.org/">Tortoise</a>.<br />
Step 2. Check out the <a href="http://code.google.com/p/popforge/source/checkout">popforge source</a>.<br />
Step 3. Create a new AS Project in Flex (download <a href="http://www.adobe.com/products/flex/?promoid=BPDEQ">here</a> if you don&#8217;t have it yet). Click Next after entering a project name. In the source path window select add folder. Navigate to &#8220;pathToPopforge/flash/PopforgeLibrary/src&#8221; and click ok.<br />
Step 4. Click finish.<br />
Step 4. Rejoice because you are done!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thestem.ca/archives/106/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Multi-touch Crop</title>
		<link>http://blog.thestem.ca/archives/109</link>
		<comments>http://blog.thestem.ca/archives/109#comments</comments>
		<pubDate>Fri, 26 Sep 2008 20:09:59 +0000</pubDate>
		<dc:creator>Brettf</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Multi-touch]]></category>

		<guid isPermaLink="false">http://blog.thestem.ca/?p=109</guid>
		<description><![CDATA[I have recently changed the final student project in my OOP class to a multi-touch app. One of the briefs I gave to students was to do a non-destructive crop on the multi-touch. Two students, Jae-Ho Yoo and Morgan Roddick made a short video of it in action and I thought I would share. Check [...]]]></description>
			<content:encoded><![CDATA[<p>I have recently changed the final student project in my OOP class to a multi-touch app. One of the briefs I gave to students was to do a non-destructive crop on the multi-touch. Two students, <a href="http://dd.ourvfs.com/dd11jaeho/wordpress/">Jae-Ho Yoo</a> and <a href="http://tismadness.com/">Morgan Roddick</a> made a short video of it in action and I thought I would share. Check it out and give them some feedback, I am sure they would appreciate it. <object width="400" height="225"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=1614427&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=1614427&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"></embed></object><br /><a href="http://vimeo.com/1614427?pg=embed&amp;sec=1614427">A demo of a multi-touch photo app</a> from <a href="http://vimeo.com/user394292?pg=embed&amp;sec=1614427">Jae-Ho Yoo</a> on <a href="http://vimeo.com?pg=embed&amp;sec=1614427">Vimeo</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thestem.ca/archives/109/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>KitchenSync, I Can Stop Using Tweener Now!</title>
		<link>http://blog.thestem.ca/archives/108</link>
		<comments>http://blog.thestem.ca/archives/108#comments</comments>
		<pubDate>Fri, 08 Aug 2008 18:30:19 +0000</pubDate>
		<dc:creator>Brettf</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://blog.thestem.ca/?p=108</guid>
		<description><![CDATA[I was alerted to KitchenSync, a AS3 tweening library by Mims Wright, on the Adobe blog feed a week or so ago. I was a bit skeptical at first of another tweening library for Flash but I have been searching for a true AS3 replacement to FUSE (I never started working with GOASAP but should [...]]]></description>
			<content:encoded><![CDATA[<p>I was alerted to <a href="http://code.google.com/p/kitchensynclib/">KitchenSync</a>, a AS3 tweening library by <a href="http://dispatchevent.org/">Mims Wright</a>, on the Adobe blog feed a week or so ago. I was a bit skeptical at first of another tweening library for Flash but I have been searching for a true AS3 replacement to <a href="http://www.mosessupposes.com/Fuse/">FUSE</a> (I never started working with <a href="http://www.goasap.org/">GOASAP</a> but should probably check it out to). <a href="http://code.google.com/p/tweener/">Tweener</a> was ok but as a teacher I prefer libraries with good documentation as it makes a students life easier, which really makes my life easier. Anyway, I had to teach an AS animation class yesterday, which I normally do with Tweener, but decided to give KS 30 minutes to blow my mind. Within 5 minutes I knew I was going to be teaching KS. Good documentation, <a href="http://code.google.com/p/kitchensynclib/wiki/ActionGroups">animation sequencing/paralleling</a>, filter tweening, a <a href="http://code.google.com/p/kitchensynclib/wiki/Roadmap">dev roadmap</a> and fast response to questions by Mims all told me KS was worth investing some time in and worth teaching to students. I hope to contribute some documentation over time and maybe a student project or two as a showcase. You should at least give it 5 minutes yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thestem.ca/archives/108/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Box2D Joints #2 &#8211; Revolute Joints</title>
		<link>http://blog.thestem.ca/archives/102</link>
		<comments>http://blog.thestem.ca/archives/102#comments</comments>
		<pubDate>Mon, 14 Jul 2008 17:00:53 +0000</pubDate>
		<dc:creator>Brettf</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Source Code]]></category>

		<guid isPermaLink="false">http://blog.thestem.ca/?p=102</guid>
		<description><![CDATA[Revolute Joints Revolute joints can be used to create rotating joints (aka motors) or joints with friction. Both types can have angular limits placed on them. Take a look at the TestRagdoll, TestCrankGearsPulley and TestTheoJansen for more examples of how to use this joint type. Creating Motors The motor features of a revolute joint provide [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Revolute Joints</strong><br />
Revolute joints can be used to create rotating joints (aka motors) or joints with friction. Both types can have angular limits placed on them. Take a look at the TestRagdoll, TestCrankGearsPulley and TestTheoJansen for more examples of how to use this joint type.</p>
<p><strong>Creating Motors</strong><br />
The motor features of a revolute joint provide a lot of functionality. Just look at the Crank/Gear/Pulley and Theo Jansen Walker test bed examples for a hint of their potential. When creating a motor the maxMotorTorque is the most import property. Measured in newton meters (N•m), when using the normal units (not converted to pixels as in my example), it determines the amount of mass the motor can rotate. If your motors is not rotating or rotating poorly you likely need to increase its maxMotorTorque. Simple Motor Example:</p>
<pre class="brush: as3">//Create and setup a revolute joint as a simple motor
pivot.position.Set(80,250);
var pivotBody1:b2Body = m_world.CreateBody(pivot);
pivotBody1.CreateShape(pivotPoly)
pivotBody1.SetMassFromShapes();

var motorPoly:b2PolygonDef = new b2PolygonDef();
motorPoly.SetAsOrientedBox(30, 10, new b2Vec2(0, 0), 0);
motorPoly.friction = .3;
motorPoly.density = .01;
motorPoly.restitution = .1;

var motorDef:b2BodyDef = new b2BodyDef();
motorDef.position.Set(110, 250);
var motorBody:b2Body = m_world.CreateBody(motorDef);
motorBody.CreateShape(motorPoly);
motorBody.SetMassFromShapes();

//Setup the revolute joint definition for later instantiation. rjd is a predefined b2RevoluteJointDef object.
rjd.Initialize(pivotBody1, motorBody, new b2Vec2(80,250));
rjd.motorSpeed = 3;
rjd.maxMotorTorque = 10000000;
rjd.enableMotor = true;
rjd.enableLimit = false;
//Create the motor joint and cast it as a b2RevoluteJoint
var joint1:b2RevoluteJoint = m_world.CreateJoint(rjd) as b2RevoluteJoint;
</pre>
<p><strong>Limits</strong><br />
Revolute joints are not limited to continuous rotation. You can set upper and lower limits to the joint (in radians). When used with the enableMotor property set to false, this joint is ideal for simulating limbs. See the TestRagdoll example for a great demo of this.</p>
<p>Combining joint limits, motors and conditionals allows for creation of some interesting systems. I could see this used as valves, platforms in games, fins on fish, etc. In this example I have setup a motor to oscillate between its limits.</p>
<pre class="brush: as3">//Setup a limited revolute joint that oscillates between its limits
pivot.position.Set(80,100);
var pivotBody2:b2Body = m_world.CreateBody(pivot);
pivotBody2.CreateShape(pivotPoly)
pivotBody2.SetMassFromShapes();

var limitMotorPoly:b2PolygonDef = new b2PolygonDef();
limitMotorPoly.SetAsOrientedBox(30, 10, new b2Vec2(0, 0), 0);
limitMotorPoly.friction = .3;
limitMotorPoly.density = .01;
limitMotorPoly.restitution = .1;

var limitedMotorDef:b2BodyDef = new b2BodyDef();
limitedMotorDef.position.Set(110, 100);
var limitedMotorBody:b2Body = m_world.CreateBody(limitedMotorDef);
limitedMotorBody.CreateShape(limitMotorPoly);
limitedMotorBody.SetMassFromShapes();

//Setup the revolute joint definition for later instantiation
rjd.Initialize(pivotBody2, limitedMotorBody, new b2Vec2(80,100));
rjd.motorSpeed = 3;
rjd.lowerAngle = 0;
//Convert the angle in degrees to radians
rjd.upperAngle = 270*(Math.PI/180);
rjd.enableLimit = true;
rjd.maxMotorTorque = 10000000;
rjd.referenceAngle = 0;
rjd.enableMotor = true;
//Create the motor joint and cast it as a b2RevoluteJoint. Store the joint object in a class var for use later in the update method.
limitMotorJoint = m_world.CreateJoint(rjd) as b2RevoluteJoint;
</pre>
<p><strong>Friction Joints</strong><br />
Friction joints are revolute joints with the motor enabled, a non-zero maxMotorTorque and the motor speed set to 0. This will cause the joint to resist rotation but give when the maxMotorTorque is exceeded, giving the effect of a joint with friction at the pivot. Once the force to the joint is removed it stops moving. Example:</p>
<pre class="brush: as3">//Setup a friction joint
pivot.position.Set(190,100);
var pivotBody3:b2Body = m_world.CreateBody(pivot);
pivotBody3.CreateShape(pivotPoly)
pivotBody3.SetMassFromShapes();

var armPoly:b2PolygonDef = new b2PolygonDef();
armPoly.SetAsOrientedBox(90, 10, new b2Vec2(0, 0), 0);
armPoly.friction = .5;
armPoly.density = .01;
armPoly.restitution=0;

var armBodyDef:b2BodyDef = new b2BodyDef();
armBodyDef.position.Set(280,100);
var armBody = m_world.CreateBody(armBodyDef);
armBody.CreateShape(armPoly)
armBody.SetMassFromShapes();

rjd.Initialize(pivotBody3, armBody, new b2Vec2(190,100));
rjd.motorSpeed = 0;
rjd.maxMotorTorque = 10000000;
rjd.enableMotor = true;
rjd.enableLimit = false;
var joint2:b2RevoluteJoint = m_world.CreateJoint(rjd) as b2RevoluteJoint;
</pre>
<p>Example:</p>
<div id="revolutejoint">If you are seeing this you need to enable javascript. This is a flash example of three different uses of the Box2D revolute joint.</div>
<p><script type="text/javascript">/*<!--*/var so = new SWFObject("http://blog.thestem.ca/wp-content/flash/RevoluteJoints.swf", "revolutejoint", "400", "400", "8", "#ffffff"); so.write("revolutejoint");/*-->*/</script><br />
<a href="/wp-content/source/revolutesource.zip">Full Source not including Box2D</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thestem.ca/archives/102/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Changes in Box2D 2.0.1</title>
		<link>http://blog.thestem.ca/archives/99</link>
		<comments>http://blog.thestem.ca/archives/99#comments</comments>
		<pubDate>Sun, 29 Jun 2008 19:13:17 +0000</pubDate>
		<dc:creator>Brettf</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://blog.thestem.ca/?p=99</guid>
		<description><![CDATA[With the release of the 2.0.1 update of Box2D a couple of things have changed. Here are a couple of changes I have found. If you know of more post a comment. Creating Bodies Previously when creating bodies you had to specify if the body was static or dynamic, this is no longer the case. [...]]]></description>
			<content:encoded><![CDATA[<p>With the release of the <a href="http://sourceforge.net/projects/box2dflash">2.0.1 update</a> of Box2D a couple of things have changed. Here are a couple of changes I have found. If you know of more post a comment.</p>
<p><strong>Creating Bodies</strong><br />
Previously when creating bodies you had to specify if the body was static or dynamic, this is no longer the case. The engine itself will determine the body type based on its density setting. If you set the density of the shapes to 0 the body will be static and any non-zero value will make it dynamic. Example:</p>
<pre class="brush: as3">//Create a static body
var staticPoly:b2PolygonDef = new b2PolygonDef();
staticPoly.SetAsOrientedBox(10, 10, new b2Vec2(0, 0), 0);
staticPoly.friction = .5;
//Setting the density to 0 will make the body that contains this poly static
staticPoly.density = 0;
staticPoly.restitution=0;

var staticBodyDef:b2BodyDef = new b2BodyDef();
staticBodyDef.position.Set(0,0);
var staticBody = m_world.CreateBody(staticBodyDef);
//New shape instantiation does not need to define static or dynamic
staticBody.CreateShape(staticPoly)
staticBody.SetMassFromShapes();

//Create a dynamic body
var dynamicPoly:b2PolygonDef = new b2PolygonDef();
dynamicPoly.SetAsOrientedBox(10, 10, new b2Vec2(0, 0), 0);
dynamicPoly.friction = .5;
//Setting the density to any value greater then 0 will make the body that contains this poly dynamic
dynamicPoly.density = 1;
dynamicPoly.restitution=0;

var dynamicBodyDef:b2BodyDef = new b2BodyDef();
dynamicBodyDef.position.Set(0,0);
var dynamicBody = m_world.CreateBody(dynamicBodyDef);
//New shape instantiation does not need to define static or dynamic
dynamicBody.CreateShape(dynamicPoly)
dynamicBody.SetMassFromShapes();
</pre>
<p><strong>Distance Joints</strong><br />
A couple of minor changes have been made to the distance joint type. It now includes a dampening factor and a frequency. I can&#8217;t really seem to figure out what they do though. I am probably wrong but I don&#8217;t think they are working properly in flash yet.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thestem.ca/archives/99/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Box2D Joints #1 &#8211; Distance Joints</title>
		<link>http://blog.thestem.ca/archives/98</link>
		<comments>http://blog.thestem.ca/archives/98#comments</comments>
		<pubDate>Fri, 20 Jun 2008 17:30:21 +0000</pubDate>
		<dc:creator>Brettf</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Source Code]]></category>

		<guid isPermaLink="false">http://blog.thestem.ca/?p=98</guid>
		<description><![CDATA[Joints are one of the most interesting parts of Box2D. Over the next several weeks I will attempt to explain all of the Box2D joint types and possibly add a new one. Before You Start I have switched over to pixels as my primary unit for working with Box2D. This was done by changing the [...]]]></description>
			<content:encoded><![CDATA[<p>Joints are one of the most interesting parts of Box2D. Over the next several weeks I will attempt to explain all of the Box2D joint types and possibly add a new one. </p>
<p><strong>Before You Start</strong><br />
I have switched over to pixels as my primary unit for working with Box2D. This was done by changing the scale factor and it impacts defining joints because they use N•m as their units. So now  the values used in joints will need to be 30x bigger. Nothing major but if you use the same values as the test bed examples your joint won&#8217;t work properly. This will be more important with other joint types.</p>
<p><strong>Distance Joints</strong><br />
A <a href="http://linuxuser.at/elements/doc/box2d/classb2_distance_joint.htm">distance joint</a> is the simplest of the joints to use. As the name implies it links two bodies together with a rigid connection that maintains the distance between the bodies. Each body is free to rotate and the collideConnected property of the inherited <a href="http://linuxuser.at/elements/doc/box2d/structb2_joint_def.htm">b2JointDef</a> controls whether the bodies collide. The joint itself will not collide with other objects. As with most things in Box2D the vectors that specify the connection points are relative to the world and not the bodies. Example:</p>
<pre class="brush: as3">//Create a new distance joint definition
var djd:b2DistanceJointDef = new b2DistanceJointDef();
//Initialize the joint between two bodies. The b2Vec2() objects are used to define the location of the joint ends. Values are sample only.
djd.Initialize(body1, body2, new b2Vec2(0,0),new b2Vec2(0,30));
//Define whether the two objects will be allowed to collide with each other.
djd.collideConnected = true;
var distJoint:b2DistanceJoint = m_world.CreateJoint(djd);
</pre>
<p>Example:</p>
<div id="distancejoint">Don&#8217;t have flash? <a href="http://www.adobe.com/products/flashplayer/">Get it </a></div>
<p><script type="text/javascript">/*<!--*/var so = new SWFObject("http://blog.thestem.ca/wp-content/flash/distancejoint.swf", "distancejoint", "400", "400", "8", "#ffffff"); so.write("distancejoint");/*-->*/</script></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thestem.ca/archives/98/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Creating Complex Shapes in Box2D UPDATED</title>
		<link>http://blog.thestem.ca/archives/91</link>
		<comments>http://blog.thestem.ca/archives/91#comments</comments>
		<pubDate>Tue, 03 Jun 2008 03:57:38 +0000</pubDate>
		<dc:creator>Brettf</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Source Code]]></category>

		<guid isPermaLink="false">http://blog.thestem.ca/?p=91</guid>
		<description><![CDATA[Creating complex shapes,aka compound shapes, in Box2d is accomplished by adding multiple shapes to a body. In exploring complex shapes I learned a bunch about defining objects in general. Again I couldn&#8217;t find any tutorials on this but I have been using the box2d manual. It helps but isn&#8217;t the best. Before we start When [...]]]></description>
			<content:encoded><![CDATA[<p>Creating complex shapes,aka compound shapes, in <a href="http://box2dflash.sourceforge.net/">Box2d</a> is accomplished by adding multiple shapes to a body. In exploring complex shapes I learned a bunch about defining objects in general. Again I couldn&#8217;t find any tutorials on this but I have been using the <a href="http://www.box2d.org/manual.html">box2d manual</a>. It helps but isn&#8217;t the best.</p>
<p><strong>Before we start</strong><br />
When I started to experiment with box2d I discounted the importance/utility of the debug draw mode. It doesn&#8217;t really do anything other then show you what is in the box2d world, which when your understanding of positioning is still bad helps a ton. So turn it on, it&#8217;s magic. If you change the m_drawScale to 1 you can use pixels as your units. Include the following after you create the b2World. </p>
<pre class="brush: as3">m_sprite = new Sprite();
addChild(m_sprite);
var dbgDraw:b2DebugDraw = new b2DebugDraw();
var dbgSprite:Sprite = new Sprite();
m_sprite.addChild(dbgSprite);
dbgDraw.m_sprite = m_sprite;
dbgDraw.m_drawScale = 1;
dbgDraw.m_fillAlpha = 0.3;
dbgDraw.m_lineThickness = 1.0;
dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit;
m_world.SetDebugDraw(dbgDraw);
</pre>
<p><strong>Complex shapes need a body</strong><br />
Ok onto complex shapes. When creating any object in box2d you start by creating a <a href="http://linuxuser.at/elements/doc/box2d/structb2_body_def.htm">b2BodyDef</a>. The b2BodyDef defines the intial properties of a <a href="http://linuxuser.at/elements/doc/box2d/classb2_body.htm">b2Body</a> object. When you create a b2Body it will either be a static (not moving) or a dynamic (moving) object. It also provides the container for shape definitions and custom visuals . b2Body creation example:</p>
<pre class="brush: as3">//Create a new body definition without any shapes
var bodyDef:b2BodyDef = new b2BodyDef();
//Set its position in the world. Units in meters.
bodyDef.position.Set(150, 150);
//Create a dynamic body in the world from the bodyDef
var body:b2Body = m_world.CreateDynamicBody(bodyDef);
</pre>
<p><strong>Creating shapes and adding them to the body</strong><br />
After creating the body, we need to define and add some shapes to it. There are 2 basic types of shapes, <a href="http://linuxuser.at/elements/doc/box2d/structb2_circle_def.htm">circles (b2CircleDef)</a> and <a href="http://linuxuser.at/elements/doc/box2d/structb2_polygon_def.htm">polygons (b2PolygonDef)</a>. Both of these inherit from the <a href="http://linuxuser.at/elements/doc/box2d/structb2_shape_def.htm">b2ShapeDef class</a> which is where you will find most of the editable shape properties (friction, restitution, density, etc). When creating shapes everything is relative to a centered registration point. b2PolygonDef shapes can be defined either using SetAsBox, for squares, or via a vertex array. In the documentation it says to limit to 8 vertex points for any polygon and it has to be <a href="http://www.box2d.org/manual.html#d0e910">convex</a>. Box2DFlash differs a little from the C++ version when creating square polygons. To position a square poly in the body you use SetAsOrientedBox instead of SetAsBox. SetAsOrientedBox takes two extra parameters for position (b2Vec2) and angle (in radians). If you create a shape using vertex points you don&#8217;t need to use SetAsBox or SetAsOrientedBox because you have defined all the points manually. Shape creation example:</p>
<pre class="brush: as3">//Create a single rectangular poylgon
var poly1:b2PolygonDef = new b2PolygonDef();
//Make a 120px by 50px poly that will be centered on the body centered and without any angle
poly1.SetAsOrientedBox(60, 30, new b2Vec2(0, 0), 0);
//Give it some friction. 0 being none 1 being a lot
poly1.friction = 0.3;
//Give it some density which will be used to calculate mass later
poly1.density = 1;
//Restitution is how elastic something is 0 being in elastic and 1 being totally elastic
poly1.restitution = .1;
</pre>
<p><strong>Putting it all together</strong><br />
Combining the two objects is really easy. Call the CreateShape() method of the b2Body with the shape as an argument. If you are going to add more shapes to the body you can just call CreateShape() again. Example:</p>
<pre class="brush: as3">//Add the poly1 shape to the body
body.CreateShape(poly1);
//If you were going to add more its as simple as
//body.CreateShape(poly2)
//SetMassFromShapes is a great feature of Box2D. It will analyze your body and determine the mass and center of gravity from the shapes.
body.SetMassFromShapes();
</pre>
<p><strong>Complete Example Source</strong></p>
<pre class="brush: as3">/*
Original software provided by Erin Catto http://www.gphysics.com. Thanks Erin for all the hard work.
Also thanks to Matthew Bush for all the hard work in porting box2D to flash
Some of the code in this example is from the Box2D Test bed examples. Check them out they are really helpful.
*/
package {

	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.geom.*;

	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Dynamics.Joints.*;
	import Box2D.Dynamics.Contacts.*;
	import Box2D.Common.*;
	import Box2D.Common.Math.*;

	public class ComplexTest extends Sprite {
		private var m_world:b2World;
		private var m_iterations:int = 10;
		private var m_timeStep:Number = 1/30;
		private var m_sprite:Sprite;
		//Convert to rads from degrees
		private var dtor:Number = Math.PI/180;

		public function ComplexTest():void {
			this.addEventListener(Event.ENTER_FRAME, update, false, 0, true);
			// Creat world AABB
			var worldAABB:b2AABB = new b2AABB();
			worldAABB.lowerBound.Set(-3000, -3000);
			worldAABB.upperBound.Set(3000, 3000);

			// Define the gravity vector
			var gravity:b2Vec2 = new b2Vec2(0, 300);

			// Allow bodies to sleep
			var doSleep:Boolean = true;

			// Construct a world object
			m_world = new b2World(worldAABB, gravity, doSleep);
			m_sprite = new Sprite();
			addChild(m_sprite);
			var dbgDraw:b2DebugDraw = new b2DebugDraw();
			var dbgSprite:Sprite = new Sprite();
			m_sprite.addChild(dbgSprite);
			dbgDraw.m_sprite = m_sprite;
			dbgDraw.m_drawScale = 1;
			dbgDraw.m_fillAlpha = .3;
			dbgDraw.m_lineThickness = 1;
			dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit;
			m_world.SetDebugDraw(dbgDraw);

			// Add ground body
			var ground:b2BodyDef = new b2BodyDef();
			ground.position.Set(0, stage.stageHeight);

			//Define a ground poly
			var groundPoly:b2PolygonDef = new b2PolygonDef();
			groundPoly.SetAsBox(1000, 90);
			groundPoly.friction = .3;

			var groundBody:b2Body = m_world.CreateStaticBody(ground);
			groundBody.CreateShape(groundPoly);
			groundBody.SetMassFromShapes();

			//Create a new body definition without any shapes
			var bodyDef:b2BodyDef = new b2BodyDef();
			//Set its position in the world. Units in meters.
			bodyDef.position.Set(150, 150);
			//Give a bit of an angle to show the object colliding
			bodyDef.angle = 15 * dtor;
			//Create a dynamic body in the world from the bodyDef
			var body:b2Body = m_world.CreateDynamicBody(bodyDef);

			//Create a single rectangular poylgon
			var poly1:b2PolygonDef = new b2PolygonDef();
			//Make a 4m by 2m poly that will be centered on the body centered and without any angle
			poly1.SetAsOrientedBox(60, 30, new b2Vec2(0, 0), 0);
			//Give it some friction. 0 being none 1 being a lot
			poly1.friction = .3;
			//Give it some density which will be used to calculate mass later
			poly1.density = 1;
			//Restitution is how elastic something is 0 being in elastic and 1 being totally elastic
			poly1.restitution = .1;

			//Create a second poly
			var poly2:b2PolygonDef = new b2PolygonDef();
			poly2.SetAsOrientedBox(30, 60, new b2Vec2(30, 0), 0);
			poly2.friction = .3;
			poly2.density = 1;
			poly2.restitution = .1;

			body.CreateShape(poly1);
			body.CreateShape(poly2);
			body.SetMassFromShapes();
		}

		private function update(e:Event):void {
			m_world.Step(m_timeStep, m_iterations);
			// Go through body list and update sprite positions/rotations
			var bb:b2Body = m_world.m_bodyList;
			for (bb; bb; bb = bb.m_next) {

				if (bb.m_userData is Sprite) {
					bb.m_userData.x = bb.GetPosition().x;
					bb.m_userData.y = bb.GetPosition().y;
					bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
				}
			}
		}
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.thestem.ca/archives/91/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Intro to Box2D in Flash, Visual Explination UPDATED</title>
		<link>http://blog.thestem.ca/archives/89</link>
		<comments>http://blog.thestem.ca/archives/89#comments</comments>
		<pubDate>Tue, 27 May 2008 17:28:45 +0000</pubDate>
		<dc:creator>Brettf</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Source Code]]></category>

		<guid isPermaLink="false">http://blog.thestem.ca/?p=89</guid>
		<description><![CDATA[Getting started with Box2D in Flash is harder then it should be. The demos are great and if you spend some time with them and the Box2D forums you can get up and running pretty quick. It&#8217;s a shame there isn&#8217;t more tutorials yet. I hope this intro will help get people started a bit [...]]]></description>
			<content:encoded><![CDATA[<p>Getting started with <a href="http://box2dflash.sourceforge.net/">Box2D</a> in Flash is harder then it should be. The demos are great and if you spend some time with them and the <a href="http://www.box2d.org/forum/">Box2D forums</a> you can get up and running pretty quick. It&#8217;s a shame there isn&#8217;t more tutorials yet. I hope this intro will help get people started a bit quicker. I will post more as I do more with it.</p>
<p><strong>Units</strong><br />
Units in the Box2D examples seem strange at first until you find out the secret. They are use the units from the C++ package which measured things in meters. They are making a meter = 30px in flash. If you just change the m_drawScale of the debug draw to 1 everything will be in pixels</p>
<p><strong>SetAsBox()</strong><br />
Everything in Box2D is setup with a centered registration point. SetAsBox() through me off at first because I was expecting it to take the full width and height of the box as parameters but it actually takes half the width and height. See diagram below.</p>
<p>That&#8217;s it for now. Next up will be setting center of gravity, friction, restitution, etc.<br />
<a href='http://blog.thestem.ca/wp-content/uploads/2008/06/digram.jpg'><img src="http://blog.thestem.ca/wp-content/uploads/2008/06/digram.jpg" alt="" title="Box2D SetAsBox" width="251" height="278" class="alignleft size-medium wp-image-97" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thestem.ca/archives/89/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Regular Expressions In Flash</title>
		<link>http://blog.thestem.ca/archives/88</link>
		<comments>http://blog.thestem.ca/archives/88#comments</comments>
		<pubDate>Wed, 14 May 2008 19:56:53 +0000</pubDate>
		<dc:creator>Brettf</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Source Code]]></category>

		<guid isPermaLink="false">http://blog.thestem.ca/?p=88</guid>
		<description><![CDATA[Flash CS3&#8242;s addition of regular expressions gives you tons of control over your text in flash. For anyone who has experience with regexp this will seem trivial but for those just starting out I hope this example will prove useful. The example is for processing specific data out of an HTML page because there isn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>Flash CS3&#8242;s addition of <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expressions</a> gives you tons of control over your text in flash. For anyone who has experience with regexp this will seem trivial but for those just starting out I hope this example will prove useful. The example is for processing <a href="http://www.weatheroffice.gc.ca/marine/marine_e.html?46146">specific data</a> out of an HTML page because there isn&#8217;t a <acronym title="Representational State Transfer"><a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">REST</a></acronym> service for this data.<br />
First, you pull in the HTML document using <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/URLLoader.html">URLLoader</a>. The data of the URLLoader will be treated as plain text making it perfect to process with a <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/RegExp.html">RegExp</a>.</p>
<pre class="brush: html">//Create a request for the weather page
var myURLRequest:URLRequest = new URLRequest(&quot;http://www.weatheroffice.gc.ca/marine/marine_e.html?46146&quot;);
//Load the weather page into a urlloader object.
var myURLLoader:URLLoader = new URLLoader(myURLRequest);
//Listen for the load complete event for the URLLoader object
myURLLoader.addEventListener(Event.COMPLETE,weatherLoadComplete);</pre>
<p>Now that the HTML file is in flash we can write a regexp to find specific data. Creating <a href="http://livedocs.adobe.com/flash/9.0/main/00000118.html">named groups</a> within your regexp was new to me and an easy way to access specific data later on.</p>
<pre class="brush: html">//Create a regexp literal that will pull out the just the air temperature from the web page
var weatherPattern:RegExp = /Temperature&lt;BR&gt;.*&lt;strong&gt;(?P&lt;airtemp&gt;.*)°C&lt;\/strong&gt;/;</pre>
<p>The next step is to run that regexp against the text document. If a match is found you can access the whole string of the match or just the named group. </p>
<pre class="brush: html">//Load complete event handler that traces out the regular expression data
function weatherLoadComplete(e:Event):void{
    //Execute the pattern against the text document returning an object containing a match
    var match:Object = weatherPattern.exec(e.target.data);
    //Since exec returns an object this will can use the full match
    trace(match);
    //Or just the group
    trace(match.airtemp);
}
</pre>
<p>Complete source.</p>
<pre class="brush: html">//Create a request for the weather page
var myURLRequest:URLRequest = new URLRequest(&quot;http://www.weatheroffice.gc.ca/marine/marine_e.html?46146&quot;);
//Load the weather page into a urlloader object.
var myURLLoader:URLLoader = new URLLoader(myURLRequest);
//Listen for the load complete event for the URLLoader object
myURLLoader.addEventListener(Event.COMPLETE,weatherLoadComplete);
//Create a regexp literal that will pull out the just the air temperature from the web page
var weatherPattern:RegExp = /Temperature&lt;BR&gt;.*&lt;strong&gt;(?P&lt;airtemp&gt;.*)°C&lt;\/strong&gt;/;
//Load complete event handler that traces out the regular expression data
function weatherLoadComplete(e:Event):void{
    //Execute the pattern against the text document returning an object containing a match.
    var match:Object = weatherPattern.exec(e.target.data);
    //Since exec returns an object this will can use the full match
    trace(match);
    //Or just the group
    trace(match.airtemp);
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.thestem.ca/archives/88/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

