<?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>Olhovsky &#187; Coding</title>
	<atom:link href="http://www.olhovsky.com/tag/coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.olhovsky.com</link>
	<description>Programming, meet art.</description>
	<lastBuildDate>Mon, 23 Jan 2012 05:19:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Dozens of point lights with forward rendering.</title>
		<link>http://www.olhovsky.com/2011/03/dozens-of-point-lights-with-forward-rendering/</link>
		<comments>http://www.olhovsky.com/2011/03/dozens-of-point-lights-with-forward-rendering/#comments</comments>
		<pubDate>Thu, 31 Mar 2011 17:10:11 +0000</pubDate>
		<dc:creator>olhovsky</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[DBP2011]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[dream build play]]></category>
		<category><![CDATA[xbox]]></category>

		<guid isPermaLink="false">http://www.olhovsky.com/?p=526</guid>
		<description><![CDATA[I&#8217;ve spent the last few days working on spatial partitioning, object pooling code (and fighting with stackoverflow about it), and other necessary performance optimization code. Most AAA titles seem to support something like 5-6 dynamic lights. I&#8217;m sure you&#8217;ve noticed &#8230; <a href="http://www.olhovsky.com/2011/03/dozens-of-point-lights-with-forward-rendering/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve spent the last few days working on spatial partitioning, object pooling code (and <a href="http://stackoverflow.com/questions/5486654/allowing-iteration-without-generating-any-garbage/5487374">fighting with stackoverflow</a> about it), and other necessary performance optimization code.</p>
<p>Most AAA titles seem to support something like 5-6 dynamic lights. I&#8217;m sure you&#8217;ve noticed by now that this game takes place mostly in the dark, and I plan on making it darker still (if I can). Well placed lighting will play a big role in making things look interesting, as well as just being able to see your surroundings.</p>
<p><a href="http://jcoluna.wordpress.com/2011/02/07/xna-4-0-light-pre-pass-shedding-the-light/">Pre-pass</a> or fully <a href="http://www.catalinzima.com/tutorials/deferred-rendering-in-xna/">deferred rendering</a> would allow for many lights with little work, but these rendering schemes don&#8217;t support MSAA or, more importantly, transparent objects. Also, deferred lighting is not a very good fit for the XBOX, due to it&#8217;s 10MB EDRAM and predicated tiling, and it&#8217;s relatively low texture bandwidth. I did spend a little bit of time playing with <a href="http://jcoluna.wordpress.com/">J. Coluna&#8217;s</a> pre-pass rendering code on the XBOX a few days ago, but the lighting quality was very poor when used with the XBOX HDRBlendable format (which is 10 bits per channel plus 2 bits left over).</p>
<p>By partitioning the space into a grid and registering objects and terrain chunks into it, I can currently draw 30 point lights at once on the xbox (at 60 FPS). The CPU speed is the limitation so far, not the graphics card.</p>
<p>Of course, that is with just the terrain. Things will get a lot slower when I start adding trees and other objects.</p>
<p>And I haven&#8217;t mentioned shadows yet.</p>
<p>In order to be able to set the game in the dark atmosphere that I&#8217;m going for, I&#8217;ll want to have at least 20 point lights, though I might be able to get away with casting shadows from only 10 or so.</p>
<div id="attachment_527" class="wp-caption aligncenter" style="width: 650px"><a href="http://www.olhovsky.com/wp/wp-content/uploads/2011/03/march31_projectiles_and_point_lights.png"><img src="http://www.olhovsky.com/wp/wp-content/uploads/2011/03/march31_projectiles_and_point_lights-1024x405.png" alt="March 31 Projectiles and Point Lights" title="march31_projectiles_and_point_lights" width="640" height="253" class="size-large wp-image-527" /></a><p class="wp-caption-text">Dynamic point lights moving across the terrain.</p></div>
<p>I&#8217;ve starting writing some basic rigid body physics. I have projectiles working that can detect collision, but do not react to collisions appropriately yet. Actually the lights in the picture above are projectiles launched from the camera&#8217;s position.</p>
<p>I&#8217;ve also built a framework for making explosions of various types, complete with flying debris.</p>
<div id="attachment_528" class="wp-caption aligncenter" style="width: 610px"><a href="http://www.olhovsky.com/wp/wp-content/uploads/2011/03/march-31-explosions.png"><img src="http://www.olhovsky.com/wp/wp-content/uploads/2011/03/march-31-explosions.png" alt="" title="march 31 explosions" width="600" height="299" class="size-full wp-image-528" /></a><p class="wp-caption-text">A couple of basic explosions with debris.</p></div>
<p>The explosion above uses a fairly flexible Explosion class that I wrote yesterday, so new explosions can easily be created with little code at this point. Most projectiles that the player can fire will need some sort of explosion, so recreating an explosion type effect will happen often. This could be used to display the destruction of a crate for example. Just replace the explosion particles with subtle smoke, and the debris with broken wood models. Since the explosion debris are projectiles themselves, they can have their own explosions, which can have their own debris, ad infinitum.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.olhovsky.com/2011/03/dozens-of-point-lights-with-forward-rendering/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Avoiding garbage collections on the compact CLR.</title>
		<link>http://www.olhovsky.com/2011/03/avoiding-garbage-collections-on-the-compact-clr/</link>
		<comments>http://www.olhovsky.com/2011/03/avoiding-garbage-collections-on-the-compact-clr/#comments</comments>
		<pubDate>Wed, 30 Mar 2011 17:24:32 +0000</pubDate>
		<dc:creator>olhovsky</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[DBP2011]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[xbox]]></category>

		<guid isPermaLink="false">http://www.olhovsky.com/?p=513</guid>
		<description><![CDATA[I haven&#8217;t posted in a few days because I&#8217;ve been working on almost purely programming related tasks, which don&#8217;t lead to pretty screenshots. I&#8217;ve been improving the efficiency of the spatial partitioning scheme, and terrain draw args sorting. Today though, &#8230; <a href="http://www.olhovsky.com/2011/03/avoiding-garbage-collections-on-the-compact-clr/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I haven&#8217;t posted in a few days because I&#8217;ve been working on almost purely programming related tasks, which don&#8217;t lead to pretty screenshots. I&#8217;ve been improving the efficiency of the spatial partitioning scheme, and terrain draw args sorting.</p>
<p>Today though, I (re)wrote an object pool class.</p>
<p>Garbage collections are expensive on the compact CLR, they occur whenever 1MB of garbage is created, and cause noticeable stutter in the game when they happen. For this reason, I simply try not to generate any garbage.</p>
<p>Why we can&#8217;t just choose to have collections occur every 16MB or something, I don&#8217;t know.</p>
<p>I&#8217;ve written custom pools in various places in this project, because creating an efficient generic pool is tricky. Have a look at <a href="http://swampthingtom.blogspot.com/2007/06/generic-pool-collection-class.html">swampthingtom&#8217;s generic object pool</a>. It uses a struct as a container for each pooled item and can only pool types that meet the <code>new()</code> constraint (public parameterless constructor, and no abstract classes allowed).</p>
<p>Having to write a pool for every type though is slowing me down, so I spent some time today thinking about a good design for a generic object pool.</p>
<p>Giving up abstract classes (which is what swampthingtom does with his pool) is going a little too far for me. Also, a significant performance cost is associated with the use of that pool, which makes it less than ideal if you are using it to allocate hundreds of objects or more.</p>
<p><a href="http://blog.gallusgames.com/programming/lean-mean-object-pool-in-c">Here&#8217;s another approach</a>, which is minimalist, and still uses the new() constraint. This pool is clearly faster, but offers no way to iterate over over the collection of active pooled objects, and again, no abstract classes or constructors with parameters allowed. It&#8217;s also basically just an unsafe version of <a href="http://www.google.ca/url?sa=t&amp;source=web&amp;cd=3&amp;ved=0CCMQFjAC&amp;url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fsystem.collections.stack.aspx&amp;ei=x7eUTeaWO9KN0QHDq5n1Cw&amp;usg=AFQjCNHuMdTjEHpoo12GMm5bl7cr6xf07A&amp;sig2=uqPYDg2YEgFDavbGqcZMmw">Stack</a> <img src='http://www.olhovsky.com/wp/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>So I wrote a pool that partitions an array into two parts, the first part for live objects, the second for dead objects. Then I effectively do what the compact CLR does with dead objects &#8212; sweep across the dead objects once in a while, and compact them into the second part of the array. As for object initialization, I pass the pool a Func delegate that does the initialization. Delegates are a little slow, but in this case they are called rarely &#8212; in fact, they should be called never, if I pick the right pool sizes. The code is barely tested so far and might benefit from another small optimization or two, but I&#8217;ll be sure to post it soonish. As usual, I&#8217;m also not fond of the idea of posting good code this far from the competition deadline.</p>
<p>By the way, <a href="http://stackoverflow.com/questions/5486654/allowing-iteration-without-generating-any-garbage/5487374">I fought with stackoverflow</a> to try to get some discussion going about ways to iterate over the pool without generating garbage. Most people did not want to hear about avoiding garbage creation. Eventually Eric Lippert came to the rescue and defended my question.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.olhovsky.com/2011/03/avoiding-garbage-collections-on-the-compact-clr/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Erratic development speed.</title>
		<link>http://www.olhovsky.com/2011/03/erratic-development-speed/</link>
		<comments>http://www.olhovsky.com/2011/03/erratic-development-speed/#comments</comments>
		<pubDate>Mon, 28 Mar 2011 00:42:21 +0000</pubDate>
		<dc:creator>olhovsky</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[DBP2011]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[dream build play]]></category>

		<guid isPermaLink="false">http://www.olhovsky.com/?p=501</guid>
		<description><![CDATA[I have a priority queue of game features that need to be completed. E.g.: [-3] Pure black rolling fog to simplify fog shader. [4] Dual paraboid point light shadow map implementation [2] Terrain diffuse lighting based lightning effect. [6] Moon &#8230; <a href="http://www.olhovsky.com/2011/03/erratic-development-speed/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I have a priority queue of game features that need to be completed.</p>
<p>E.g.:</p>
<ul>
<li>[-3] Pure black rolling fog to simplify fog shader.</li>
<li>[4] Dual paraboid point light shadow map implementation</li>
<li>[2] Terrain diffuse lighting based lightning effect.</li>
<li>[6] Moon billboard.</li>
<li>&#8230; etc.</li>
</ul>
<p>Where [x] represents x priority for the item. (A larger priority value means I should focus on that item before a lower priority valued item.)</p>
<p>I assign higher priority to items that are both easy/fast to implement and important features that the game needs. Hence the moon is high priority &#8212; it dramatically alters the mood of the scene for something that should only take maybe an hour to implement (considering that I have zero billboards in the game at the moment).</p>
<p>The more I try to estimate the time cost of implementing various features, the more I realize how hopeless it is to try to estimate the time cost of implementing a feature.</p>
<p>Creating the moon texture took an hour.</p>
<p>Creating the billboard code took an hour, including setting the moonlight from the right direction.</p>
<p>Getting the moon to look just right (that is, tweaking the texture, position, and lighting) took an hour.</p>
<p>At this point it was working on the PC.</p>
<div id="attachment_502" class="wp-caption aligncenter" style="width: 650px"><a href="http://www.olhovsky.com/wp/wp-content/uploads/2011/03/march27_moon_pc.png"><img class="size-large wp-image-502" title="Moon on PC. March 27." src="http://www.olhovsky.com/wp/wp-content/uploads/2011/03/march27_moon_pc-1024x598.png" alt="" width="640" height="373" /></a><p class="wp-caption-text">The moon! Notice the specular moonlight shining on the terrain.</p></div>
<p>Then I slept, and when I woke up, tested some other code on the XBOX only to find that the moon is not displayed properly on the XBOX!</p>
<div id="attachment_503" class="wp-caption aligncenter" style="width: 650px"><a href="http://www.olhovsky.com/wp/wp-content/uploads/2011/03/march27_xbox_moon.png"><img class="size-large wp-image-503" title="Moon on xbox (bug). March 27." src="http://www.olhovsky.com/wp/wp-content/uploads/2011/03/march27_xbox_moon-1024x576.png" alt="" width="640" height="360" /></a><p class="wp-caption-text">Obviously this is not how the moon is supposed to look.</p></div>
<p>Figuring out this bug took over an hour.</p>
<p>The fix by the way, was changing this pixel shader:</p>
<p>&nbsp;</p>
<pre class="brush:c#">float4 PixelShaderFunction(float2 texCoord : TEXCOORD0, float4 color : COLOR0) : COLOR0
{
    color = tex2D(TextureSampler, texCoord);

    // Apply the alpha test.
    clip((color.a - AlphaTestThreshold) * AlphaTestDirection);

    return color;
}</pre>
<p>To this one:</p>
<pre class="brush:c#">float4 PixelShaderFunction(float2 texCoord : TEXCOORD0) : COLOR0
{
    float4 color = tex2D(TextureSampler, texCoord);

    // Apply the alpha test.
    clip((color.a - AlphaTestThreshold) * AlphaTestDirection);

    return color;
}</pre>
<p>All in all, creating the moon took over 4 hours &#8212; about 4 times longer than I thought.</p>
<p>Days like this make me nervous &#8212; what if creating the whole game takes 4 times longer than I thought? After all, first place <a href="http://www.dreambuildplay.com">DBP</a> winners likely spend years, not months, (and certainly not just 3 months) on their submissions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.olhovsky.com/2011/03/erratic-development-speed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sky effect and bloom filter.</title>
		<link>http://www.olhovsky.com/2011/03/sky-effect-and-bloom-filter/</link>
		<comments>http://www.olhovsky.com/2011/03/sky-effect-and-bloom-filter/#comments</comments>
		<pubDate>Sat, 26 Mar 2011 07:55:58 +0000</pubDate>
		<dc:creator>olhovsky</dc:creator>
				<category><![CDATA[DBP2011]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[dream build play]]></category>

		<guid isPermaLink="false">http://www.olhovsky.com/?p=497</guid>
		<description><![CDATA[I&#8217;ve added a bloom filter, which every big title seems to use these days. The bloom filter takes the brightest parts of the image, blurs them, and mixes the result back into the scene. This makes lights glow, softens bright &#8230; <a href="http://www.olhovsky.com/2011/03/sky-effect-and-bloom-filter/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve added a bloom filter, which every big title seems to use these days.</p>
<p>The bloom filter takes the brightest parts of the image, blurs them, and mixes the result back into the scene. This makes lights glow, softens bright edges, and generally makes things look more natural.</p>
<p>Since there&#8217;s very little light you can&#8217;t notice the bloom so much, although it&#8217;s very obvious on the bottom center bar indicator. The rest of the HUD does not have the bloom applied to it.</p>
<p><a href="http://www.olhovsky.com/wp/wp-content/uploads/2011/03/march26_bloom_and_aurora_sky.png"><img class="aligncenter size-large wp-image-498" title="March 26 bloom and Aurora Borealis sky effect." src="http://www.olhovsky.com/wp/wp-content/uploads/2011/03/march26_bloom_and_aurora_sky-1024x598.png" alt="" width="640" height="373" /></a>The blue sky effect resembles an <a href="http://en.wikipedia.org/wiki/Aurora_(astronomy)">aurora</a> somewhat, and is animated. It&#8217;s obviously more interesting to see in motion than in this still picture.</p>
<p>Compare to this aurora below (courtesy wikipedia).</p>
<p style="text-align: center;">&nbsp;</p>
<div class="wp-caption aligncenter" style="width: 609px"><img class="     " title="Aurora Australis" src="http://upload.wikimedia.org/wikipedia/commons/thumb/6/63/Aurora_australis_panorama.jpg/800px-Aurora_australis_panorama.jpg" alt="" width="599" height="142" /><p class="wp-caption-text">Aurora Australis</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.olhovsky.com/2011/03/sky-effect-and-bloom-filter/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The importance of assertion statements.</title>
		<link>http://www.olhovsky.com/2011/03/the-importance-of-assertion-statements/</link>
		<comments>http://www.olhovsky.com/2011/03/the-importance-of-assertion-statements/#comments</comments>
		<pubDate>Wed, 23 Mar 2011 14:51:08 +0000</pubDate>
		<dc:creator>olhovsky</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[DBP2011]]></category>
		<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://www.olhovsky.com/?p=457</guid>
		<description><![CDATA[When implementing a complex system, where there are many variables/objects, I can&#8217;t stress enough how important it is to have some sort of assertion tests in your code. In C# we can write assertion tests that are only run when &#8230; <a href="http://www.olhovsky.com/2011/03/the-importance-of-assertion-statements/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When implementing a complex system, where there are many variables/objects, I can&#8217;t stress enough how important it is to have some sort of assertion tests in your code.<br />
In C# we can write assertion tests that are only run when compiled in Debug mode like so:</p>
<pre class="brush:c#">public void ReturnPointLight(PointLight3D light)
{
#if DEBUG
    // Assertion tests.

    Debug.Assert(light == _pointLightPool[light.PoolIndex]);
    Debug.Assert(_pointLightAvailable[light.PoolIndex] == false);

    // Make sure that light is not already in available stack.
    foreach (int index in _pointLightAvailableIndices)
    {
        Debug.Assert(index != light.PoolIndex);
    }

    _pointLightAvailable[light.PoolIndex] = true;
#endif
    _pointLightAvailableIndices.Push(light.PoolIndex);
}</pre>
<p>That is an excerpt from my LightingManager class, which has a PointLight pool built in.</p>
<p>That function returns a point light to the pool when it is no longer needed. This is roughly the equivalent of doing free(myPointLightInstance) in C or C++ when the instance is no longer being used. Sadly, the compact CLR that the XBOX uses has a terrible garbage collection implementation, so using object pools and allocating and freeing objects from the pools manually is a necessity.<br />
As you can see in this function, several assertions are done.</p>
<ul>
<li>Assert that the light instances&#8217; index matches the index of the internal pool.</li>
<li>Assert that the light is actually not available (because the function is meant to make unavailable lights available).</li>
<li>Assert that the light is not already stated as being available in the availability stack.</li>
</ul>
<p>These checks are a bit slow, so I run them only when the DEBUG flag is set.<br />
<strong>Edit:</strong> Using the DEBUG conditional is not necessary if you only write Debug.Assert() since Debug methods are only called if the DEBUG flag is set automatically, but are useful if you&#8217;re writing other code, as I have above.<br />
And actually, the bool[] _pointLightAvailable array is only used for debugging/testing. Setting up some decent sanity checks light this throughout my code makes development much faster in the long run, because I can be confident that things are doing what they are supposed to.</p>
<p>With the lighting manager in particular, it would be easy for the manager to do something unwanted, and cause the lights to LOOK wrong, although the program will not crash. In this type of case, it&#8217;s hard to know what&#8217;s causing the problem without lengthy code tracing/debugging if you don&#8217;t have some pre/post condition assertions in the code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.olhovsky.com/2011/03/the-importance-of-assertion-statements/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>The CPU vs GPU XBOX360 speed disparity.</title>
		<link>http://www.olhovsky.com/2011/03/the-cpu-vs-gpu-xbox360-speed-disparity/</link>
		<comments>http://www.olhovsky.com/2011/03/the-cpu-vs-gpu-xbox360-speed-disparity/#comments</comments>
		<pubDate>Sun, 20 Mar 2011 00:19:42 +0000</pubDate>
		<dc:creator>olhovsky</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[DBP2011]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[xbox]]></category>

		<guid isPermaLink="false">http://www.olhovsky.com/?p=430</guid>
		<description><![CDATA[Today and yesterday I mostly worked on class work. Actually I mostly worked on a single assignment :/ However, I did find time to write the terrain chunk drawing loop, which implicitly builds a quadtree and tests at each recursive &#8230; <a href="http://www.olhovsky.com/2011/03/the-cpu-vs-gpu-xbox360-speed-disparity/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Today and yesterday I mostly worked on class work. Actually I mostly worked on a single assignment :/</p>
<p>However, I did find time to write the terrain chunk drawing loop, which implicitly builds a quadtree and tests at each recursive level the nodes of the tree against the camera frustum.</p>
<p>I discovered quickly that testing whether every single chunk is in the camera frustum or not is <em>very</em> expensive &#8212; 8 ms, which is half the time I have to do all draw thread computation at 60 fps.</p>
<p>In my <a title="Terrain started, and quadtree vs. block based LOD." href="http://www.olhovsky.com/2011/03/terrain-started/">previous terrain</a>, where I used a quadtree for each chunk, the number of tests to see if a chunk was in the frustum was bounded, since the quadtree didn&#8217;t need to recurse all of the way, if the entire terrain was visible, since further blocks would simply be drawn larger.</p>
<p>In the new chunk LOD method, in the worst case, the entire terrain is visible and all blocks need to be drawn and tested, which takes 8 milliseconds to check, using 64&#215;64 chunks and a 1024&#215;1024 terrain heightmap.</p>
<p>On the XBOX (using only 1 core), using XNA&#8217;s BoundingSphere and BoundingFrustum classes, I can do about 40,000 sphere/frustum tests per second (600-700 per frame).</p>
<p>The GPU can test whether a triangle is in the frustum on the order of 100,000,000 times per second (over 1 million per frame).</p>
<p>The terrain chunks I&#8217;m using are 64&#215;64 = 4096 triangles each. It is actually faster to send thousands of triangles to the GPU than to perform a single intersection test on the CPU. Unless of course, you&#8217;re GPU bound and have some CPU cycles to spare.</p>
<p>Exactly how my situation will turn out is to be determined, but I thought I&#8217;d emphasize just how large the processing speed difference is for this type of operation, between the CPU and the GPU on the XBOX with XNA.</p>
<p>In an effort to minimize the chunk drawing loop CPU time, I&#8217;ve also built a ReusableMinHeap class, which is a garbage-free min heap, which sorts the chunk draw calls for me from closest to furthest &#8212; to minimize overdraw. So far, it performs roughly the same as C#&#8217;s <a href="http://msdn.microsoft.com/en-us/library/8k6e334t.aspx">Array.Sort</a>, which uses quick sort.</p>
<p>I&#8217;ll be happy to share the min heap class when the competition is over. Someone remind me.</p>
<p>By the way, here&#8217;s what the chunk-LOD terrain looks like so far:</p>
<div id="attachment_440" class="wp-caption aligncenter" style="width: 650px"><a href="http://www.olhovsky.com/wp/wp-content/uploads/2011/03/march19_chunk_lod_terrain.png"><img class="size-large wp-image-440" title="Chunk LOD Terrain March 19" src="http://www.olhovsky.com/wp/wp-content/uploads/2011/03/march19_chunk_lod_terrain-1024x598.png" alt="" width="640" height="373" /></a><p class="wp-caption-text">The current state of the terrain as of March 19, 2011. Using 64x64 chunks and a 1024x1024 heightmap.</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.olhovsky.com/2011/03/the-cpu-vs-gpu-xbox360-speed-disparity/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Path finding: What would Kirk do?</title>
		<link>http://www.olhovsky.com/2011/03/pathfinding-what-would-kirk-do/</link>
		<comments>http://www.olhovsky.com/2011/03/pathfinding-what-would-kirk-do/#comments</comments>
		<pubDate>Wed, 16 Mar 2011 14:27:33 +0000</pubDate>
		<dc:creator>olhovsky</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[DBP2011]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[dream build play]]></category>

		<guid isPermaLink="false">http://www.olhovsky.com/?p=373</guid>
		<description><![CDATA[In my previous post about the challenge of path finding on the XBOX with XNA, my A* algorithm benchmarks were abysmally slow. Recall from my previous post that pathfinding was done in 3D, including diagonal movement, allowing 26 possible directions &#8230; <a href="http://www.olhovsky.com/2011/03/pathfinding-what-would-kirk-do/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In my previous post about the challenge of path finding on the XBOX with XNA, my A* algorithm benchmarks were abysmally slow.</p>
<p>Recall from my <a title="The Challenge of Efficient Path Finding on the XBOX360" href="http://www.olhovsky.com/2011/03/xbox-path-finding-2/">previous post</a> that pathfinding was done in 3D, including diagonal movement, allowing 26 possible directions of movement at each cell in the grid. To reduce this, we move to 2D pathfinding, since the 3D grid was very thin anyway, and introduce weights at each grid cell to account for almost-3D objects on the grid.</p>
<p>Allowing diagonals permits 8 movement directions. It also allows actors to circumvent obstacles by moving through a corner, when adjacent cells touching at their corners should block it.</p>
<div id="attachment_380" class="wp-caption aligncenter" style="width: 160px"><a href="http://www.olhovsky.com/wp/wp-content/uploads/2011/03/pathfinding_pass_through.png"><img class="size-full wp-image-380" title="Diagonal pass through object path finding." src="http://www.olhovsky.com/wp/wp-content/uploads/2011/03/pathfinding_pass_through.png" alt="" width="150" height="150" /></a><p class="wp-caption-text">With diagonals, if we aren&#39;t careful, objects can pass through two objects that meet at their corners.</p></div>
<p>To increase speed and avoid having to check for adjacent obstacles before moving through corners, we can use only 4 directions of movement. The problem with this is that monsters following a path defined by this movement look robotic:</p>
<div id="attachment_274" class="wp-caption aligncenter" style="width: 260px"><a href="http://www.olhovsky.com/wp/wp-content/uploads/2011/03/4way.png"><img class="size-full wp-image-274" title="2D path finding without diagonal movement." src="http://www.olhovsky.com/wp/wp-content/uploads/2011/03/4way.png" alt="" width="250" height="250" /></a><p class="wp-caption-text">2D path finding without diagonals. A monster following this path in the naive way would turn down, right, down, right,... making it seem robotic.</p></div>
<p>&nbsp;</p>
<p>One solution I proposed to this was to use minimal acceleration hermite curves to generate smooth paths between grid cell centers.</p>
<p>To find any alternatives, I asked myself, what would Captain Kirk do?</p>
<p>Then I remembered this: <a href="http://kirkfails.ytmnd.com/">http://kirkfails.ytmnd.com/</a></p>
<p>While I might still need to generate these curves, in the meantime I have decided to take a different approach:</p>
<div id="attachment_378" class="wp-caption aligncenter" style="width: 260px"><a href="http://www.olhovsky.com/wp/wp-content/uploads/2011/03/hex_path_finding_work.png"><img class="size-full wp-image-378" title="Path finding in a hexagonal coordinate system." src="http://www.olhovsky.com/wp/wp-content/uploads/2011/03/hex_path_finding_work.png" alt="" width="250" height="264" /></a><p class="wp-caption-text">Path finding in a hexagonal coordinate system.</p></div>
<p>Using a hexagonal coordinate system will naturally produce smoother paths.</p>
<p style="text-align: center;"><a href="http://www.olhovsky.com/wp/wp-content/uploads/2011/03/kirk_vs_spock_test_conditions.png"><img class="aligncenter" title="Kirk Vs. Spock Test Conditions" src="http://www.olhovsky.com/wp/wp-content/uploads/2011/03/kirk_vs_spock_test_conditions.png" alt="" width="500" height="286" /></a></p>
<p>In the first diagram, the end position is 10 units across and 10 units down relative to the start position. In the hexagonal version, the end position is still 10 units across, and is 9.5 units down.</p>
<p>Notice that we can&#8217;t create a perfect analog of the end position, or of the obstacle locations. In this particular example, the chosen path in the hexagonal version took 12 steps, as opposed to 17 in the square coordinate system.</p>
<p>This was no coincidence. A circle inscribed in a square occupies 79% of its area. A circle inscribed in a hexagon occupies 91% of its area. So on average we expect to represent an equivalent square grid with a hexagonal grid using 11% fewer cells.</p>
<p>However, since the hexagonal version requires checking 6 neighbor cells at each step, a minimum of 102 cells were examined in the hexagonal path, as opposed to a minimum of 72 cell examinations (at 4 examinations per cell) in the square version.</p>
<p><a href="http://www.amazon.com/gp/product/1584505273/ref=as_li_ss_tl?ie=UTF8&amp;tag=progmeetart-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1584505273">Game Programming Gems 7</a><img style="border: none !important; margin: 0px !important;" src="http://www.assoc-amazon.com/e/ir?t=&amp;l=as2&amp;o=1&amp;a=1584505273" border="0" alt="" width="1" height="1" /> chapter 1.5 &#8220;For Bees and Gamers: How to Handle Hexagonal Tiles&#8221; makes the claim that you gain performance by using hexagonal coordinates, because fewer cells are needed to represent the same data as rectangular cells. However, other constructs are rectangular, and need to be mapped to hexagonal coordinates. For example RAM is rectangular. The terrain quadtree subdivides into squares, and the world octree or GGR grid is square. Mapping between rectangular and hexagonal coordinate systems has an associated time cost, and having to check 6 directions instead of 4 in the path finding is going to cost a little bit more. Of course, it&#8217;s likely that hexagonal pathfinding is going to be faster than 8 directional rectangular pathfinding.</p>
<p>In the end I expect performance to be similar, though we gain the benefit of more natural looking paths, since hexagons have the same distance between the center and any adjacent cell (as opposed to squares, where you must travel 41% further to a diagonally placed cell).</p>
<p>One of the themes in this game is hexagonal tiling. I use them in <a title="Menu system started." href="http://www.olhovsky.com/2011/03/menu-system-started/">the menu</a>, and the skill tree (yet to be implemented). You&#8217;ll even find hexagonal tiling behind the main character of the game in the <a href="http://www.olhovsky.com/wp/wp-content/uploads/2011/03/dbp2011title5.png">title banner</a> of the website right now (look at the red background closely). That said, using hexagonal objects in the game world is something I&#8217;ve been considering for a while now. If used, hexagonal path finding would make a lot of sense.</p>
<p>Hexagons tend to represent organic shapes well (like islands and rocks), and squares tend to represent manmade shapes better (like city buildings or the rooms of a house).</p>
<p>Since most of the objects in this game will be organic in nature, hexagons will work out well. If you are building a post-apocalyptic zombie game that takes place in New York, then maybe not so much (streets and buildings are rectangular).</p>
<p>So in the next couple of days, I&#8217;ll be extending my hexagonal coordinate class (already used for the hexagonal menu system), and then modifying the path finding to use it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.olhovsky.com/2011/03/pathfinding-what-would-kirk-do/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

