<?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>Cheezeworld &#187; Programming Tidbits</title>
	<atom:link href="http://cheezeworld.com/category/programming-tidbits/feed/" rel="self" type="application/rss+xml" />
	<link>http://cheezeworld.com</link>
	<description>The Cheeze to Your Macaroni - Games, Open Source Programming, and Other Odd Ramblings.</description>
	<lastBuildDate>Mon, 04 Jan 2010 18:23:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Programming Tidbit #3 &#8211; Constraining Rotation to a Max Turn Rate Per Second</title>
		<link>http://cheezeworld.com/constraining-rotation-max-turn-rate/</link>
		<comments>http://cheezeworld.com/constraining-rotation-max-turn-rate/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 14:57:58 +0000</pubDate>
		<dc:creator>Colby Cheeze</dc:creator>
				<category><![CDATA[Games]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Tidbits]]></category>
		<category><![CDATA[actionscript 3]]></category>
		<category><![CDATA[Cannon]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[Rotations]]></category>
		<category><![CDATA[Turn Rate]]></category>
		<category><![CDATA[Turret]]></category>

		<guid isPermaLink="false">http://cheezeworld.com/?p=177</guid>
		<description><![CDATA[
This is the third post in the series “Programming Tidbits.” These articles consist of useful bits of information or code that I would like to share, but are not necesarily full length articles.
Sometimes you may wish to constrain the amount of rotation an entity can rotate per second or tick or whatever, such as the [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-medium wp-image-178" title="d_turret" src="http://cheezeworld.com/wp-content/uploads/2009/01/d_turret-300x225.jpg" alt="d_turret" width="300" height="225" /></p>
<p>This is the third post in the series “Programming Tidbits.” These articles consist of useful bits of information or code that I would like to share, but are not necesarily full length articles.</p>
<p>Sometimes you may wish to constrain the amount of rotation an entity can rotate per second or tick or whatever, such as the turret on a tank or the turning of the tank itself. This can be done with a bit of math. It could seem a bit complicated without using the math libraries I have already created.</p>
<p align="left">
<h4>Here it is using my code:</h4>
<p>We are going to assume the maxTurnRate will be in degrees per second.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> maxTurnRate <span style="color: #66cc66;">&amp;</span>gt; <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&amp;</span>amp;<span style="color: #66cc66;">&amp;</span>amp; currentVelocity.<span style="color: #006600;">angleTo</span><span style="color: #66cc66;">&#40;</span> newVelocity <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;</span>gt; <span style="color: #66cc66;">&#40;</span> maxTurnRate <span style="color: #66cc66;">*</span> MathUtils.<span style="color: #006600;">DEG_TO_RAD</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> stepSize <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
 	<span style="color: #000000; font-weight: bold;">var</span> mat:Matrix2D = <span style="color: #000000; font-weight: bold;">new</span> Matrix2D<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	mat.<span style="color: #006600;">rotate</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span> maxTurnRate <span style="color: #66cc66;">*</span> MathUtils.<span style="color: #006600;">DEG_TO_RAD</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> stepSize <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> heading.<span style="color: #006600;">sign</span><span style="color: #66cc66;">&#40;</span> newVelocity <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
 	mat.<span style="color: #006600;">transformVector</span><span style="color: #66cc66;">&#40;</span> currentVelocity<span style="color: #66cc66;">&#41;</span>;
 	mat.<span style="color: #006600;">transformVector</span><span style="color: #66cc66;">&#40;</span> heading <span style="color: #66cc66;">&#41;</span>;
 <span style="color: #66cc66;">&#125;</span>
 <span style="color: #b1b100;">else</span>
 <span style="color: #66cc66;">&#123;</span>
 	velocity.<span style="color: #006600;">x</span> = _newVel.<span style="color: #006600;">x</span>;
 	velocity.<span style="color: #006600;">y</span> = _newVel.<span style="color: #006600;">y</span>;
 <span style="color: #66cc66;">&#125;</span></pre></div></div>

<h2>The Code Explained</h2>
<p>So let&#8217;s take a look at what is happening there. First, we are checking to see if maxTurnRate is &gt; 0&#8230;because we will assume if it is set as 0 then the entity should be able to turn freely. If it is greater, then we will get the angle from our current velocity and check to see if it is greater than the max turn rate multiplied by the step size. The step size is generally just: tickSize / 1000.</p>
<p>The math to get the angle from one vector to another is:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> angleTo<span style="color: #66cc66;">&#40;</span> vector1:Vector2D, vector2:Vector2D <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">acos</span><span style="color: #66cc66;">&#40;</span> dotProduct<span style="color: #66cc66;">&#40;</span> vector1, vector2 <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #66cc66;">&#40;</span> vector1.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">*</span> vector2.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> dotProduct<span style="color: #66cc66;">&#40;</span> vector1:Vector2D, vector2:Vector2D <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span> vector1.<span style="color: #006600;">x</span> <span style="color: #66cc66;">*</span> vector2.<span style="color: #006600;">x</span> <span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span> vector1.<span style="color: #006600;">y</span> <span style="color: #66cc66;">*</span> vector2.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Next, if the rotation is greater than the max amount aloud&#8230;we will use a matrix to rotate the velocity vector to it&#8217;s correct position and transform it and the heading ( the direction the entity is going ). Note: The &#8220;sign&#8221; function just returns -1 if vector is counterclockwise, else 1 if clockwise. We need this to know which direction to rotate obviously.</p>
<h2>The Matrix Code</h2>
<p>Here is some of the matrix code, though I definitely don&#8217;t plan on explaining matrices in this post. Which is why it&#8217;s best to just use the library if you don&#8217;t actually understand the math part itself. <img src='http://cheezeworld.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**
 * Use to rotate.
 * ( Considers that the Vector object represents a point, not an actual vector )
 * @param rot
 *
 */</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> rotate<span style="color: #66cc66;">&#40;</span>rot:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">sin</span>:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sin</span><span style="color: #66cc66;">&#40;</span>rot<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">cos</span>:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">cos</span><span style="color: #66cc66;">&#40;</span>rot<span style="color: #66cc66;">&#41;</span>;
	multiply<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> Matrix2D<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">cos</span>, <span style="color: #0066CC;">sin</span>, <span style="color: #cc66cc;">0</span>, -<span style="color: #0066CC;">sin</span>, <span style="color: #0066CC;">cos</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/**
 * Use to multiply two Matrixes...mainly used internally.
 * @param matrix The Matrix to multiply by.
 *
 */</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> multiply<span style="color: #66cc66;">&#40;</span>matrix:Matrix2D<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> m:Matrix2D = <span style="color: #000000; font-weight: bold;">new</span> Matrix2D<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//first row</span>
	m.<span style="color: #006600;">a1</span> = <span style="color: #66cc66;">&#40;</span>a1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>a2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>a3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c1</span><span style="color: #66cc66;">&#41;</span>;
	m.<span style="color: #006600;">a2</span> = <span style="color: #66cc66;">&#40;</span>a1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a2</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>a2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b2</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>a3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c2</span><span style="color: #66cc66;">&#41;</span>;
	m.<span style="color: #006600;">a3</span> = <span style="color: #66cc66;">&#40;</span>a1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a3</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>a2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b3</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>a3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c3</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//second row</span>
	m.<span style="color: #006600;">b1</span> = <span style="color: #66cc66;">&#40;</span>b1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c1</span><span style="color: #66cc66;">&#41;</span>;
	m.<span style="color: #006600;">b2</span> = <span style="color: #66cc66;">&#40;</span>b1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a2</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b2</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c2</span><span style="color: #66cc66;">&#41;</span>;
	m.<span style="color: #006600;">b3</span> = <span style="color: #66cc66;">&#40;</span>b1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a3</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b3</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c3</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//third row</span>
	m.<span style="color: #006600;">c1</span> = <span style="color: #66cc66;">&#40;</span>c1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c1</span><span style="color: #66cc66;">&#41;</span>;
	m.<span style="color: #006600;">c2</span> = <span style="color: #66cc66;">&#40;</span>c1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a2</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b2</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c2</span><span style="color: #66cc66;">&#41;</span>;
	m.<span style="color: #006600;">c3</span> = <span style="color: #66cc66;">&#40;</span>c1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a3</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b3</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c3</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #0066CC;">Set</span><span style="color: #66cc66;">&#40;</span>m.<span style="color: #006600;">a1</span>,m.<span style="color: #006600;">a2</span>,m.<span style="color: #006600;">a3</span>,m.<span style="color: #006600;">b1</span>,m.<span style="color: #006600;">b2</span>,m.<span style="color: #006600;">b3</span>,m.<span style="color: #006600;">c1</span>,m.<span style="color: #006600;">c2</span>,m.<span style="color: #006600;">c3</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/**
 * This is used to do the final transformation upon the Vector
 * @param point The Vector to apply this Matrix transformation to.
 *
 */</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> transformVector<span style="color: #66cc66;">&#40;</span>point:Vector2D<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> tempX:<span style="color: #0066CC;">Number</span> = <span style="color: #66cc66;">&#40;</span>a1<span style="color: #66cc66;">*</span>point.<span style="color: #006600;">x</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b1<span style="color: #66cc66;">*</span>point.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c1<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> tempY:<span style="color: #0066CC;">Number</span> = <span style="color: #66cc66;">&#40;</span>a2<span style="color: #66cc66;">*</span>point.<span style="color: #006600;">x</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b2<span style="color: #66cc66;">*</span>point.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c2<span style="color: #66cc66;">&#41;</span>;
	point.<span style="color: #006600;">x</span> = tempX;
	point.<span style="color: #006600;">y</span> = tempY;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h2>And that&#8217;s all!</h2>
<p>So anyways&#8230;I&#8217;ve shown the internal workings of the math code there for you, though the first code block is all that should really matter to you in the end&#8230;since it shows how to actually use the math.</p>
<p>No example today, since I&#8217;m pretty sure you can imagine a turret rotating at a max speed&#8230;lol. However, it will be used in the next <a href="http://cheezeworld.com/game-structure/">Game Structure</a> post for the entities.</p>
]]></content:encoded>
			<wfw:commentRss>http://cheezeworld.com/constraining-rotation-max-turn-rate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming Tidbit #2 &#8211; Efficient Multiple Object Collision Testing</title>
		<link>http://cheezeworld.com/efficient-collisions/</link>
		<comments>http://cheezeworld.com/efficient-collisions/#comments</comments>
		<pubDate>Sun, 30 Nov 2008 22:32:28 +0000</pubDate>
		<dc:creator>Colby Cheeze</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Tidbits]]></category>
		<category><![CDATA[actionscript 3]]></category>
		<category><![CDATA[collisions]]></category>

		<guid isPermaLink="false">http://cheezeworld.com/?p=122</guid>
		<description><![CDATA[This is the second post in the series &#8220;Programming Tidbits.&#8221; These articles consist of useful bits of information or code that I would like to share, but may not be considered a full article.
Sometimes your game may consist of a very large amount of entities that must be checked for collision with each other. In [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://cheezeworld.com/wp-content/uploads/2008/11/collision.jpg"><img class="alignleft size-full wp-image-125" title="collision" src="http://cheezeworld.com/wp-content/uploads/2008/11/collision.jpg" alt="" width="400" height="281" /></a>This is the second post in the series &#8220;Programming Tidbits.&#8221; These articles consist of useful bits of information or code that I would like to share, but may not be considered a full article.</p>
<p>Sometimes your game may consist of a very large amount of entities that must be checked for collision with each other. In order to increase the speed of your collision tests there are a few methods that you can employ. This is no way an all inclusive article on this subject because there are MANY different methods for improving collision performance, however hopefully this can get you started.</p>
<p>The easiest thing to do is to eliminate as many collision checks as possible, as well as keep the collision checks as simple as you can. I&#8217;ll try to explain a few different methods which you can combine to speed up your testing.</p>
<h3>The Bad Way</h3>
<p>Generally you will be storing multiple entities in some type of array or map. The most common way of checking for collision is to loop through this array and check each entity against one another.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> entity1:Entity;
<span style="color: #000000; font-weight: bold;">var</span> entity2:Entity;
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> entities.<span style="color: #0066CC;">length</span>; i++ <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
 	entity1 = entities<span style="color: #66cc66;">&#91;</span> i <span style="color: #66cc66;">&#93;</span>;
&nbsp;
 	<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>; j <span style="color: #66cc66;">&lt;</span> entities.<span style="color: #0066CC;">length</span>; j++ <span style="color: #66cc66;">&#41;</span>
 	<span style="color: #66cc66;">&#123;</span>
 		entity2 = entities<span style="color: #66cc66;">&#91;</span> j <span style="color: #66cc66;">&#93;</span>;
&nbsp;
 		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> entity1.<span style="color: #006600;">isOverlapping</span><span style="color: #66cc66;">&#40;</span> entity2 <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
 		<span style="color: #66cc66;">&#123;</span>
 			<span style="color: #808080; font-style: italic;">// Handle Collision</span>
 		<span style="color: #66cc66;">&#125;</span>
 	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h3>Reduce Redundancy</h3>
<p>Using this method will work, however the more entities you add to the array the slower it will become. The main problem here is that many of your checks are redundant. The more efficient method would be to make sure that you only check each entity once by making a small change to the inner loop:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = i + <span style="color: #cc66cc;">1</span>; j <span style="color: #66cc66;">&lt;</span> entities.<span style="color: #0066CC;">length</span>; j++ <span style="color: #66cc66;">&#41;</span></pre></div></div>

<h3>Collision Groups</h3>
<p>Another way of increasing the speed is to give your entities flags or collision groups. For example, you may flag some entities as &#8220;collidable&#8221; and you may also decide that some entities cannot collide with each other so you might give them a &#8220;group&#8221; variable. This eliminates the need for actually running checks against this entity at all.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> entity1:Entity;
<span style="color: #000000; font-weight: bold;">var</span> entity2:Entity;
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> entities.<span style="color: #0066CC;">length</span>; i++ <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
 	entity1 = entities<span style="color: #66cc66;">&#91;</span> i <span style="color: #66cc66;">&#93;</span>;	
&nbsp;
 	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span>entity1.<span style="color: #006600;">isCollidable</span> <span style="color: #66cc66;">&#41;</span>
 	<span style="color: #66cc66;">&#123;</span>
 		<span style="color: #b1b100;">continue</span>;
 	<span style="color: #66cc66;">&#125;</span>
&nbsp;
 	<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = i + <span style="color: #cc66cc;">1</span>; j <span style="color: #66cc66;">&lt;</span> entities.<span style="color: #0066CC;">length</span>; j++ <span style="color: #66cc66;">&#41;</span>
 	<span style="color: #66cc66;">&#123;</span>
 		entity2 = entities<span style="color: #66cc66;">&#91;</span> j <span style="color: #66cc66;">&#93;</span>;
&nbsp;
 		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span>entity2.<span style="color: #006600;">isCollidable</span> <span style="color: #66cc66;">||</span> entity1.<span style="color: #006600;">group</span> == entity2.<span style="color: #006600;">group</span> <span style="color: #66cc66;">&#41;</span>
 		<span style="color: #66cc66;">&#123;</span>
 			<span style="color: #b1b100;">continue</span>;
 		<span style="color: #66cc66;">&#125;</span>
&nbsp;
 		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> entity1.<span style="color: #006600;">isOverlapping</span><span style="color: #66cc66;">&#40;</span> entity2 <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
 		<span style="color: #66cc66;">&#123;</span>
 			<span style="color: #808080; font-style: italic;">// Handle Collision</span>
 		<span style="color: #66cc66;">&#125;</span>
 	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h3>Simplify First</h3>
<p>The last trick would apply when you are using complex collision such as pixel perfect / polygon collision. Instead of checking each object with the complex collision algorithm, use a simple bounding circle collision test first, and if that test returns true then proceed with the more detailed collision test.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> entity1.<span style="color: #006600;">isOverlapping</span><span style="color: #66cc66;">&#40;</span> entity2 <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> entity1.<span style="color: #006600;">detailedCollisionTest</span><span style="color: #66cc66;">&#40;</span> entity2 <span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// Handle Collision</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h3>Final Words</h3>
<p>Other methods of increasing collision testing speed would be to implement some sort of &#8220;broad phase&#8221; algorithm, however that is beyond the scope of this article. The methods discussed here should be sufficient for most flash games.</p>
]]></content:encoded>
			<wfw:commentRss>http://cheezeworld.com/efficient-collisions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming Tidbit #1 &#8211; Eliminate Magic Numbers</title>
		<link>http://cheezeworld.com/tidbit1-magic-numbers/</link>
		<comments>http://cheezeworld.com/tidbit1-magic-numbers/#comments</comments>
		<pubDate>Wed, 12 Nov 2008 16:09:14 +0000</pubDate>
		<dc:creator>Colby Cheeze</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Tidbits]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[magic numbers]]></category>
		<category><![CDATA[tidbits]]></category>

		<guid isPermaLink="false">http://cheezeworld.com/?p=87</guid>
		<description><![CDATA[
This is the first post in the series &#8220;Programming Tidbits.&#8221; These articles consist of useful bits of information or code that I would like to share, but may not be considered a full article.
The Wrong Thing&#8230;
When programming, a lot of times you may find yourself throwing together some code which uses &#8220;Magic Numbers.&#8221; A magic [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://cheezeworld.com/wp-content/uploads/2008/11/hocus-plate-small0003_edited-5.jpg"><img class="size-full wp-image-88 alignleft" title="hocus-plate-small0003_edited-5" src="http://cheezeworld.com/wp-content/uploads/2008/11/hocus-plate-small0003_edited-5.jpg" alt="" width="237" height="239" /></a></p>
<p>This is the first post in the series &#8220;Programming Tidbits.&#8221; These articles consist of useful bits of information or code that I would like to share, but may not be considered a full article.</p>
<h3>The Wrong Thing&#8230;</h3>
<p>When programming, a lot of times you may find yourself throwing together some code which uses &#8220;Magic Numbers.&#8221; A magic number is basically just any static number sitting in your code such as:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> moveMyCharacter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	myCharacter.<span style="color: #006600;">x</span> += <span style="color: #cc66cc;">5</span>;
	myCharacter.<span style="color: #006600;">y</span> += <span style="color: #cc66cc;">5</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> damageEnemy<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	someEnemy.<span style="color: #006600;">health</span> -= <span style="color: #cc66cc;">20</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h3>Why is it bad?</h3>
<p>Now obviously that is just a simple example, but let&#8217;s say you have code ALL OVER your project using these &#8220;magic numbers.&#8221; What happens then, if later you want to change these numbers? You have a long and tedious process of finding all times that they are used. Not only that, but you are prone to missing something which will therefore lead to unpredictable code and bugs.</p>
<h3>The right thing&#8230;</h3>
<p>Instead of being mean to yourself, do the right thing and create constants or variables for all of your magic numbers. The usual place to put them is at the top of your class. Even better would be to externalize the data into some sort of settings file ( if the data is worthy of course ). I&#8217;ll be discussing externalizing data in a future article if you aren&#8217;t quite sure how to go about that&#8230;</p>
<p>So here is the code once again, done properly.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// Top of your Class somewhere...</span>
<span style="color: #0066CC;">private</span> const MOVE_SPEED:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">5</span>;
<span style="color: #0066CC;">private</span> const HERO_DAMAGE:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">20</span>;
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> moveMyCharacter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	myCharacter.<span style="color: #006600;">x</span> += MOVE_SPEED;
	myCharacter.<span style="color: #006600;">y</span> += MOVE_SPEED;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> damageEnemy<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	someEnemy.<span style="color: #006600;">health</span> -= HERO_DAMAGE;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://cheezeworld.com/tidbit1-magic-numbers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

