<?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>sqrtof5</title>
	<atom:link href="http://blog.sqrtof5.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://blog.sqrtof5.com</link>
	<description>Coder&#039;s journal of Arnaud Icard</description>
	<lastBuildDate>Tue, 05 Jan 2010 17:19:58 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>New year</title>
		<link>http://blog.sqrtof5.com/?p=238</link>
		<comments>http://blog.sqrtof5.com/?p=238#comments</comments>
		<pubDate>Tue, 05 Jan 2010 17:19:58 +0000</pubDate>
		<dc:creator>Arnaud</dc:creator>
				<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://blog.sqrtof5.com/?p=238</guid>
		<description><![CDATA[<p>Hopefully I will be able to post more. Unlikely. But who knows? Anyways, to start off the new year, I posted this <a href="http://blog.sqrtof5.com/?p=173">simple gesture recognition</a> thing. Only mildly interesting, already kinda obsolete. But hey, in the process of doing things, one learns I believe.</p>]]></description>
			<content:encoded><![CDATA[<p>Hopefully I will be able to post more. Unlikely. But who knows? Anyways, to start off the new year, I posted this <a href="http://blog.sqrtof5.com/?p=173">simple gesture recognition</a> thing. Only mildly interesting, already kinda obsolete. But hey, in the process of doing things, one learns I believe.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sqrtof5.com/?feed=rss2&amp;p=238</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Simple gesture recognition in AS3</title>
		<link>http://blog.sqrtof5.com/?p=173</link>
		<comments>http://blog.sqrtof5.com/?p=173#comments</comments>
		<pubDate>Tue, 05 Jan 2010 15:13:36 +0000</pubDate>
		<dc:creator>Arnaud</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>

		<guid isPermaLink="false">http://blog.sqrtof5.com/?p=173</guid>
		<description><![CDATA[<p><a href="http://blog.sqrtof5.com/?p=173"><img class="summary_img" src="http://blog.sqrtof5.com/wp-content/uploads/2010/01/post5_summary.png" alt="A simple gesture recognition engine in AS3"/></a></p>

<p>A simple recognition engine in AS3. Source code available at Github.</p>]]></description>
			<content:encoded><![CDATA[<p>A few years ago I coded a simple AS2 gesture recognition package in the hope I could use it in the future. Well. 5 years have passed. No real project have ever made use of it, except a small prototype for a (successful) pitch for Nintendo&#8230; well&#8230; you know&#8230; they&#8217;ve got that little DS thing&#8230; seemd appropriate, I guess. As often though, that which have been pitched wasn&#8217;t built, and the whole gesture/drawable UI component got ditched. And now I need this thing to work again, and I&#8217;m scared to even take a look at the old AS2 base code. Well it turns out, that stuff (in this simple form) isn&#8217;t really complicated and rewriting the whole thing from scratch didn&#8217;t take long at all.</p>
<p>
<h2>Gesture recognition, the basic kind</h2>
</p>
<div id="attachment_64" class="wp-caption alignnone" style="width: 510px">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_GestureRecognition_921810803"
			class="flashmovie"
			width="500"
			height="411">
	<param name="movie" value="http://blog.sqrtof5.com/wp-content/uploads/2010/01/GestureRecognition.swf" />
	<param name="quality" value="high" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://blog.sqrtof5.com/wp-content/uploads/2010/01/GestureRecognition.swf"
			name="fm_GestureRecognition_921810803"
			width="500"
			height="411">
		<param name="quality" value="high" />
	<!--<![endif]-->
		<a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a>
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object><p class="wp-caption-text"><b>GestureRecognition.swf</b><i>, draw the symbols</i></p></div>
<p>
<h2>How it works</h2>
</p>
<p>This gesture recognition method is done in three steps:</p>
<p>
<ul>
<li>Capture user input</li>
<li>Homogenize and normalize input</li>
<li>Match to a predefined pattern</li>
</ul>
<p>The first step is trivial and won&#8217;t be discussed. The second step is best described with a diagram. I chose to use an approach relying on a line segment/circle intersection algorithm. The goal is to get a path defined by equally distanced points, scaled to fit a 1&#215;1 square. We can obtain this by iterating through the initial path, and finding intersections with a circle of radius <code>r</code>. This radius is very simply determined by dividing the length of the initial path by the number of points desired in the resulting path.</p>
<p>Note that the simplified path is most certainly going to be shorter than the initial path as a result of the process (see the diagram). In some occasions, the final path will effectively &#8220;miss&#8221; points. In this implementation we stop as soon as there is no intersection left with the next circle. We could think of lots of complicated and/or &#8220;expensive&#8221; ways to get that done more accurately. But really, as will show the next diagram, this does not affect the recognition itself.</p>
<div id="attachment_64" class="wp-caption alignnone" style="width: 510px"><img class="size-full wp-image-64" title="Diagram" src="http://blog.sqrtof5.com/wp-content/uploads/2010/01/diagram5.png" alt="Processing user input, for a simple gesture recognition engine" width="500" height="350" /><p class="wp-caption-text"><i>Processing user input</i></p></div>
<p/>
<p>Once we have a normalized path, we can find a matching pattern in the dictionary. Comparing the data sets is now simply a matter of comparing where the points are relatively to each other within this 1&#215;1 square.</p>
<div id="attachment_64" class="wp-caption alignnone" style="width: 510px"><img class="size-full wp-image-64" title="Diagram" src="http://blog.sqrtof5.com/wp-content/uploads/2010/01/diagram6.png" alt="Finding a match, for a simple gesture recognition engine" width="500" height="350" /><p class="wp-caption-text"><i>Finding a match</i></p></div>
<p/>
<p>Since the points are ordered (there is a first point and a last point, and the order matters) we can find the average distance between the paths. We can expect the best matching pattern to have the lower average distance between its points and the normalized user generated path, as pictured on the diagram.</p>
<p>Note that the implementation I propose shows no optimization whatsoever. It will bluntly go through each and every pattern in the dictionary. There is also a total lack of quality control (or threshold value) regarding the &#8220;best match&#8221;. Whatever you draw, a match will be found. I personally think it is the way it should work, but well, you can disagree and add some extra checks yourself (see in the source, there are a couple hints on where to look).</p>
<p>I can see two very easy ways to optimize the search though. The relative position of the first points is absolutely important. Should you draw one of these symbols backwards, matching exactly the shape, well&#8230; it&#8217;s very unlikely it&#8217;ll be the best match. We can easily extrapolate that too great of a distance between the first points should discard the pattern (let&#8217;s say something like <code>>=.5</code>). What is also meaningful is the length of these paths, which should be &#8220;close enough&#8221;. A very first check combining a comparison in length and relative position of first points could discard a good number of irrelevant patterns. Then&#8230; we can look for the best match in whatever remains. Writing this, I suddenly feel like implementing it. Well. I certainly will if this gets used, as I hope, in a real project. Which, yes, is still up in the air.</p>
<p>
<h2>Useful links</h2>
</p>
<p><a href="http://github.com/sqrtof5/GestureRecognition" target="new">Github repo (includes rough gesture creation tool)</a><br />
<a href="http://local.wasp.uwa.edu.au/~pbourke/geometry/sphereline/" target="new">Paul Bourke&#8217;s line/sphere intersection algorithm</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sqrtof5.com/?feed=rss2&amp;p=173</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>A Wonderwall-like effect, with Traer AS3</title>
		<link>http://blog.sqrtof5.com/?p=122</link>
		<comments>http://blog.sqrtof5.com/?p=122#comments</comments>
		<pubDate>Fri, 27 Nov 2009 17:35:32 +0000</pubDate>
		<dc:creator>Arnaud</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Traer AS3 port]]></category>

		<guid isPermaLink="false">http://blog.sqrtof5.com/?p=122</guid>
		<description><![CDATA[<p><a href="http://blog.sqrtof5.com/?p=122"><img class="summary_img" src="http://blog.sqrtof5.com/wp-content/uploads/2009/11/post4_summary.png" alt="A Wonderwall effect with Traer for AS3"/></a></p>

<p>Using the ActionScript3 port of the Traer physics engine, get something close to THA's <a href="http://wonder-wall.com/" target="new">Wonderwall</a> effect. Source code available at Github.</p>]]></description>
			<content:encoded><![CDATA[<p>There are so many reasons to admire <a href="http://yugop.com/" target="new">Yugo Nakamura&#8217;s work</a> I wouldn&#8217;t know where to start. The incredible range, quality, and singularity of work on display at <a href="http://tha.jp/" target="new">tha.jp</a> (the studio&#8217;s site) is simply puzzling. I had the chance to see Yugo&#8217;s talk at <a href="http://www.fitc.ca/" target="new">FITC</a> in Toronto a few years ago, and the modesty and insight he showed was absolutely amazing. One of his topics was -if I remember well- about over sophistication, and how he relentlessly seeks to push boundaries emerging from a seemingly simple form. The <a href="http://wonder-wall.com/" target="new">Wonderwall</a> is in my opinion a perfect example of that commitment. And as you&#8217;ll see, we&#8217;ll barely scratch the surface of it and yet have a peek at how technically and artistically refined the original interactive piece is.</p>
<p>
<h2>A Wonderwall-like thingy with Traer AS3</h2>
</p>
<p>What this little experiment is focusing on is the deformation of the grid <i>when interacting</i>. The underlying grid pattern is not stretched and deformed as in the original (which I do realize is absolutely part of the awesomeness of it). This experiment solely focuses on the use of <i>physics simulation</i> to recreate the effect, and yes, there&#8217;s way more to it in the real thing. Anyways, here&#8217;s the quick experiment, in just shy of 370 lines of non-engine code:</p>
<div id="attachment_64" class="wp-caption alignnone" style="width: 535px">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_WonderwallLikeRefined_965729752"
			class="flashmovie"
			width="525"
			height="650">
	<param name="movie" value="http://blog.sqrtof5.com/wp-content/uploads/2009/11/WonderwallLikeRefined.swf" />
	<param name="quality" value="high" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://blog.sqrtof5.com/wp-content/uploads/2009/11/WonderwallLikeRefined.swf"
			name="fm_WonderwallLikeRefined_965729752"
			width="525"
			height="650">
		<param name="quality" value="high" />
	<!--<![endif]-->
		<a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a>
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object><p class="wp-caption-text"><b>WonderwallLikeRefined.swf</b><i>, hover the grid</i></p></div>
<p>
<h2>The differences with the original</h2>
</p>
<p>As previously mentioned, the grid pattern is regular. Rollovers are basic (lines connecting the particles). There&#8217;s no text, just images.<br />
But from a code perspective, the main difference is probably that there&#8217;s springiness in this experiment, while THA&#8217;s original piece shows none. The reason is (I&#8217;m guessing) that another integration method was used to set the constraints: <a href="http://en.wikipedia.org/wiki/Verlet_integration" target="new">Verlet integration</a>. Traer uses a <a href="http://en.wikipedia.org/wiki/Runge_kutta" target="new">Runge-Kutta integrator</a> which is less suitable for stiff joints, usually approximated by infinite stiffness springs. Well, no need to hide it. I kept the springiness in.</p>
<p>
<h2>How this is done</h2>
</p>
<div id="attachment_64" class="wp-caption alignnone" style="width: 510px"><img class="size-full wp-image-64" title="Diagram" src="http://blog.sqrtof5.com/wp-content/uploads/2009/11/diagram2.png" alt="Using two sets of particles for a Wonderwall effect in Flash" width="500" height="350" /><p class="wp-caption-text"><i>Using two sets of particles</i></p></div>
<p/>
<p>I believe this diagram is pretty self explanatory. Two sets of particles are in use, linked together in a one-to-one relation (in our case using a <code>Spring</code>). Attached to the mouse is an <code>Attractor</code> (with negative strength) that repulses the free particles. A face of the final and deformed grid is defined by four free particles. Now, if we were to use this as-is, we would get an awful amount of distortion within each image.</p>
<div id="attachment_64" class="wp-caption alignnone" style="width: 510px"><img class="size-full wp-image-64" title="Diagram" src="http://blog.sqrtof5.com/wp-content/uploads/2009/11/diagram3.png" alt="Subdividing faces for a Wonderwall effect in Flash" width="500" height="350" /><p class="wp-caption-text"><i>Subdividing the faces</i></p></div>
<p/>
<p>Given these four points, some subdivision is necessary to minimize distortion showing the triangles used to draw the faces. In the <code>bin/</code> directory, you will find one .swf without face subdivision: not so pretty. In the source code (<code>WonderwallLikeRefined.as</code>), I&#8217;m using 6 subdivisions (72 triangles) for each face.<br />
There&#8217;s a lot of room for improvement there, the number of subdivisions required to draw a nice face depends on its dimensions and the amount of deformation applied. One possible improvement could be to check the difference in slope between the top and bottom edges: the greater the difference, the greater the number of subdivisions necessary. I am inclined to think that in THA&#8217;s piece some system is in place: their grid is huge, runs real smooth, with minimal distortion.</p>
<div id="attachment_64" class="wp-caption alignnone" style="width: 510px"><img class="size-full wp-image-64" title="Diagram" src="http://blog.sqrtof5.com/wp-content/uploads/2009/11/diagram4.png" alt="Finding if a point is inside a polygon for a Wonderwall effect in Flash" width="500" height="350" /><p class="wp-caption-text"><i>Where is the mouse?</i></p></div>
<p/>
<p>Another problem arise. How do we handle rollovers? One simple way is to check if the mouse position is within a given polygon (here, a face defined by 4 free particles). Luckily, <a href="http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/" target="new">Paul Bourke&#8217;s line-to-line intersection algorithm</a> comes in handy, as illustrated in the diagram above. If we take half a line starting from the current mouse position and passing through an arbitrary point, the mouse is inside the polygon if the number of intersections is an odd number (or just 1 or 3 in our case). Yup. It&#8217;s that simple!</p>
<p>Again, in this piece of code, I am performing the check for every single face every time the physics simulation advances. A variety of techniques could be used to improve this. One way to do this could be to perform this test on the polygon encompassing half the particles. If the test yields a true, divide it in half. And again, until we get a false. Or the subset is a face. Yup, even a good old <a href="http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm" target="new">divide and conquer</a> would be a real improvement.</p>
<p>
<h2>A disclaimer</h2>
</p>
<p>I don&#8217;t pretend I know for sure how the original Wonderwall was built by THA. They may have taken a totally different approach and the only way to make sure would be to ask them. But I&#8217;m too shy. Or to decompile the .swf, which would be totally sacrilegious given how much I respect these guys and their work. So really, I have no clue. This is simply an exploration of how one could have done something a bit similar with the <a href="http://blog.sqrtof5.com/?p=53">Traer physics engine for AS3</a>. And this was only driven by my admiration for the studio&#8217;s work, and sheer curiosity (how did they do it??! this is sooo cool!!).</p>
<p>
<h2>Useful links</h2>
</p>
<p><a href="http://github.com/sqrtof5/traerAS3" target="new">Github repo for TraerAs3 (includes the Wonderwall-like effect files)</a><br />
<a href="http://wonder-wall.com/" target="new">The gorgeous original Wonderwall</a><br />
<a href="http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/" target="new">Paul Bourke&#8217;s line-to-line intersection algorithm</a><br />
<a href="http://tha.jp/" target="new">Tha, Yugo Nakamura&#8217;s studio: an absolute must-see</a><br />
<a href="http://yugop.com/" target="new">And of course, Yugop</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sqrtof5.com/?feed=rss2&amp;p=122</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Traer AS3, the mandatory cloth simulation</title>
		<link>http://blog.sqrtof5.com/?p=44</link>
		<comments>http://blog.sqrtof5.com/?p=44#comments</comments>
		<pubDate>Tue, 24 Nov 2009 19:20:34 +0000</pubDate>
		<dc:creator>Arnaud</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Traer AS3 port]]></category>

		<guid isPermaLink="false">http://blog.sqrtof5.com/?p=44</guid>
		<description><![CDATA[<p><a href="http://blog.sqrtof5.com/?p=44"><img class="summary_img" src="http://blog.sqrtof5.com/wp-content/uploads/2009/11/post3_summary.png" alt="TraerAS3 cloth simulation"/></a></p>

<p>A cloth simulation built with Traer.physics port to ActionScript3, using <code>GraphicsTrianglePath</code>. Source code available at Github.</p>]]></description>
			<content:encoded><![CDATA[<p>I believe every single particle engine starts with that as an example: the classic cloth-like simulation. The original <a href="http://www.cs.princeton.edu/~traer/physics/" target="new">Traer.physics package</a> has it. So here it is, adapted from the original source code. But since just lines and dots don&#8217;t really make the thing look like anything, I added some <code>GraphicsTrianglePath</code> goodness in there. The mesh is only made of a 8&#215;8 particles grid, or 128 triangles when rendered, which really isn&#8217;t much!</p>
<div id="attachment_64" class="wp-caption alignnone" style="width: 510px">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_ClothTriangles_781350056"
			class="flashmovie"
			width="500"
			height="500">
	<param name="movie" value="http://blog.sqrtof5.com/wp-content/uploads/2009/11/ClothTriangles.swf" />
	<param name="quality" value="high" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://blog.sqrtof5.com/wp-content/uploads/2009/11/ClothTriangles.swf"
			name="fm_ClothTriangles_781350056"
			width="500"
			height="500">
		<param name="quality" value="high" />
	<!--<![endif]-->
		<a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a>
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object><p class="wp-caption-text"><b>ClothTriangles.swf</b><i>, drag handles to move</i></p></div>
<p/>
<p>The source code for this is part of <a href="http://github.com/sqrtof5/traerAS3" target="new">my Traer.physics port to ActionScript3</a>, and is available at Github. There really isn&#8217;t much to it, but if you haven&#8217;t yet used <code>GraphicsTrianglePath</code> or this little physics package, then it&#8217;s probably an okay-ish place to start.</p>
<p>
<h2>Useful links</h2>
</p>
<p><a href="http://github.com/sqrtof5/traerAS3" target="new">Github repo for TraerAS3</a><br />
<a href="http://www.adobe.com/livedocs/flex/3/langref/flash/display/GraphicsTrianglePath.html" target="new">Livedocs about GraphicsTrianglePath</a></p>
<p/>
]]></content:encoded>
			<wfw:commentRss>http://blog.sqrtof5.com/?feed=rss2&amp;p=44</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Traer.physics port to AS3</title>
		<link>http://blog.sqrtof5.com/?p=53</link>
		<comments>http://blog.sqrtof5.com/?p=53#comments</comments>
		<pubDate>Tue, 24 Nov 2009 16:17:04 +0000</pubDate>
		<dc:creator>Arnaud</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Traer AS3 port]]></category>

		<guid isPermaLink="false">http://blog.sqrtof5.com/?p=53</guid>
		<description><![CDATA[<p><a href="http://blog.sqrtof5.com/?p=53"><img class="summary_img" src="http://blog.sqrtof5.com/wp-content/uploads/2009/11/post2_summary.png" alt="Port of Traer.physics to AS3"/></a></p>

<p>Port to AS3 of the venerable Traer.physics Java library. Source code available at Github.</p>]]></description>
			<content:encoded><![CDATA[<p>Long-time Processing users probably remember <a href="http://www.cs.princeton.edu/~traer/physics/" target="new">Traer.physics</a>, one of the first physics libraries for <a href="http://processing.org/" target="new">Processing</a>, written in Java by Jeffrey Traer Bernstein. It was widely used long before the incredibly useful and much-larger-in-scope <a href="http://code.google.com/p/toxiclibs/" target="new">ToxicLibs</a> were around. So what is it? A very simple particle system.</p>
<p>As Jeffrey puts it on his project page:</p>
<blockquote><p>&#8220;All this is supposed to do is let you make particles, apply forces and calculate the positions of particles over time in real-time. Anything else you need to handle yourself&#8221;.</p></blockquote>
<p>And well, this is, in my opinion, the beauty of it. Traer physics is really a lot of fun to play with. Granted, there isn&#8217;t a lot provided. A <code>ParticleSystem</code> class. A <code>Particle</code> class. <code>Spring</code> and <code>Attraction</code> (which acts also as repulsion by negating a value). That&#8217;s pretty much it. But it&#8217;s way more than enough to provide a sandbox to come up in no time with new ideas for interaction. Or cool visuals. Or whatever.</p>
<p>
<h2>What&#8217;s in it?</h2>
</p>
<p>
<h3>ParticleSystem</h3>
<p>This is where everything starts. It provides a dead easy API. The methods listed below are pretty much all you need to know get started:</p>
<p>
<ul>
<li><code>ParticleSystem(gravity:Vector3D, drag:Number); <font color=#727272>// constructor</font></code></li>
<li><code>ParticleSystem.makeParticle(mass:Number, position:Vector3D):Particle;</code></li>
<li><code>ParticleSystem.makeSpring(a:Particle, b:Particle, springConstant:Number, damping:Number, restLength:Number):Spring;</code></li>
<li><code>ParticleSystem.makeAttraction(a:Particle, b:Particle, strength:Number, minDistance:Number):Attraction;</code></li>
<li><code>ParticleSystem.tick(t:Number):void; <font color=#727272>// advances the simulation by an amount of time</font></code></li>
<li><code>ParticleSystem.addCustomForce(f:Force):void; <font color=#727272>// this is how you add your custom designed forces that implement Force</font></code></li>
</ul>
<p>Of course, a complete API to add/remove/get/set particles, forces, and various settings is provided (i.e. <code>getSpring</code>, <code>getParticle</code>, <code>removeSpringByReference</code>, <code>removeParticleByReference</code>&#8230;).</p>
<p>
<h3>Particle</h3>
<p>Can be fixed or free using <code>Particle.makeFixed()</code> and <code>Particle.makeFree()</code>, and have <code>mass</code> and <code>age</code> properties.</p>
<p>
<h3>Integrator</h3>
<p>Your choice of <code>RungeKuttaIntegrator</code> (more stable, more precise, slower) or <code>ModifiedEulerIntegrator</code> (fast, less precise, at times barely stable).<br />
Hmm&#8230; What does stability mean? We&#8217;re talking about <a href="http://en.wikipedia.org/wiki/Numerical_stability" target="new">numerical stability</a> here. The wikipedia entry for the <a href="http://en.wikipedia.org/wiki/Euler_method" target="new">Euler method</a> explains very well what it means. In short, there is a correlation in the magnitude of errors occurring and the time increment used when calling <code>ParticleSystem.tick(t)</code>. On top of that, some situations will make the integrator simply fail. Oh no worries. You&#8217;ll see&#8230; particles supposedly fixed flying around and your entire simulation exploding. Fun times! Ways of controlling your system&#8217;s stability include choosing a smaller time increment (slowing down the simulation) and add more drag to the system (making it harder for the particles to freely move).</p>
<p>
<h3>Force</h3>
<p><code>Spring</code> and <code>Attraction</code> are provided. You are more than encouraged to create your own forces by implementing the <code>Force</code> interface.</p>
<p>
<h2>The port, some nitty gritty details</h2>
</p>
<p>Some changes to the API were necessary since ActionScript3 doesn&#8217;t allow for method overload. Traer.physics being originally written in Java, well, these were all over. I had to limit the options available.</p>
<p>I also chose not to use the original package custom vector class, since AS3 provides a standard <code>Vector3D</code>. This should make it easier to plug into this package other libraries. That change means the <code>position</code> property of <code>Particle</code> is actually a <code>flash.geom.Vector3D</code>.</p>
<p><code>ParticleSystem</code> uses vector quantities to store particles and forces. <code>Vector.&lt;Particle&gt;</code>, <code>Vector.&lt;Spring&gt;</code>, <code>Vector.&lt;Attraction&gt;</code>, <code>Vector.&lt;Force&gt;</code> have been used where the original code uses Java&#8217;s <code>ArrayList</code>.</p>
<p>
<h2>Examples</h2>
</p>
<p>I included a bunch of examples in the package. I will post some explanations regarding some of them shortly. These include an orb web like little toy, a tribute to Yugo&#8217;s Wonderwall, and yes, the mandatory cloth simulation.</p>
<p>Oh, you may want to see a little something right away don&#8217;t you? Here are 700 free floating particles. Incredibly boring, but so little code was written for that!</p>
<div id="attachment_64" class="wp-caption alignnone" style="width: 360px">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_FreeFloatingTest_572700968"
			class="flashmovie"
			width="350"
			height="280">
	<param name="movie" value="http://blog.sqrtof5.com/wp-content/uploads/2009/11/FreeFloatingTest.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://blog.sqrtof5.com/wp-content/uploads/2009/11/FreeFloatingTest.swf"
			name="fm_FreeFloatingTest_572700968"
			width="350"
			height="280">
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object><p class="wp-caption-text"><b>FreeFloating.swf</b><i>, hover to disturb</i></p></div>
<p>
<h2>What&#8217;s missing/What&#8217;s next?</h2>
</p>
<p>Proper ASDocs and unit testing. I&#8217;ll try to get that done at some point&#8230; Performance tuning/optimization. I&#8217;m sure there&#8217;s a *lot* of room for improvement. Oh, and more examples!</p>
<p>
<h2>Remind me again, why porting it?</h2>
</p>
<p>Quite some time ago I used to play a lot with this package in Processing. I loved it. Some of the little experiments I did at this time&#8230; I thought&#8230; Damn, I wished Flash could handle that. Now I guess it can. Now, too, Jeffrey released the source code. I had to port over this thing to AS3, mostly for the fond memories I had of it. As a tribute to the good old times where people like Jeffrey, among others, were shaping what Processing is now: the enabler of data visualization for a wide and diverse crowd with only one requirement: a curious mind.</p>
<p/>
<p>
<h2>Useful links</h2>
</p>
<p><a href="http://github.com/sqrtof5/traerAS3" target="new">Get the source + examples: Github repo</a><br />
<a href="http://www.cs.princeton.edu/~traer/physics/" target="new">Traer.physics: the original package</a><br />
<a href="http://processing.org/" target="new">Processing</a><br />
<a href="http://code.google.com/p/toxiclibs/" target="new">Toxiclibs</a><br />
<a href="http://en.wikipedia.org/wiki/Numerical_stability" target="new">http://en.wikipedia.org/wiki/Numerical_stability</a><br />
<a href="http://en.wikipedia.org/wiki/Euler_method" target="new">http://en.wikipedia.org/wiki/Euler_method</a><br />
<a href="http://en.wikipedia.org/wiki/Runge-Kutta_method" target="new">http://en.wikipedia.org/wiki/Runge-Kutta_method</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sqrtof5.com/?feed=rss2&amp;p=53</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Stitching planes seamlessly in FP10: 3D grass</title>
		<link>http://blog.sqrtof5.com/?p=6</link>
		<comments>http://blog.sqrtof5.com/?p=6#comments</comments>
		<pubDate>Sun, 22 Nov 2009 17:37:19 +0000</pubDate>
		<dc:creator>Arnaud</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>

		<guid isPermaLink="false">http://blog.sqrtof5.com/?p=6</guid>
		<description><![CDATA[<p><a href="http://blog.sqrtof5.com/?p=6"><img class="summary_img" src="http://blog.sqrtof5.com/wp-content/uploads/2009/11/post1_summary.png" alt="3D grass in AS3"/></a></p>

<p>3D grass experiment... Stitching sprite instances together seamlessly. Easy technique, although using <code>GraphicsTrianglePath</code> would give better results. Source code available at Github.</p>]]></description>
			<content:encoded><![CDATA[<p>There is one thing I really wanted to do as soon as the Flash Player 10 was released, before getting familiar with the power behind <code>GraphicsTrianglePath</code>, and that was to stitch <code>Sprite</code> instances together seamlessly in one direction. I think the simplicity of the technique is interesting and relevant is peculiar cases, so here it is.</p>
<p>Before looking at the extremely simple math behind it, here&#8217;s the use I made of it: Randomly generated grass stands, in a billboarding fashion.</p>
<div id="attachment_64" class="wp-caption alignnone" style="width: 510px">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_GrassPatch_1056780068"
			class="flashmovie"
			width="500"
			height="350">
	<param name="movie" value="http://blog.sqrtof5.com/wp-content/uploads/2009/11/GrassPatch.swf" />
	<param name="quality" value="medium" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://blog.sqrtof5.com/wp-content/uploads/2009/11/GrassPatch.swf"
			name="fm_GrassPatch_1056780068"
			width="500"
			height="350">
		<param name="quality" value="medium" />
	<!--<![endif]-->
		<a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a>
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object><p class="wp-caption-text"><b>GrassPatch.swf</b><i>, click and drag to rotate</i></p></div>
<p/>
<p>Again, way better results would be obtained with <code>GraphicsTrianglePath</code>. Oh, yup, and the code -you will soon realize- is quite messy, this is really only a quick and dirty proof of concept. Alright. Enough disclaimers, how is this little thing done? Since we know the planes will be aligned along X, all we need to do is to find the correct y and z positions, as shown in this diagram:</p>
<div id="attachment_64" class="wp-caption alignnone" style="width: 510px"><img class="size-full wp-image-64" title="Diagram" src="http://blog.sqrtof5.com/wp-content/uploads/2009/11/diagram1.png" alt="How to stitch planes together in Flash" width="500" height="350" /><p class="wp-caption-text"><i>How to stitch planes together in Flash</i></p></div>
<p/>
<p>Assuming the registration point of our sprites is at their center, and that we want to align these planes vertically, then we really want to translate <code>C1</code> to <code>C0</code>. <code>P0</code> is known to us. <code>P1</code> is an arbitrary position (we&#8217;ll use the sprite&#8217;s local coordinates to find <code>C1</code>). The orientation of the planes along one axis is known as well (in that case, along Z). The dimensions before any transformation, is also known for both sprites (segment length).<br />
So really, all we&#8217;ve got to do is to find <code>C0</code> and <code>C1</code>, and translate <code>C1</code> to <code>C0</code>.</p>
<p><code>C0.y = P0.y + Math.sin(theta) *(length*.5);<br />
C0.z = P0.z + Math.cos(theta)*(length*.5);<br />
C1.y = P1.y + Math.sin(omega+Math.PI)*(length*.5);<br />
C1.z = P1.z + Math.cos(omega+Math.PI)*(length*.5);</code</p>
<p>We can now translate <code>P1</code> to <code>C1</code>. There are many ways to do this. In the provided code, y is first set to <code>-i*length</code> then the difference between <code>C0</code> and <code>C1</code> is added to this (effectively translating from an incorrect guessed position to the correct position):</p>
<p><code>P1.x = P0.x;<br />
P1.y =  -i*length + (C0.y - C1.y);<br />
P1.z = C0.z - C1.z;></code></p>
<p><h2>Useful links</h2>
<p><a href="http://github.com/sqrtof5/GrassPatch/" target="new">Get the source: Git repo on github</a><br />
<a href="http://en.wikipedia.org/wiki/2.5D#Billboarding" target="new">Wikipedia, about billboarding</a><br />
<a href="http://www.adobe.com/livedocs/flex/3/langref/flash/display/GraphicsTrianglePath.html" target="new">Livedocs, about GraphicsTrianglePath</a>
</p>
<p/>
]]></content:encoded>
			<wfw:commentRss>http://blog.sqrtof5.com/?feed=rss2&amp;p=6</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Hello world!</title>
		<link>http://blog.sqrtof5.com/?p=1</link>
		<comments>http://blog.sqrtof5.com/?p=1#comments</comments>
		<pubDate>Sun, 22 Nov 2009 01:28:57 +0000</pubDate>
		<dc:creator>Arnaud</dc:creator>
				<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://blog.sqrtof5.com/?p=1</guid>
		<description><![CDATA[<p>Finally. It's set up, and running. Something I should have done years ago, even when insanely busy with client works, and kids, and moving from France to the US to Canada, and a thousand other reasons.
This blog, a place to share some code, and keep track of my own special interests.</p>]]></description>
			<content:encoded><![CDATA[<p>Fresh off Wordpress, blog.sqrtof5.com. There it is. My own little place to share some code, little experiments and some thoughts. Not that I think it is in any way really interesting for anybody else than myself, but I finally got over getting it up and running. A long overdue effort to share these little snippets, these toys in byte code with source included. All as a mean to keep track of my very own &#8220;coder&#8217;s obsessions&#8221;.</p>
<p>These posts are to be mainly –I&#8217;m hoping– the main pages for GitHub repos, where you can fork, modify, repost, rewrite, any of it. Make them better (or just plain &#8220;good&#8221;– some won&#8217;t be to begin with :$). Or just use them. Or even simply criticize, say they&#8217;re stupid. The point being, it&#8217;s an interesting effort to me to self-document my very own activity. Or lack thereof.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sqrtof5.com/?feed=rss2&amp;p=1</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
