<?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>Enigmatic Thought &#187; game programming</title> <atom:link href="http://blog.enigmaticthought.com/tag/game-programming/feed/" rel="self" type="application/rss+xml" /><link>http://blog.enigmaticthought.com</link> <description>import com.enigmaticThought.blog;</description> <lastBuildDate>Fri, 25 Jun 2010 19:54:39 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.2.1</generator> <item><title>Clever Code is dangerous</title><link>http://blog.enigmaticthought.com/2010/03/clever-code/</link> <comments>http://blog.enigmaticthought.com/2010/03/clever-code/#comments</comments> <pubDate>Thu, 04 Mar 2010 18:48:47 +0000</pubDate> <dc:creator>Matt C</dc:creator> <category><![CDATA[Flex]]></category> <category><![CDATA[algorithms]]></category> <category><![CDATA[game programming]]></category> <category><![CDATA[programming]]></category> <guid
isPermaLink="false">http://enigmaticthought.com/?p=264</guid> <description><![CDATA[When we last left you, we talked about Big O Notation and how we can use that to compare two algorithms. That was all theoretical though, dealing with infinite sets of data. In the real world, you&#8217;ll find that sometimes a &#8220;bad&#8221; algorithm is really the best for your application. Lets track down an example [...]]]></description> <content:encoded><![CDATA[<p>When we last left you, we talked about <a
href="http://enigmaticthought.com/2009/12/big-o-notation/">Big O Notation</a> and how we can use that to compare two algorithms.  That was all theoretical though, dealing with infinite sets of data.  In the real world, you&#8217;ll find that sometimes a &#8220;bad&#8221; algorithm is really the best for your application.</p><p>Lets track down an example from the world of video games.  You&#8217;ve got a ship that flying around trying to dodge asteroids, and the ship could fire bullets to blow the asteroids up.  Let&#8217;s lay out some ground rules as well.  We don&#8217;t worry about the asteroids hitting each other, and bullets can&#8217;t hit each other or your own ship.  There&#8217;s two separate problems here, and we&#8217;ll divide the collision system between them.  In the first, we have a single ship and a one or more asteroids (when there are no asteroids, the level is over).  In the second, there are zero or more bullets (maybe there&#8217;s a limit, but I&#8217;ll assume the <a
href="http://catb.org/jargon/html/Z/Zero-One-Infinity-Rule.html">Zero-One-Infinity Rule</a>) and one or more asteroids.</p><p>On the surface these seem identical, there is a target set, and a set to hit the targets.  So let&#8217;s solve the complicated one first (bullets &#038; asteroids).  There&#8217;s a many to many relationship, so during the first pass we could compare each bullet to each asteroid and see if we register a hit.  Since we&#8217;re looping twice, the Big O notation would be O(b*a) with b = # of bullets, and 1 = # of asteroids.  We can do better than that though, thanks to some clever thinking.  Let&#8217;s sort our lists of asteroids and bullets in the order of the y (vertical) coordinate.  We know we can sort using lots of O(n log n) algorithms (<a
href="http://en.wikipedia.org/wiki/Quicksort">quicksort</a>, <a
href="http://en.wikipedia.org/wiki/Merge_sort">mergesort</a>, and whatnot) and then we can use the concept of merging to work our way down the 2 sorted lists looking for matching x coordinates.  Here&#8217;s some pseudocode:</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left"><a
href="javascript:;" onclick="javascript:showCodeTxt('p264code3'); return false;">View Code</a> ACTIONSCRIPT3</span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p2643"><td
class="code" id="p264code3"><pre class="actionscript3" style="font-family:monospace;">asteroidList<span style="color: #000066; font-weight: bold;">.</span>sortY<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
bulletsList<span style="color: #000066; font-weight: bold;">.</span>sortY<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
asteroid = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #004993;">bullet</span> = <span style="color: #000000; font-weight:bold;">0</span>
<span style="color: #0033ff; font-weight: bold;">while</span> <span style="color: #000000;">&#40;</span>bulletList<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span> <span style="color: #000066; font-weight: bold;">&lt;</span> <span style="color: #004993;">bullet</span> <span style="color: #000066; font-weight: bold;">&amp;&amp;</span> asteroidList<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span> <span style="color: #000066; font-weight: bold;">&lt;</span> asteroid<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>bulletList<span style="color: #000000;">&#91;</span><span style="color: #004993;">bullet</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">&lt;</span> asteroidList<span style="color: #000000;">&#91;</span>asteroid<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #004993;">bullet</span><span style="color: #000066; font-weight: bold;">++;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>bulletList<span style="color: #000000;">&#91;</span><span style="color: #004993;">bullet</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">&gt;</span> asteroidList<span style="color: #000000;">&#91;</span>asteroid<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		asteroid<span style="color: #000066; font-weight: bold;">++;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #009900; font-style: italic;">// y is ==</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #009900; font-style: italic;">//check for collision and do work to handle multiple objects on same y point</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div><p>Of course you&#8217;d have to build a way to handle multiple object on the same plane, but it&#8217;d work out to about O(a+b) assuming everything is evenly distributed on the y axis. Combined with the initial sorting, your Big O notation would be O(n log n) (either n=a or n=b depending on which is larger) because O(n log n) is &#8220;larger&#8221; than O(a+b), which would simplify to O(n).</p><p>So we&#8217;ve figured out the collision detection right? What about the ship and asteroids?  Since we know one value, O(s*a) is really O(1*a)=O(a) and our &#8220;clever&#8221; solution is actually slower than the original! In fact, depending on the number of bullets, we may have slowed the simple O(b*a) solution down as well.</p><p>So you&#8217;re not programming a game, so why should you care?  Throw out asteroids, ships, and bullets and look at two sets of data you&#8217;re trying to merge.  A small list of data may just need a brute force solution rather than spending time figuring out a &#8220;really nifty algorithm&#8221; which may be horribly inefficient with the amount of data your working on.  Maybe you should just have everything pre-sorted so you don&#8217;t take the O(n log n) penalty, then O(a+b) can be no worse than O(a*n).  Regardless, don&#8217;t just look at the algorithm ans what it does, but look at the data that will be pushed around when you have to decide how to accomplish a task.<br
/><h3 class='related_post_title'>Related Posts (generated):</h3><ul
class='related_post'><li><a
href='http://enigmaticthought.com/2009/12/big-o-notation/' title='Big O Notation'>Big O Notation</a></li><li><a
href='http://enigmaticthought.com/2009/12/why-algorithms-matter/' title='Why Algorithms Matter'>Why Algorithms Matter</a></li><li><a
href='http://enigmaticthought.com/2010/06/flash-debug-crashing-in-firefox/' title='Flash Debug crashing in Firefox?'>Flash Debug crashing in Firefox?</a></li><li><a
href='http://enigmaticthought.com/2009/12/bugquash-badges-fixed/' title='Bugquash badges fixed'>Bugquash badges fixed</a></li><li><a
href='http://enigmaticthought.com/2009/11/scientific-notation-patch-accepted/' title='Scientific Notation Patch Accepted'>Scientific Notation Patch Accepted</a></li></ul> ]]></content:encoded> <wfw:commentRss>http://blog.enigmaticthought.com/2010/03/clever-code/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
