<?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; Physics</title>
	<atom:link href="http://blog.rudiyardley.com/tag/physics/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>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_0" 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_1" 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_2" 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_3" 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_4" 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_5" 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>
	</channel>
</rss>

