<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>RudiYardley.com &#187; Flash</title>
	<atom:link href="http://blog.rudiyardley.com/category/flash/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.rudiyardley.com</link>
	<description>Technical tidbits on Flash, Flex, Application Development, MODx, Silverstripe and Freelancing</description>
	<lastBuildDate>Tue, 31 Jan 2012 04:31:03 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to calculate the intersection of two circles mathematically in Actionscript.</title>
		<link>http://blog.rudiyardley.com/2010/01/how-to-calculate-the-intersection-of-two-circles-mathematically-in-actionscript/</link>
		<comments>http://blog.rudiyardley.com/2010/01/how-to-calculate-the-intersection-of-two-circles-mathematically-in-actionscript/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 11:20:58 +0000</pubDate>
		<dc:creator>rudi</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game development]]></category>
		<category><![CDATA[Scripted Animation]]></category>

		<guid isPermaLink="false">http://blog.rudestar.net/?p=279</guid>
		<description><![CDATA[A client of mine wanted to create an interactive teacher animation that pointed to a point on a chart that could be calculated as the result of a user quiz. They were stuck though as creating a realistic elbow movement that points to the desired point from a programmatic perspective is actually harder than it [...]]]></description>
			<content:encoded><![CDATA[<p>A client of mine wanted to create an interactive teacher animation that pointed to a point on a chart that could be calculated as the result of a user quiz. They were stuck though as creating a realistic elbow movement that points to the desired point from a programmatic perspective is actually harder than it seems as most teachers arms tend to have elbows. The solution took some time to come up with although simple enough in the end after a bit of geometric research. The first step to creating the animation was to calculate mathematically the intersections of two circles.</p>
<p>Finding the intersection of two circles appears complicated although in actual fact it is just a matter of unraveling Pythagoras&#8217; Theorem a few times. Pythagoras&#8217; Theorem states that the sum of square of the both straight sides of any right angled triangle is equal to the square of the diagonal.</p>
<p>ie. <em>a<sup>2</sup>=b<sup>2</sup>+c<sup>2</sup></em></p>
<p>Here is how you use it to find the intersection points of a circle with Actionscript:</p>
<p>1) Start by creating a couple of draggable circles and some hidden dots that will be used to indicate where the intersections are:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title="'actionscript'"><div class="devcodeoverflow"><ol><li>package</li><li>{</li><li>	import flash.display.Sprite;</li><li>	import flash.geom.Point;</li><li>	import flash.events.Event;</li><li>	import flash.events.MouseEvent;</li><li></li><li>	public class FindIntersection extends Sprite</li><li>	{</li><li>		public var circle1Radius:Number = 40;</li><li>		public var circle2Radius:Number = 70;</li><li>		public var circle1:Sprite = drawCircle(circle1Radius);</li><li>		public var circle2:Sprite = drawCircle(circle2Radius);</li><li>		public var dot1:Sprite = drawDot();</li><li>		public var dot2:Sprite = drawDot();</li><li></li><li>		function FindIntersection()</li><li>		{</li><li>			var origin:Point = new Point(stage.stageWidth/2, stage.stageHeight/2);</li><li>			circle1Radius = 40;</li><li>			circle2Radius = 70;</li><li>			circle1 = drawCircle(circle1Radius);</li><li>			circle2 = drawCircle(circle2Radius);</li><li>			dot1 = drawDot();</li><li>			dot2 = drawDot();</li><li>			circle1.x = origin.x - 10;</li><li>			circle1.y = origin.y;</li><li>			circle2.x = origin.x + 10;</li><li>			circle2.y = origin.y;</li><li>			addChild(circle1);</li><li>			addChild(circle2);</li><li>			addChild(dot1);</li><li>			addChild(dot2);</li><li>		}</li><li></li><li>		function drawCircle(rad:Number):Sprite</li><li>		{</li><li>			var c:Sprite = new Sprite();</li><li>			c.graphics.lineStyle(1,0x000000);</li><li>			c.graphics.beginFill(0xBBBBBB,.1);</li><li>			c.graphics.drawCircle(0,0,rad);</li><li>			c.graphics.endFill();</li><li>			c.graphics.moveTo(-5,0);</li><li>			c.graphics.lineTo(5,0);</li><li>			c.graphics.moveTo(0,-5);</li><li>			c.graphics.lineTo(0,5);</li><li>			c.addEventListener(MouseEvent.MOUSE_DOWN, onDown);</li><li>			c.addEventListener(MouseEvent.MOUSE_UP, onUp);</li><li>			return c;</li><li>		}</li><li></li><li>		function drawDot()</li><li>		{</li><li>			var d:Sprite = new Sprite();</li><li>			d.graphics.beginFill(0xFF0000);</li><li>			d.graphics.drawCircle(0,0,2);</li><li>			d.graphics.endFill();</li><li>			d.visible = false;</li><li>			return d;</li><li>		}</li><li></li><li>		function onDown(e:Event):void</li><li>		{</li><li>			var circle:Sprite = e.target&nbsp;&nbsp;as&nbsp;&nbsp;Sprite;</li><li>			circle.startDrag();</li><li>		}</li><li></li><li>		function onUp(e:Event):void</li><li>		{</li><li>			var circle:Sprite = e.target&nbsp;&nbsp;as&nbsp;&nbsp;Sprite;</li><li>			circle.stopDrag();</li><li>		}</li><li></li><li>	}</li><li>}</li></ol></div></pre><!--END_DEVFMTCODE--><br />
2) Next add an enterframe handler that checks for a collision by calculating the distance between the two circles. If the distance is greater than the sum of the radiae of both circles there is no intersection. Also if the distance is less than the absolute difference between the radiae then there is no solution as one circle is contained within the other.<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li>function enterFrameHandler<span class="br0">&#40;</span>e:Event<span class="br0">&#41;</span>:void</li><li><span class="br0">&#123;</span></li><li>	// Set up variables</li><li>	var r0:Number = circle1Radius;</li><li>	var r1:Number = circle2Radius;</li><li>&nbsp;</li><li>	// Point x and y coords</li><li>	var p0x:Number = circle1.x;</li><li>	var p0y:Number = circle1.y;</li><li>	var p1x:Number = circle2.x;</li><li>	var p1y:Number = circle2.y;</li><li>&nbsp;</li><li>	//First calculate the distance d between the center of the circles. d = ||P1 - P0||.</li><li>	var dx:Number = p0x - p1x;</li><li>	var dy:Number = p0y - p1y;</li><li>	var d:Number = Math.sqrt<span class="br0">&#40;</span>dx*dx + dy*dy<span class="br0">&#41;</span>;</li><li>&nbsp;</li><li>	//If d &gt; r0 + r1 then there are no solutions, the circles are separate.</li><li>	//If d &lt; |r0 - r1| then there are no solutions because one circle is contained within the other. 	if <span class="br0">&#40;</span><span class="br0">&#40;</span>d &gt; r0 + r1<span class="br0">&#41;</span> ||</li><li>	<span class="br0">&#40;</span>d &lt; Math.abs<span class="br0">&#40;</span>r0 - r1<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></li><li>	<span class="br0">&#123;</span></li><li>		dot1.visible = false;</li><li>		dot2.visible = false;</li><li>		return;</li><li>	<span class="br0">&#125;</span></li><li><span class="br0">&#125;</span></li></ol></div></pre><!--END_DEVFMTCODE--><br />
<img class="alignnone size-full wp-image-303" title="CircleIntersection1" src="http://blog.rudestar.net/wp-content/uploads//CircleIntersection1.jpg" alt="CircleIntersection1" width="400" height="300" /></p>
<p>The way we calculate distance is by using Pythagoras&#8217; formula by determining distance as a hypotenuse of a right angled triangle calculated as the square root of the square of the x-difference plus the square of the y-difference. ie.</p>
<p><em>d=√( dx<sup>2</sup>+dy<sup>2</sup> )</em></p>
<p>3) Now comes something slightly trickier. Consider the following diagram:</p>
<p><img class="alignnone size-full wp-image-293" title="CircleIntersection" src="http://blog.rudestar.net/wp-content/uploads//CircleIntersection.jpg" alt="CircleIntersection" width="400" height="300" /></p>
<p>Again using Pythagoras we can say that:</p>
<p><em>a<sup>2</sup>+ h<sup>2</sup>= r<sub>0</sub><sup>2</sup></em> is true for the red triangle and <em>b<sup>2</sup>+ h<sup>2</sup>= r<sub>1</sub><sup>2</sup></em> is true for the green triangle.</p>
<p>Now knowing that <em>d = a + b</em> we can then use a bit of algebra to solve for <em>a</em> in terms of<em> r<sub>0</sub></em>, <em>r<sub>1</sub></em> and <em>d</em> (I can show the working here if you want just leave a comment or send me a message):</p>
<p><em>a = (r<sub>0</sub><sup>2</sup> &#8211; r<sub>1</sub><sup>2</sup> + d<sup>2</sup> ) / (2 d)</em></p>
<p>Which we can write in actionscript as:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li>var a:Number = <span class="br0">&#40;</span> r0*r0 - r1*r1 + d*d <span class="br0">&#41;</span> / <span class="br0">&#40;</span>2*d<span class="br0">&#41;</span>;</li></ol></div></pre><!--END_DEVFMTCODE--><br />
Have a look at the following diagram:</p>
<p><img class="alignnone size-full wp-image-316" title="CircleIntersection2" src="http://blog.rudestar.net/wp-content/uploads//CircleIntersection2.jpg" alt="CircleIntersection2" width="400" height="300" /></p>
<p>Now since P<sub>2</sub> is along the line distance <em>d</em> and the <em>x</em> and <em>y</em> coordinates are relative to <em>a/d</em> therefore:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li>var p2x:Number = p0x + a * <span class="br0">&#40;</span> p1x - p0x <span class="br0">&#41;</span> / d;</li><li>var p2y:Number = p0y + a * <span class="br0">&#40;</span> p1y - p0y <span class="br0">&#41;</span> / d;</li></ol></div></pre><!--END_DEVFMTCODE--><br />
Since we know that <em>r<sub>0</sub><sup>2</sup> = h<sup>2</sup>+ a<sup>2</sup></em> and now that we have the value of <em>a</em> we can get <em>h</em> using pythagoras yet again:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li>var h:Number = Math.sqrt<span class="br0">&#40;</span>r0*r0 - a*a<span class="br0">&#41;</span>;</li></ol></div></pre><!--END_DEVFMTCODE--><br />
Now all that is left to do is project the positive <em>h</em> point and the negative <em>h</em> point:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li>	var d1x:Number = p2x + h * <span class="br0">&#40;</span> p1y - p0y <span class="br0">&#41;</span> / d;</li><li>	var d1y:Number = p2y - h * <span class="br0">&#40;</span> p1x - p0x <span class="br0">&#41;</span> / d;</li><li>	var d2x:Number = p2x - h * <span class="br0">&#40;</span> p1y - p0y <span class="br0">&#41;</span> / d;</li><li>	var d2y:Number = p2y + h * <span class="br0">&#40;</span> p1x - p0x <span class="br0">&#41;</span> / d;</li></ol></div></pre><!--END_DEVFMTCODE--><br />
Now we have the points of intersection, our file should look something like this:</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_0" width="530" height="398" align="left">
      <param name="movie" value="http://blog.rudestar.net/wp-content/uploads/FindIntersection1.swf" />
      <param name="align" value="left" />
      <param name="allowfullscreen" value="true" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.rudestar.net/wp-content/uploads/FindIntersection1.swf" width="530" height="398" align="left" allowfullscreen="true">
      <!--<![endif]-->
        <p>The Flash plugin is required to view this object.</p>
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p>Here is the <a href="http://blog.rudestar.net/wp-content/uploads/FindIntersection.zip">source code</a>.</p>
<p>Soon I plan to write about how to make an actual arm move based on this code but that is for another article.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.rudiyardley.com/2010/01/how-to-calculate-the-intersection-of-two-circles-mathematically-in-actionscript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>What is Flex?</title>
		<link>http://blog.rudiyardley.com/2010/01/what-is-flex/</link>
		<comments>http://blog.rudiyardley.com/2010/01/what-is-flex/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 12:52:56 +0000</pubDate>
		<dc:creator>rudi</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://blog.rudestar.net/?p=351</guid>
		<description><![CDATA[I suppose sometimes as a Flex or Flash developer you are so immersed in the whole SWF ecosystem that occasionally you get asked a question that should be easy to answer but becomes difficult to explain to someone without a technical perspective.
The other day I was preparing a spec and getting a colleague to double [...]]]></description>
			<content:encoded><![CDATA[<p>I suppose sometimes as a Flex or Flash developer you are so immersed in the whole SWF ecosystem that occasionally you get asked a question that should be easy to answer but becomes difficult to explain to someone without a technical perspective.</p>
<p>The other day I was preparing a spec and getting a colleague to double check my correspondence. They brought up that the client likely did not know what Flex was and that I should be prepared to explain it to the client in the meeting we were tabled to have. My first reaction was fine until it dawned upon me that finding the words to describe flex for someone who only really understands flash as a scripting and animation technology may actually be a little difficult so thought I would share with you what I came up with as well as ask for opinions on the matter.</p>
<p>Here are a few possible answers:</p>
<ol>
<li>Flex is a collection of compilers that output SWF bitecode</li>
<li>Flex is a framework that is based on MXML as a meta-language for forming SWF bytecode</li>
<li>The Flex framework uses both MXML and a bunch of custom Actionscript classes to define the various visual and non-visual elements of Rich Applications</li>
<li>Flex makes building Rich applications with Flash faster by leveraging the use of standard user interface components and layout method</li>
</ol>
<p>Any other thoughts?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.rudiyardley.com/2010/01/what-is-flex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to collision detection &amp; 2D physics</title>
		<link>http://blog.rudiyardley.com/2009/11/introduction-to-collision-detection-2d-physics/</link>
		<comments>http://blog.rudiyardley.com/2009/11/introduction-to-collision-detection-2d-physics/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 05:44:55 +0000</pubDate>
		<dc:creator>rudi</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game development]]></category>
		<category><![CDATA[Scripted Animation]]></category>
		<category><![CDATA[Physics]]></category>

		<guid isPermaLink="false">http://blog.rudestar.net/?p=147</guid>
		<description><![CDATA[This is an introductory tutorial on collision detection and physics. Creating 2D physics is often daunting to the beginner flash programmer but as you will soon see it is really not that hard so long as you break everything down into small steps. Here we are going to create a world and populate it with [...]]]></description>
			<content:encoded><![CDATA[<p>This is an introductory tutorial on collision detection and physics. Creating 2D physics is often daunting to the beginner flash programmer but as you will soon see it is really not that hard so long as you break everything down into small steps. Here we are going to create a world and populate it with a realistic bouncing ball:</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_1" width="530" height="398">
      <param name="movie" value="http://blog.rudestar.net/wp-content/uploads/BallWorld5.swf" />
      <param name="allowfullscreen" value="true" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.rudestar.net/wp-content/uploads/BallWorld5.swf" width="530" height="398" allowfullscreen="true">
      <!--<![endif]-->
        <p>The Flash plugin is required to view this object.</p>
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
 
<p>The concepts required here may look daunting but all that needs to be required are the following:</p>
<ul>
<li>Velocity</li>
<li>Wall Collision Detection</li>
<li>Friction</li>
<li>Gravity</li>
</ul>
<h2>Defining the world</h2>
<p>Lets start off by defining our bounds and our world as a class like so:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li>package</li><li><span class="br0">&#123;</span></li><li>	import flash.display.Sprite;</li><li>	import flash.events.Event;</li><li>	import flash.geom.Rectangle;</li><li>&nbsp;</li><li>	<span class="br0">&#91;</span>SWF<span class="br0">&#40;</span>width=&quot;530&quot;, height=&quot;397&quot;, frameRate=&quot;30&quot;, backgroundColor=&quot;#EEEEEE&quot;<span class="br0">&#41;</span><span class="br0">&#93;</span></li><li>	public class BallWorld extends Sprite</li><li>	<span class="br0">&#123;</span></li><li>		public function BallWorld<span class="br0">&#40;</span><span class="br0">&#41;</span></li><li>		<span class="br0">&#123;</span></li><li>			var world:World = new World<span class="br0">&#40;</span>530,397<span class="br0">&#41;</span>;</li><li>			addChild<span class="br0">&#40;</span>world<span class="br0">&#41;</span>;</li><li>		<span class="br0">&#125;</span></li><li>	<span class="br0">&#125;</span></li><li><span class="br0">&#125;</span></li><li>&nbsp;</li><li>import flash.display.Sprite;</li><li>import flash.events.Event;</li><li>import flash.geom.Rectangle;</li><li>import flash.events.MouseEvent;</li><li>&nbsp;</li><li>class World extends Sprite</li><li><span class="br0">&#123;</span></li><li>&nbsp;</li><li>	public function World<span class="br0">&#40;</span>w:Number, h:Number<span class="br0">&#41;</span></li><li>	<span class="br0">&#123;</span></li><li>		var backing:Sprite;</li><li>		backing = new Sprite<span class="br0">&#40;</span><span class="br0">&#41;</span>;</li><li>		backing.graphics.lineStyle<span class="br0">&#40;</span>1,0xAAAAAA<span class="br0">&#41;</span>;</li><li>		backing.graphics.beginFill<span class="br0">&#40;</span>0xFFFFFF<span class="br0">&#41;</span>;</li><li>		backing.graphics.drawRect<span class="br0">&#40;</span>0,0,400,300<span class="br0">&#41;</span>;</li><li>		backing.x = w/2 - backing.width/<span style="">2</span>;</li><li>		backing.y = h/2 - backing.height/<span style="">2</span>;</li><li>		addChild<span class="br0">&#40;</span>backing<span class="br0">&#41;</span>;</li><li>&nbsp;</li><li>		var ball:Ball;</li><li>		ball = new Ball<span class="br0">&#40;</span>20<span class="br0">&#41;</span>;</li><li>		ball.x = w/<span style="">2</span>;</li><li>		ball.y = h/<span style="">2</span>;</li><li>		addChild<span class="br0">&#40;</span>ball<span class="br0">&#41;</span>;</li><li>&nbsp;</li><li>	<span class="br0">&#125;</span></li><li><span class="br0">&#125;</span></li><li>&nbsp;</li><li>class Ball extends Sprite</li><li><span class="br0">&#123;</span></li><li>	public var radius:Number = <span style="">20</span>;	//	circle radius</li><li>&nbsp;</li><li>	public function Ball<span class="br0">&#40;</span>rad:Number<span class="br0">&#41;</span></li><li>	<span class="br0">&#123;</span></li><li>		//	Apply params</li><li>		radius = rad;</li><li>&nbsp;</li><li>		//	Draw Ball</li><li>		graphics.lineStyle<span class="br0">&#40;</span>1,0xAAAAAA<span class="br0">&#41;</span>;</li><li>		graphics.beginFill<span class="br0">&#40;</span>0xA9C065<span class="br0">&#41;</span>;</li><li>		graphics.drawCircle<span class="br0">&#40;</span>0,0,radius<span class="br0">&#41;</span>;</li><li>		graphics.endFill<span class="br0">&#40;</span><span class="br0">&#41;</span>;</li><li>&nbsp;</li><li>	<span class="br0">&#125;</span></li><li><span class="br0">&#125;</span></li></ol></div></pre><!--END_DEVFMTCODE--><br />

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_2" width="530" height="398">
      <param name="movie" value="http://blog.rudestar.net/wp-content/uploads/BallWorld1.swf" />
      <param name="allowfullscreen" value="true" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.rudestar.net/wp-content/uploads/BallWorld1.swf" width="530" height="398" allowfullscreen="true">
      <!--<![endif]-->
        <p>The Flash plugin is required to view this object.</p>
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
</p>
<p>Here we start with a World class that contains a backing sprite and a Ball Sprite which we have placed in the middle of the bounds. This tute is all about creating some realistic movement based on basic physics so lets get the ball to move.</p>
<h2>Velocity</h2>
<p>Within the physical world moving objects have a velocity which represents a distance moved over time. In Actionscript, we have a frame ticker which represents time so once every frame we can add the velocity to the coordinate position of our sprite to give the appearance of movement. Velocity in physics is given as a vector but in Actionscript we can provide it as two separate values for each coordinate. Lets add velocity to our ball:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li>class Ball extends Sprite</li><li><span class="br0">&#123;</span></li><li>	public var vx:Number = <span style="">0</span>;		//	velocity x</li><li>	public var vy:Number = <span style="">0</span>;		//	velocity y</li><li>	public var radius:Number = <span style="">20</span>;	//	circle radius</li><li>&nbsp;</li><li>	public function Ball<span class="br0">&#40;</span>rad:Number<span class="br0">&#41;</span></li><li>	<span class="br0">&#123;</span></li><li>		//	Apply params</li><li>		radius = rad;</li><li>&nbsp;</li><li>		//	Draw Ball</li><li>		graphics.lineStyle<span class="br0">&#40;</span>1,0xAAAAAA<span class="br0">&#41;</span>;</li><li>		graphics.beginFill<span class="br0">&#40;</span>0xA9C065<span class="br0">&#41;</span>;</li><li>		graphics.drawCircle<span class="br0">&#40;</span>0,0,radius<span class="br0">&#41;</span>;</li><li>		graphics.endFill<span class="br0">&#40;</span><span class="br0">&#41;</span>;</li><li>&nbsp;</li><li>		//	Add Listeners</li><li>		addEventListener<span class="br0">&#40;</span>Event.ENTER_FRAME, onFrame<span class="br0">&#41;</span>;</li><li>	<span class="br0">&#125;</span></li><li>&nbsp;</li><li>	private function onFrame<span class="br0">&#40;</span>e:Event<span class="br0">&#41;</span>:void</li><li>	<span class="br0">&#123;</span></li><li>		//	Apply Velocity to Position</li><li>		x += vx;</li><li>		y += vy;</li><li>&nbsp;</li><li>	<span class="br0">&#125;</span></li><li><span class="br0">&#125;</span></li></ol></div></pre><!--END_DEVFMTCODE--><br />
Notice nothing much has happened. Our Ball is still sitting in the center of the stage and not moving. The reason it isn&#8217;t moving is because we have given it an initial velocity of 0 for both the x and y directions. Lets give this a number value of lets say 5. Add the following to our World class:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li>...</li><li>var ball:Ball;</li><li>ball = new Ball<span class="br0">&#40;</span>20<span class="br0">&#41;</span>;</li><li>ball.x = w/<span style="">2</span>;</li><li>ball.y = h/<span style="">2</span>;</li><li>&nbsp;</li><li>//	Initial Velocity</li><li>ball.vx = <span style="">5</span>;</li><li>ball.vy = <span style="">5</span>;</li><li>...</li></ol></div></pre><!--END_DEVFMTCODE--><br />
Which gives us this:</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_3" width="530" height="398">
      <param name="movie" value="http://blog.rudestar.net/wp-content/uploads/BallWorld2.swf" />
      <param name="allowfullscreen" value="true" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.rudestar.net/wp-content/uploads/BallWorld2.swf" width="530" height="398" allowfullscreen="true">
      <!--<![endif]-->
        <p>The Flash plugin is required to view this object.</p>
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p>Bingo! the ball is moving but it is hardly realistic. Firstly the ball leaves the bounds almost immediately so we need to provide some collision detection and to constrain it to the given bounds.</p>
<h2>Wall Collision Detection</h2>
<p>Let&#8217;s add the following handler and variable to our Ball class:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li>private var bounds:Rectangle;</li><li>...</li><li>public function Ball<span class="br0">&#40;</span>rad:Number<span class="br0">&#41;</span></li><li><span class="br0">&#123;</span></li><li>...</li><li>	//	Add Listeners</li><li>	addEventListener<span class="br0">&#40;</span>Event.ENTER_FRAME, onFrame<span class="br0">&#41;</span>;</li><li>	addEventListener<span class="br0">&#40;</span>Event.ADDED, onAdded<span class="br0">&#41;</span>;</li><li><span class="br0">&#125;</span></li><li>...</li><li>private function onAdded<span class="br0">&#40;</span>e:Event<span class="br0">&#41;</span>:void</li><li><span class="br0">&#123;</span></li><li>	world = parent as World;</li><li>	bounds = world.bounds;</li><li><span class="br0">&#125;</span></li></ol></div></pre><!--END_DEVFMTCODE--><br />
This is going to listen for when we add our ball to the world and at that instance get the properties of our world and apply them to the ball animation.</p>
<p>now add the following to our frame routine:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li>private function onFrame<span class="br0">&#40;</span>e:Event<span class="br0">&#41;</span>:void</li><li><span class="br0">&#123;</span></li><li>	//	Use a temp var for x and y to increase performance</li><li>	var tx:Number = x;</li><li>	var ty:Number = y;</li><li>&nbsp;</li><li>	//	Collision Detection with right wall</li><li>	if<span class="br0">&#40;</span>tx + vx &amp;gt; bounds.right - radius<span class="br0">&#41;</span></li><li>	<span class="br0">&#123;</span></li><li>		tx = bounds.right - radius;</li><li>		vx *= -<span style="">1</span>;</li><li>	<span class="br0">&#125;</span></li><li>&nbsp;</li><li>	//	Collision Detection with left wall</li><li>	if<span class="br0">&#40;</span>tx + vx &amp;lt; bounds.left + radius<span class="br0">&#41;</span> 	<span class="br0">&#123;</span></li><li> 		tx = bounds.left + radius;</li><li> 		vx *= -<span style="">1</span>;</li><li>	<span class="br0">&#125;</span></li><li>&nbsp;</li><li>&nbsp;&nbsp;	//	Collision Detection with floor</li><li>	if<span class="br0">&#40;</span>ty + vy &amp;gt; bounds.bottom - radius<span class="br0">&#41;</span></li><li>	<span class="br0">&#123;</span></li><li>		ty = bounds.bottom - radius;</li><li>		vy *= -<span style="">1</span>;</li><li>	<span class="br0">&#125;</span></li><li>&nbsp;</li><li>	//	Collision Detection with ceiling</li><li>	if<span class="br0">&#40;</span>ty + vy &amp;lt; bounds.top + radius<span class="br0">&#41;</span></li><li>	<span class="br0">&#123;</span></li><li>		ty = bounds.top + radius;</li><li>		vy *= -<span style="">1</span>;</li><li>	<span class="br0">&#125;</span></li><li>&nbsp;</li><li>	//	Apply Velocity to tx and ty</li><li>	tx += vx;</li><li>	ty += vy;</li><li>&nbsp;</li><li>	//	Render the change</li><li>	x = tx;</li><li>	y = ty;</li><li>&nbsp;</li><li><span class="br0">&#125;</span></li></ol></div></pre><!--END_DEVFMTCODE--><br />
Lastly we need to alter our World class to provide us with the bounds property containing the bounds to constrict the ball to. So lets move our backing instance to become a private instance variable and access its getBounds method for the bounds of the world.<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li>class World extends Sprite</li><li><span class="br0">&#123;</span></li><li>...</li><li>	private var backing:Sprite;</li><li>...</li><li>	public function get bounds<span class="br0">&#40;</span><span class="br0">&#41;</span>:Rectangle</li><li>	<span class="br0">&#123;</span></li><li>		return backing.getBounds<span class="br0">&#40;</span>this<span class="br0">&#41;</span>;</li><li>	<span class="br0">&#125;</span></li><li><span class="br0">&#125;</span></li></ol></div></pre><!--END_DEVFMTCODE--><br />
Within the frame script we are now checking to see if the object has moved beyond the boundary given by the bounds Rectangle. If the bounds have been exceeded for any given direction the direction is reversed based on the boundary exceeded.</p>
<p>Here we also use a temporary variable to make it so that x is only set at the end of the frame script and is only necessary for performance reasons but is a good habit to get into since it is good to keep data and render separate.</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_4" width="530" height="398">
      <param name="movie" value="http://blog.rudestar.net/wp-content/uploads/BallWorld3.swf" />
      <param name="allowfullscreen" value="true" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.rudestar.net/wp-content/uploads/BallWorld3.swf" width="530" height="398" allowfullscreen="true">
      <!--<![endif]-->
        <p>The Flash plugin is required to view this object.</p>
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p>So now we have our ball flying away within our bounding box but the movement doesn&#8217;t look particularly real as we haven&#8217;t added any of the common physical forces to our object such as friction or gravity.</p>
<h2>Adding Friction</h2>
<p>To add air friction we simply need to multiply the velocity by a given amount between 0 and 1 every frame ie:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li>//	Air Dampening</li><li>vx *= .99;</li><li>vy *= .99;</li></ol></div></pre><!--END_DEVFMTCODE--><br />
This leaves us with something that looks like the following:</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_5" width="530" height="398">
      <param name="movie" value="http://blog.rudestar.net/wp-content/uploads/BallWorld4.swf" />
      <param name="allowfullscreen" value="true" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.rudestar.net/wp-content/uploads/BallWorld4.swf" width="530" height="398" allowfullscreen="true">
      <!--<![endif]-->
        <p>The Flash plugin is required to view this object.</p>
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<h2>Gravity</h2>
<p>The last thing to do is add gravity to simulate the natural pull towards the ground by simply adding to the downward velocity every frame:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li>//	Air Dampening</li><li>vx *= .99;</li><li>vy *= .99;	</li><li>&nbsp;</li><li>//	Gravity</li><li>vy += <span style="">1</span>;	</li><li>&nbsp;</li><li>//	Apply Velocity to tx and ty</li><li>tx += vx;</li><li>ty += vy;</li><li>&nbsp;</li><li>//	Render the change</li><li>x = tx;</li><li>y = ty;</li></ol></div></pre><!--END_DEVFMTCODE--><br />
After moving the gravity and friction properties to belong to the World we are left with the following full source code:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li>package</li><li><span class="br0">&#123;</span></li><li>	import flash.display.Sprite;</li><li>	import flash.events.Event;</li><li>	import flash.geom.Rectangle;</li><li>&nbsp;</li><li>	<span class="br0">&#91;</span>SWF<span class="br0">&#40;</span>width=&quot;530&quot;, height=&quot;397&quot;, frameRate=&quot;30&quot;, backgroundColor=&quot;#EEEEEE&quot;<span class="br0">&#41;</span><span class="br0">&#93;</span></li><li>	public class BallWorld extends Sprite</li><li>	<span class="br0">&#123;</span></li><li>		public function BallWorld<span class="br0">&#40;</span><span class="br0">&#41;</span></li><li>		<span class="br0">&#123;</span></li><li>			var world:World = new World<span class="br0">&#40;</span>530,397<span class="br0">&#41;</span>;</li><li>			addChild<span class="br0">&#40;</span>world<span class="br0">&#41;</span>;</li><li>		<span class="br0">&#125;</span></li><li>	<span class="br0">&#125;</span></li><li><span class="br0">&#125;</span></li><li>&nbsp;</li><li>import flash.display.Sprite;</li><li>import flash.events.Event;</li><li>import flash.geom.Rectangle;</li><li>import flash.events.MouseEvent;</li><li>&nbsp;</li><li>class World extends Sprite</li><li><span class="br0">&#123;</span></li><li>	private var backing:Sprite;</li><li>	public var friction:Number;</li><li>	public var gravity:Number;</li><li>&nbsp;</li><li>	public function World<span class="br0">&#40;</span>w:Number, h:Number<span class="br0">&#41;</span></li><li>	<span class="br0">&#123;</span></li><li>		friction = <span style="">0.95</span>;</li><li>		gravity = <span style="">1</span>;</li><li>&nbsp;</li><li>		backing = new Sprite<span class="br0">&#40;</span><span class="br0">&#41;</span>;</li><li>		backing.graphics.lineStyle<span class="br0">&#40;</span>1,0xAAAAAA<span class="br0">&#41;</span>;</li><li>		backing.graphics.beginFill<span class="br0">&#40;</span>0xFFFFFF<span class="br0">&#41;</span>;</li><li>		backing.graphics.drawRect<span class="br0">&#40;</span>0,0,400,300<span class="br0">&#41;</span>;</li><li>		backing.x = w/2 - backing.width/<span style="">2</span>;</li><li>		backing.y = h/2 - backing.height/<span style="">2</span>;</li><li>		addChild<span class="br0">&#40;</span>backing<span class="br0">&#41;</span>;</li><li>&nbsp;</li><li>		var ball:Ball;</li><li>		ball = new Ball<span class="br0">&#40;</span>20<span class="br0">&#41;</span>;</li><li>		ball.x = w/<span style="">2</span>;</li><li>		ball.y = h/<span style="">2</span>;</li><li>&nbsp;</li><li>		//	Initial Velocity</li><li>		ball.vx = <span style="">5</span>;</li><li>		ball.vy = <span style="">5</span>;</li><li>&nbsp;</li><li>		addChild<span class="br0">&#40;</span>ball<span class="br0">&#41;</span>;</li><li>&nbsp;</li><li>	<span class="br0">&#125;</span></li><li>&nbsp;</li><li>	public function get bounds<span class="br0">&#40;</span><span class="br0">&#41;</span>:Rectangle</li><li>	<span class="br0">&#123;</span></li><li>		return backing.getBounds<span class="br0">&#40;</span>this<span class="br0">&#41;</span>;</li><li>	<span class="br0">&#125;</span></li><li><span class="br0">&#125;</span></li><li>&nbsp;</li><li>class Ball extends Sprite</li><li><span class="br0">&#123;</span></li><li>	public var vx:Number = <span style="">0</span>;		//	velocity x</li><li>	public var vy:Number = <span style="">0</span>;		//	velocity y</li><li>	public var radius:Number = <span style="">20</span>;	//	circle radius</li><li>	private var bounds:Rectangle;</li><li>	private var friction:Number;</li><li>	private var gravity:Number;</li><li>&nbsp;</li><li>	public function Ball<span class="br0">&#40;</span>rad:Number<span class="br0">&#41;</span></li><li>	<span class="br0">&#123;</span></li><li>		//	Apply params</li><li>		radius = rad;</li><li>&nbsp;</li><li>		//	Draw Ball</li><li>		graphics.lineStyle<span class="br0">&#40;</span>1,0xAAAAAA<span class="br0">&#41;</span>;</li><li>		graphics.beginFill<span class="br0">&#40;</span>0xA9C065<span class="br0">&#41;</span>;</li><li>		graphics.drawCircle<span class="br0">&#40;</span>0,0,radius<span class="br0">&#41;</span>;</li><li>		graphics.endFill<span class="br0">&#40;</span><span class="br0">&#41;</span>;</li><li>&nbsp;</li><li>		//	Add Listeners</li><li>		addEventListener<span class="br0">&#40;</span>Event.ENTER_FRAME, onFrame<span class="br0">&#41;</span>;</li><li>		addEventListener<span class="br0">&#40;</span>Event.ADDED, onAdded<span class="br0">&#41;</span>;</li><li>	<span class="br0">&#125;</span></li><li>&nbsp;</li><li>	private function onAdded<span class="br0">&#40;</span>e:Event<span class="br0">&#41;</span>:void</li><li>	<span class="br0">&#123;</span></li><li>		var world:World = parent as World;</li><li>		bounds = world.bounds;</li><li>		friction = world.friction;</li><li>		gravity = world.gravity;</li><li>	<span class="br0">&#125;</span></li><li>&nbsp;</li><li>	private function onFrame<span class="br0">&#40;</span>e:Event<span class="br0">&#41;</span>:void</li><li>	<span class="br0">&#123;</span></li><li>		//	Use a temp var for x and y to increase performance</li><li>		var tx:Number = x;</li><li>		var ty:Number = y;</li><li>&nbsp;</li><li>		//	Collision Detection with right wall</li><li>		if<span class="br0">&#40;</span>tx + vx &amp;gt; bounds.right - radius<span class="br0">&#41;</span></li><li>		<span class="br0">&#123;</span></li><li>			tx = bounds.right - radius;</li><li>			vx *= -<span style="">1</span>;</li><li>		<span class="br0">&#125;</span></li><li>&nbsp;</li><li>		//	Collision Detection with left wall</li><li>		if<span class="br0">&#40;</span>tx + vx &amp;lt; bounds.left + radius<span class="br0">&#41;</span> 		<span class="br0">&#123;</span> 			tx = bounds.left + radius; 			vx *= -<span style="">1</span>; 		<span class="br0">&#125;</span> 		 		//	Collision Detection with floor 		if<span class="br0">&#40;</span>ty + vy &amp;gt; bounds.bottom - radius<span class="br0">&#41;</span></li><li>		<span class="br0">&#123;</span></li><li>			ty = bounds.bottom - radius;</li><li>			vy *= -<span style="">1</span>;</li><li>		<span class="br0">&#125;</span></li><li>&nbsp;</li><li>		//	Collision Detection with ceiling</li><li>		if<span class="br0">&#40;</span>ty + vy &amp;lt; bounds.top + radius<span class="br0">&#41;</span></li><li>		<span class="br0">&#123;</span></li><li>			ty = bounds.top + radius;</li><li>			vy *= -<span style="">1</span>;</li><li>		<span class="br0">&#125;</span></li><li>&nbsp;</li><li>		//	Air Dampening</li><li>		vx *= friction;</li><li>		vy *= friction;	</li><li>&nbsp;</li><li>		vy += gravity;</li><li>&nbsp;</li><li>		//	Apply Velocity to tx and ty</li><li>		tx += vx;</li><li>		ty += vy;</li><li>&nbsp;</li><li>		//	Render the change</li><li>		x = tx;</li><li>		y = ty;</li><li>&nbsp;</li><li>	<span class="br0">&#125;</span></li><li><span class="br0">&#125;</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>As you can see by adding gravity we substantially increase the reality factor of the motion.</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_6" width="530" height="398">
      <param name="movie" value="http://blog.rudestar.net/wp-content/uploads/BallWorld5.swf" />
      <param name="allowfullscreen" value="true" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.rudestar.net/wp-content/uploads/BallWorld5.swf" width="530" height="398" allowfullscreen="true">
      <!--<![endif]-->
        <p>The Flash plugin is required to view this object.</p>
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
 
]]></content:encoded>
			<wfw:commentRss>http://blog.rudiyardley.com/2009/11/introduction-to-collision-detection-2d-physics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SEO and Flash</title>
		<link>http://blog.rudiyardley.com/2009/07/seo-and-flash/</link>
		<comments>http://blog.rudiyardley.com/2009/07/seo-and-flash/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 21:19:13 +0000</pubDate>
		<dc:creator>rudi</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[deeplinking]]></category>
		<category><![CDATA[seo]]></category>
		<category><![CDATA[swfaddress]]></category>

		<guid isPermaLink="false">http://blog.rudestar.net/?p=98</guid>
		<description><![CDATA[
I have been doing a bit of research on the subject of SEO accessibility and full flash websites and have put together a few responses on common queries or concerns regarding the subject:

Flash can not be indexed by google using flash is bad for SEO.
Users cannot click the back button in a flash site
Users cannot [...]]]></description>
			<content:encoded><![CDATA[<div>
<p>I have been doing a bit of research on the subject of SEO accessibility and full flash websites and have put together a few responses on common queries or concerns regarding the subject:</p>
<ol>
<li>Flash can not be indexed by google using flash is bad for SEO.</li>
<li>Users cannot click the back button in a flash site</li>
<li>Users cannot directly link into a content page on a flash only site</li>
</ol>
<p>In response I have put together a couple of guidelines:</p>
<ol>
<li>Prepare a CMS serving content to two different output templates one for flash one for html. Serve flash using SWFObject within every html page of content into the div that contains the html content. Representatives from Google have said that whilst this method can draw attention since it is open to being exploited, so long as the content is identical to the site content should not be considered as cloaking and should not be penalised.</li>
<li>Serving the Flash on every page is done so that should a user click a link in google the website will load up the web page should inject a flash var to the swf as well so that the swf can redirect to the correct content.</li>
<li>Deeplinking can be accomplished by writing an anchor to the url that is relavent to the page or by using SWFAddress</li>
<li>Another way of creating permanent links to content is to have a link to this page link which displays the link url (cf google maps). This method is considered a little safer appending an anchor esp due to bugs within ie although browser history will be compromised.</li>
<li>A last homebrew method for creating browser history is to add an iframe to the page and mirror page changes with the iframe this is the method that Flex uses withi its history template.</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.rudiyardley.com/2009/07/seo-and-flash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

