<?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; Source Code</title>
	<atom:link href="http://blog.thestem.ca/archives/category/source-code/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>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>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>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>
		<item>
		<title>Distance in Wiiflash, Head Tracking Now Possible in Flash.</title>
		<link>http://blog.thestem.ca/archives/86</link>
		<comments>http://blog.thestem.ca/archives/86#comments</comments>
		<pubDate>Sun, 16 Mar 2008 21:01:20 +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/archives/86</guid>
		<description><![CDATA[Inspired by Jonny Lee&#8217;s head tracking video, I decided to see what it would take to get it working in flash. 4 hours later I got a dirty implementation working with Wiiflash. I haven&#8217;t tested this for accuracy but it works well enough to mess about with. Tyler Egeto, one of my students in Digital [...]]]></description>
			<content:encoded><![CDATA[<p>Inspired by <a href="http://procrastineering.blogspot.com/">Jonny Lee&#8217;s</a> <a href="http://youtube.com/watch?v=Jd3-eiid-Uw">head tracking</a> video, I decided to see what it would take to get it working in flash. 4 hours later I got a dirty implementation working with <a href="http://www.wiiflash.org">Wiiflash</a>. I haven&#8217;t tested this for accuracy but it works well enough to mess about with. <a href="http://www.tyleregeto.com/">Tyler Egeto</a>, one of my students in <a href="http://vfs.com/fulltime.php?id=13">Digital Design</a>, has done a <a href="http://blog.papervision3d.org">Papervision</a> demo using the new functionality which should be available next week. Download the modified class file: <a href="http://blog.thestem.ca/wp-content/source/IR.zip">IR.as</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thestem.ca/archives/86/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Processing Controlling a Color Kinetics LED String Over Ethernet</title>
		<link>http://blog.thestem.ca/archives/81</link>
		<comments>http://blog.thestem.ca/archives/81#comments</comments>
		<pubDate>Sat, 22 Dec 2007 03:28:56 +0000</pubDate>
		<dc:creator>Brettf</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[Lighting]]></category>
		<category><![CDATA[Processing]]></category>
		<category><![CDATA[Source Code]]></category>
		<category><![CDATA[color kinetics]]></category>
		<category><![CDATA[engineer]]></category>
		<category><![CDATA[ethernet]]></category>
		<category><![CDATA[header]]></category>
		<category><![CDATA[LED]]></category>
		<category><![CDATA[packet]]></category>
		<category><![CDATA[reverse]]></category>
		<category><![CDATA[sound]]></category>
		<category><![CDATA[visualization]]></category>

		<guid isPermaLink="false">http://blog.thestem.ca/archives/81</guid>
		<description><![CDATA[Below you will find code that will allow you to control a Color Kinetics iColor Flex SL LED string via UDP. Why should you care? Well this will allow control of the string directly from your computer via ethernet. If you can&#8217;t see the power in that then maybe your inner nerd isn&#8217;t shining through. [...]]]></description>
			<content:encoded><![CDATA[<p>Below you will find code that will allow you to control a Color Kinetics iColor Flex SL LED string via UDP. Why should you care? Well this will allow control of the string directly from your computer via ethernet. If you can&#8217;t see the power in that then maybe your inner nerd isn&#8217;t shining through.<br />
Here is a video showing it in action.<br />
<object type="application/x-shockwave-flash" width="" height="" data="http://www.vimeo.com/moogaloop.swf?clip_id=445798&amp;server=www.vimeo.com&amp;fullscreen=1&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color="><param name="quality" value="best" /><param name="allowfullscreen" value="true" /><param name="scale" value="showAll" /><param name="movie" value="http://www.vimeo.com/moogaloop.swf?clip_id=445798&amp;server=www.vimeo.com&amp;fullscreen=1&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=" /></object></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thestem.ca/archives/81/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Flashduino!</title>
		<link>http://blog.thestem.ca/archives/77</link>
		<comments>http://blog.thestem.ca/archives/77#comments</comments>
		<pubDate>Sun, 04 Nov 2007 03:29:02 +0000</pubDate>
		<dc:creator>Brettf</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Source Code]]></category>
		<category><![CDATA[arduino flash binary socket source code xmlsocket tinke]]></category>

		<guid isPermaLink="false">http://blog.thestem.ca/archives/77</guid>
		<description><![CDATA[I have been working with Arduino lately. I must say it has made me really excited about technology again. There is something about creating physical devices that is more rewarding then always working on virtual projects. Having said that, ever since I started working with Arduino I have wanted to make interfaces for it with [...]]]></description>
			<content:encoded><![CDATA[<p>I have been working with <a href="http://www.arduino.cc/">Arduino</a> lately. I must say it has made me really excited about technology again. There is something about creating physical devices that is more rewarding then always working on virtual projects. Having said that, ever since I started working with Arduino I have wanted to make interfaces for it with <a href="http://www.adobe.com/products/flash/">Flash</a>. The tutorials I have found for connecting the two all exploited the XMLSocket object in AS2. Using the XMLSocket has some small issues to it, namely it needs a null byte on the end of each message. ActionScript 3 fixes this little speed bump by including a binary socket that does not need a null byte to end a message. I had to try it out for myself so I put together a small demo of serial communication between Flash and Arduino using the <a href="http://tinker.it/now/2007/05/11/tinkerproxy-for-windows/">TinkerProxy</a>.</p>
<p><a href="http://blog.thestem.ca/wp-content/source/Flash+Arduino.zip">Get it here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thestem.ca/archives/77/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Arduino Servo Control for Beginners</title>
		<link>http://blog.thestem.ca/archives/74</link>
		<comments>http://blog.thestem.ca/archives/74#comments</comments>
		<pubDate>Sat, 03 Nov 2007 17:43:07 +0000</pubDate>
		<dc:creator>Brettf</dc:creator>
				<category><![CDATA[arduino]]></category>
		<category><![CDATA[Electronics]]></category>
		<category><![CDATA[Source Code]]></category>

		<guid isPermaLink="false">http://blog.thestem.ca/archives/74</guid>
		<description><![CDATA[I managed to get my servo working with Arduino today using this tutorial. Nothing to special. The first time I tried nothing happened because I had the min/max pulse time setup wrong for the hitec servo. With the hitec servos the min pulse is 900&#181;sec and the max is 2100&#181;sec. Once I changed that it [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://blog.thestem.ca/wp-content/uploads/2007/11/arduino-servo-setup-1.jpg' title='arduino-servo-setup-1.jpg'><img src='http://blog.thestem.ca/wp-content/uploads/2007/11/arduino-servo-setup-1.jpg' alt='arduino-servo-setup-1.jpg' class="alignleft" /></a>I managed to get my <a href="http://www.hitecrcd.com/servos/show?name=HS-311">servo</a> working with <a href="http://www.arduino.cc">Arduino</a> today using this <a href="http://itp.nyu.edu/physcomp/Labs/Servo">tutorial</a>. Nothing to special. The first time I tried nothing happened because I had the min/max pulse time setup wrong for the hitec servo. With the hitec servos the min pulse is 900&micro;sec and the max is 2100&micro;sec. Once I changed that it worked and should work for all hitec servos. I have looked at the Arduino servo libraries and they all seem unfinished or limited in one way or another. I am currently working on a sketch that will allow the control of up to 11 servos with analog inputs or serial commands. Also in the works is an Aduino servo shield.</p>
<p><a href="http://blog.thestem.ca/wp-content/source/Analog_Servo.zip">Arduino Servo Sketch for analog control of a hitec servo</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thestem.ca/archives/74/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

