<?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>Roy Triesscheijn’s Weblog &#187; 3D</title>
	<atom:link href="http://roy-t.nl/index.php/tag/3d/feed/" rel="self" type="application/rss+xml" />
	<link>http://roy-t.nl</link>
	<description>My programming world</description>
	<lastBuildDate>Wed, 07 Dec 2011 23:19:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>XNA stereoscopic 3D using anyglyphs and red/cyan 3D-glasses</title>
		<link>http://roy-t.nl/index.php/2011/05/08/xna-stereoscopic-3d-using-anyglyphs-and-redcyan-3d-glasses/</link>
		<comments>http://roy-t.nl/index.php/2011/05/08/xna-stereoscopic-3d-using-anyglyphs-and-redcyan-3d-glasses/#comments</comments>
		<pubDate>Sun, 08 May 2011 11:44:31 +0000</pubDate>
		<dc:creator>Roy Triesscheijn</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[General Coding]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[Anaglyph]]></category>
		<category><![CDATA[Easy]]></category>
		<category><![CDATA[Glasses]]></category>
		<category><![CDATA[XNA4]]></category>

		<guid isPermaLink="false">http://roy-t.nl/?p=488</guid>
		<description><![CDATA[Anaglyphs are images designed to give a 3D effect when viewed with special glasses, usually with red and blue (sometimes red and cyan) filters over the left and right eyes, respectively.[1] Using anaglyphs we can make our game look real 3D through those cheap red/cyan 3D-glasses. Adding an anaglyph effect to your XNA game is [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://roy-t.nl/index.php/2011/05/08/xna-stereoscopic-3d-using-anyglyphs-and-redcyan-3d-glasses/' addthis:title='XNA stereoscopic 3D using anyglyphs and red/cyan 3D-glasses' ><a class="addthis_button_twitter"></a><a class="addthis_button_stumbleupon"></a><a class="addthis_button_reddit"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p><em></p>
<blockquote><p>Anaglyphs are images designed to give a 3D effect when viewed with special glasses, usually with red and blue (sometimes red and cyan) filters over the left and right eyes, respectively.<a href="http://hypnagogic.net/anaglyphs/">[1]</a></p></blockquote>
<p></em><br />
Using anaglyphs we can make our game look real 3D through those cheap red/cyan 3D-glasses. Adding an anaglyph effect to your XNA game is fairly easy.</p>
<p>Basically we need to undertake the following steps:<br />
-Draw our scene twice to separate render targets, with a slightly offset camera<br />
-Use a shader and draw a full screen quad to blend the images and color coding the images for each eye</p>
<p>Easy right!</p>
<p>Lets start by assuming you have the following draw code:</p>
<pre class="brush: csharp; title: ; notranslate">
protected override void Draw(GameTime gameTime)
{
    //a lot of draw calls
    base.Draw(gameTime);
}
</pre>
<p>We need to extract all your drawing code (except base.Draw(..)) to a new method that as argument accepts a view matrix. Update your code so that the passed viewMatrix is used instead of your normal camera&#8217;s viewMatrix. Doing this will allow us to easily draw the scene twice with a slightly offset camera. You should now have something like this:</p>
<pre class="brush: csharp; title: ; notranslate">
private void DrawForEye(Matrix viewMatrix)
{
    //...
}
</pre>
<p>Now let&#8217;s first write the shader that is going to blend the images, create a new effect in your content project and paste in the following code:</p>
<pre class="brush: csharp; title: ; notranslate">
texture left;
sampler sLeft = sampler_state
{
	texture = ;
	magfilter = POINT;
	minfilter = POINT;
	mipfilter = POINT;
	AddressU  = CLAMP;
	AddressV  = CLAMP;
};

texture right;
sampler sRight = sampler_state
{
	texture = ;
	magfilter = POINT;
	minfilter = POINT;
	mipfilter = POINT;
	AddressU  = CLAMP;
	AddressV  = CLAMP;
};

struct VS_INPUT
{
	float3 Pos : POSITION;
	float2 Tex : TEXCOORD0;
};

struct VS_OUTPUT
{
	float4 Pos : POSITION;
	float2 Tex : TEXCOORD0;
};

void VtAnaglyph(VS_INPUT In, out VS_OUTPUT Out)
{
	Out.Pos = float4(In.Pos,1);
	Out.Tex = In.Tex;
}

float4 PxAnaglyph(VS_OUTPUT In) : COLOR0
{
    float4 colorLeft = tex2D(sLeft, In.Tex.xy);
	float4 colorRight = tex2D(sRight, In.Tex.xy);
    return float4(colorRight.r, colorLeft.g, colorLeft.b, max(colorLeft.a, colorRight.a));
}

technique Anaglyphs
{
    pass p0
    {
        VertexShader = compile vs_2_0 VtAnaglyph();
        PixelShader = compile ps_2_0 PxAnaglyph();
    }

}
</pre>
<p>This is a pretty standard HLSL shader, but I will quickly go over it.</p>
<p>The texture&#8217;s left and right will be the textures resulting from drawing the scene twice, slightly offset from each other. We use a sampler with POINT filters because the left and right textures are going to be exactly the same size as our final rendering.</p>
<p>The vertex shader is passed as input nothing more than the position and texture coordinate of the vertex, it doesn&#8217;t transform anything but it just directly passes to the pixel shader.</p>
<p>The pixel shader samples the textures for the left and right eye. The red channel is used to &#8216;encode&#8217; the image for the right eye (the red is unfiltered by the cyan colored lens). The green and blue channel are taken from the image for the left eye (they are unfiltered by the red colored lens). You can look at <a href="http://en.wikipedia.org/wiki/Anaglyphs#Possible_color_schemes">this wikipedia entry</a> for other color combinations in case you have different 3D-glasses.</p>
<p>Now that we have our effect we need to add 2 render targets, the effect and a quad renderer to our game class. (The Quad class is posted as a code snippet <a href="http://roy-t.nl/index.php/2011/05/07/codesnippet-quad-renderer/">here</a> and used as to render the final image).</p>
<p>Added these lines to the top of your game class.</p>
<pre class="brush: csharp; title: ; notranslate">
RenderTarget2D leftEye;
RenderTarget2D rightEye;
Effect anaglyphEffect;
QuadRenderer quad;

//you can change this later to test different distances between the left and right eye viewpoint,
//the offset depends on the scale of your game, but small values seem to work best.
//I used 0.05 for a scene about 5x5x5 size.
float ammount = 0.05f;
</pre>
<p>And add these lines in your LoadContent method:</p>
<pre class="brush: csharp; title: ; notranslate">
leftEye = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
rightEye = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
anaglyphEffect = Content.Load(&quot;Anaglyphs&quot;);
quad = new QuadRenderer(GraphicsDevice);
</pre>
<p>Now we need to calculate two slightly offset view-matrices, draw the scene two times using these view-matrices and then combine them using our effect. To accomplish this write the following Draw method:</p>
<pre class="brush: csharp; title: ; notranslate">
protected override void Draw(GameTime gameTime)
{
    Matrix viewMatrix = camera.ViewMatrix; //use your own camere class here

    //The vector pointing to the right (1,0,0) as seen from the view matrix is stored
    //in the view matrix as (M11, M21, M31)
    Vector3 right = new Vector3(viewMatrix.M11, viewMatrix.M21, viewMatrix.M31) * amount; //ofset from the center for each eye
    Matrix viewMatrixLeft = Matrix.CreateLookAt(camera.Position - right, camera.LookAt, Vector3.Up);
    Matrix viewMatrixRight = Matrix.CreateLookAt(camera.Position + right, camera.LookAt, Vector3.Up);

    GraphicsDevice.SetRenderTarget(leftEye);
    DrawForEye(viewMatrixLeft, camera.ProjectionMatrix);

    GraphicsDevice.SetRenderTarget(rightEye);
    DrawForEye(viewMatrixRight, camera.ProjectionMatrix);

    GraphicsDevice.SetRenderTarget(null);

    anaglyphEffect.Techniques[&quot;Anaglyphs&quot;].Passes[0].Apply();
    anaglyphEffect.Parameters[&quot;left&quot;].SetValue(leftEye);
    anaglyphEffect.Parameters[&quot;right&quot;].SetValue(rightEye);
    quad.DrawFullScreenQuad();

    base.Draw(gameTime);
}
</pre>
<p>And hooray! We now have anaglyphs in our game! This will result in some pretty picture (the following picture of course only make sense when you use red/cyan 3D-glasses)</p>
<div id="attachment_489" class="wp-caption aligncenter" style="width: 292px"><a href="http://roy-t.nl/wp-content/uploads/2011/05/Example2.png"><img class="size-medium wp-image-489" title="Anaglyph Example" src="http://roy-t.nl/wp-content/uploads/2011/05/Example2-282x300.png" alt="" width="282" height="300" /></a><p class="wp-caption-text">Anaglyph Example, view with red/cyan glasses</p></div>
<p><strong>When you have created some cool anaglyph images in XNA, be sure to send them in, I&#8217;ll make a small gallery here!</strong></p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://roy-t.nl/index.php/2011/05/08/xna-stereoscopic-3d-using-anyglyphs-and-redcyan-3d-glasses/' addthis:title='XNA stereoscopic 3D using anyglyphs and red/cyan 3D-glasses' ><a class="addthis_button_twitter"></a><a class="addthis_button_stumbleupon"></a><a class="addthis_button_reddit"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://roy-t.nl/index.php/2011/05/08/xna-stereoscopic-3d-using-anyglyphs-and-redcyan-3d-glasses/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Collision avoidance, path finding and smooth following, 3D snippet (C#/XNA)</title>
		<link>http://roy-t.nl/index.php/2010/01/13/collision-avoidance-path-finding-and-smooth-following-3d-snippet-cxna/</link>
		<comments>http://roy-t.nl/index.php/2010/01/13/collision-avoidance-path-finding-and-smooth-following-3d-snippet-cxna/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 17:59:16 +0000</pubDate>
		<dc:creator>Roy Triesscheijn</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[General Gamedesign]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[A*]]></category>
		<category><![CDATA[collision avoidance]]></category>
		<category><![CDATA[flocking]]></category>
		<category><![CDATA[Path finding]]></category>
		<category><![CDATA[smooth following]]></category>
		<category><![CDATA[steering behavior]]></category>
		<category><![CDATA[swarming]]></category>

		<guid isPermaLink="false">http://roy-t.nl/?p=257</guid>
		<description><![CDATA[Today I converted my &#8220;Swarm Like collision avoidance, path finding and smooth following&#8221;- project (what a mouth full) to work with 3D coordinates, if you understood the code, you would know this wasn&#8217;t much of a problem. Anyway I did it for you today . (original article here).<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://roy-t.nl/index.php/2010/01/13/collision-avoidance-path-finding-and-smooth-following-3d-snippet-cxna/' addthis:title='Collision avoidance, path finding and smooth following, 3D snippet (C#/XNA)' ><a class="addthis_button_twitter"></a><a class="addthis_button_stumbleupon"></a><a class="addthis_button_reddit"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Today I converted my <em>&#8220;Swarm Like collision avoidance, path finding and smooth following&#8221;</em>- project (what a mouth full) to work with 3D coordinates, if you understood the code, you would know this wasn&#8217;t much of a problem. Anyway I did it for you today <img src='http://roy-t.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . (original article <a href="http://roy-t.nl/index.php/2009/12/29/swarm-like-collision-avoidance-path-finding-and-smooth-following/">here</a>).</p>
<pre class="brush: csharp; title: ; notranslate">
public static void Attract(GameTime gameTime, Object3D ship, Object3D[] obstacles, Object3D target)
        {
            float pullDistance = Vector3.Distance(target.Position, ship.Position);

            //Only do something if we are not already there
            if (pullDistance &gt; ((ship.Radius + target.Radius) * 1.5f))
            {
                Vector3 pull = (target.Position - ship.Position) * (1 / pullDistance); //the target tries to 'pull us in'
                Vector3 totalPush = Vector3.Zero;

                int contenders = 0;
                for (int i = 0; i &lt; obstacles.Length; ++i)
                {

                    //draw a vector from the obstacle to the ship, that 'pushes the ship away'
                    Vector3 push = ship.Position - obstacles[i].Position;

                    //calculate how much we are pushed away from this obstacle, the closer, the more push
                    float distance = (Vector3.Distance(ship.Position, obstacles[i].Position) - obstacles[i].Radius) - ship.Radius;
                    //only use push force if this object is close enough such that an effect is needed
                    if (distance &lt; ship.Radius * 3)
                    {
                        ++contenders; //count that this object is actively pushing

                        if (distance &lt; 0.0001f) //prevent division by zero errors and extreme pushes
                        {
                            distance = 0.0001f;
                        }
                        float weight = 1 / distance;
                        totalPush += push * weight;
                    }
                }

                pull *= Math.Max(1, 4 * contenders); //4 * contenders gives the pull enough force to pull stuff trough (tweak this setting for your game!)
                pull += totalPush;

                //Normalize the vector so that we get a vector that points in a certain direction, which we van multiply by our desired speed
                pull.Normalize();
                //Set the ships new position;
                ship.Position += (pull * ship.Speed) * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
        }
</pre>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://roy-t.nl/index.php/2010/01/13/collision-avoidance-path-finding-and-smooth-following-3d-snippet-cxna/' addthis:title='Collision avoidance, path finding and smooth following, 3D snippet (C#/XNA)' ><a class="addthis_button_twitter"></a><a class="addthis_button_stumbleupon"></a><a class="addthis_button_reddit"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://roy-t.nl/index.php/2010/01/13/collision-avoidance-path-finding-and-smooth-following-3d-snippet-cxna/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>New Version A* pathfinding in 3D</title>
		<link>http://roy-t.nl/index.php/2009/07/07/new-version-a-pathfinding-in-3d/</link>
		<comments>http://roy-t.nl/index.php/2009/07/07/new-version-a-pathfinding-in-3d/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 15:01:53 +0000</pubDate>
		<dc:creator>Roy Triesscheijn</dc:creator>
				<category><![CDATA[General Gamedesign]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[A Star]]></category>
		<category><![CDATA[A*]]></category>
		<category><![CDATA[A* 3D]]></category>
		<category><![CDATA[Binary Heap]]></category>
		<category><![CDATA[Breadcrumbs]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[fast]]></category>
		<category><![CDATA[Navigation]]></category>
		<category><![CDATA[Pathfinding]]></category>

		<guid isPermaLink="false">http://royalexander.wordpress.com/?p=181</guid>
		<description><![CDATA[Intro and thanks. Ok so let&#8217;s quickly forget the debacle around the first release of this code sample, I wan&#8217;t to delete the entire post about that one but I would&#8217;nt  have know that I made an error (until much later on) if it wasn&#8217;t for some people spotting it instantly! So I would first [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://roy-t.nl/index.php/2009/07/07/new-version-a-pathfinding-in-3d/' addthis:title='New Version A* pathfinding in 3D' ><a class="addthis_button_twitter"></a><a class="addthis_button_stumbleupon"></a><a class="addthis_button_reddit"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<h3>Intro and thanks.</h3>
<p>Ok so let&#8217;s quickly forget the debacle around the first release of this code sample, I wan&#8217;t to delete the entire post about that one but I would&#8217;nt  have know that I made an error (until much later on) if it wasn&#8217;t for some people spotting it instantly! So I would first like to thank some of my visitors who spotted the errors:</p>
<p>-Oscar for spotting possible performance problems.</p>
<p>-Ryan for suggesting possible HashSet&#8217;s (altough I didn&#8217;t use them in the end, see below why).</p>
<p>I would also like to thank posters from www.tweakers.net  who helped me get this version of A* so fast, especially (on post order):</p>
<p>-Phyxion &amp; Mammon for their one line version of Equals()</p>
<p>-Nick The Heazk for spotting that I totally forgot the Heuristic part for scoring (doh, I swear I had it in my draft)</p>
<p>-pedorus &amp;  jip_86 for suggesting to use a Binary(Min)Heap</p>
<p>-Marcj &amp; NotPingu for pointing out a 3D version of Pythagoras</p>
<p>-Soultaker for letting me think more about efficiency of containers (O(?) of add, extract and contains))</p>
<p>-.oisyn &amp; pedorus for suggesting to combine a binary heap and a HashSet (altough I didn&#8217;t use it in the end, see why below)</p>
<p>-PrisonerOfPain for suggesting to use binary flags instead of extra HashSet&#8217;s (that&#8217;s why I didn&#8217;t use them).</p>
<h3>Why is this version faster?</h3>
<p>A lot was changed from the previous version, let&#8217;s start with the openList, checking if an item is in the openList costs O(n) iterations and checking which item has the lowest score costs O(n) and removing an item costs O(log(n)). So I&#8217;ve replaced the openList with a BinaryHeap for checking the lowest score and removing/adding. These operations cost O(1), O(log(n)). That&#8217;s allot faster already.</p>
<p>As for checking if an item is in an openList (or closedList) all I did was add a boolean flag to the BreadCrumb class (onOpenList and onClosedList).  Checking this flagg is O(1) so that really makes checks allot faster.</p>
<p>Also all I needed the closedList for was checking if items where already in there, so with the boolean flag I could completely remove the closedList.</p>
<p>Another new feature is that we immediatly return from the FindPath function when we find the finish, this also saves some operations.</p>
<p>I also made sure that I don&#8217;t  create to much new objects but re-use them after they where first indexed (this was  also the cause for a small &#8216;score&#8217; bug) and I&#8217;ve re-aranged some ifs.</p>
<p>For the algorithm itself I now use DistanceSquared for the (forgotten) Heuristic part of the scoring which is allot better than ManHattan distance which is slightly biased. I&#8217;ve also changed the cost (G) to move from the current node to the next node to incorporate these squared cost  (so instead of 1,  1.4 and 1.7 I can now use 1,2,3 for these scores. I also don&#8217;t have to multiply all these numbers by 10 since 1, 2 and 3 are integers.</p>
<p>A final additon is the class Point3D which allows us to use only Integers instead of floats (which are slower for a cpu).</p>
<p>All in all this made this code about 200x faster (yes really!). But that is not completely fair since the first code was broken. If you count from after I fixed the heuristic part of the code the code is &#8216;only&#8217; about 30x faster.</p>
<h3>Benchmark results.</h3>
<p>And before we get to the exciting part (the code!).  Let&#8217;s first show some benchmarks.</p>
<p>Situation:</p>
<p>World: 10x10x10</p>
<p>Start: 0,0,0</p>
<p>End: 9,5,8</p>
<p>Nodes blocked: 300</p>
<p>Time:</p>
<p>Total (100 iterations) : 134ms</p>
<p>Average:  1.34ms</p>
<p>Lowest:   &lt; 1ms</p>
<p>Highest:  17ms</p>
<p>Note: as you can see because we did this test 100 times there is quite allot off difference between the lowest and highest time we needed to calculate this route, that&#8217;s because sometimes the cpu stops executing this program, and starts handleing an irq or doing something else, that&#8217;s why it&#8217;s important to always take a big number of benches to be representative. We can see from the average that this code is extremely fast.</p>
<p>Efficiency:</p>
<p>A* is an extremely efficient algorithm, consider we would&#8217;ve liked to brute-force this problem instead of using A*. That would&#8217;ve cost us 10^10^10 = 1,e+100 iterations. However with an (this) A* implementation we only needed 119 iterations (in which we checked 3094 nodes).</p>
<p>So, on to the code!</p>
<h3>The code.</h3>
<p>Well since I&#8217;ve divided every thing in much neater classes it&#8217;s a bit of a problem adding showing off all these as code-listings as I usually do. So I will only show the most important method here, the rest of the classes, and an example  you can download at the bottom of this article or by clicking <a title="Download A* 3D" href="http://cid-64e785655f2eee72.skydrive.live.com/self.aspx/.Public/XNA3/AStar3DUpdated.zip" target="_blank">here</a></p>
<pre class="brush: csharp; title: ; notranslate">
        /// &lt;summary&gt;
        /// Method that switfly finds the best path from start to end. Doesn't reverse outcome
        /// &lt;/summary&gt;
        /// &lt;returns&gt;The end breadcrump where each .next is a step back)&lt;/returns&gt;
        private static BreadCrumb FindPathReversed(World world, Point3D start, Point3D end)
        {
            MinHeap&lt;breadCrumb&gt; openList = new MinHeap&lt;breadCrumb&gt;(256);
            BreadCrumb[, ,] brWorld = new BreadCrumb[world.Right, world.Top, world.Back];
            BreadCrumb node;
            Point3D tmp;
            int cost;
            int diff;

            BreadCrumb current = new BreadCrumb(start);
            current.cost = 0;

            BreadCrumb finish = new BreadCrumb(end);
            brWorld[current.position.X, current.position.Y, current.position.Z] = current;
            openList.Add(current);

            while (openList.Count &gt; 0)
            {
                //Find best item and switch it to the 'closedList'
                current = openList.ExtractFirst();
                current.onClosedList = true;

                //Find neighbours
                for (int i = 0; i &lt; surrounding.Length; i++)
                {
                    tmp = current.position + surrounding[i];
                    if (world.PositionIsFree(tmp))
                    {
                        //Check if we've already examined a neighbour, if not create a new node for it.
                        if (brWorld[tmp.X, tmp.Y, tmp.Z] == null)
                        {
                            node = new BreadCrumb(tmp);
                            brWorld[tmp.X, tmp.Y, tmp.Z] = node;
                        }
                        else
                        {
                            node = brWorld[tmp.X, tmp.Y, tmp.Z];
                        }

                        //If the node is not on the 'closedList' check it's new score, keep the best
                        if (!node.onClosedList)
                        {
                            diff = 0;
                            if (current.position.X != node.position.X)
                            {
                                diff += 1;
                            }
                            if (current.position.Y != node.position.Y)
                            {
                                diff += 1;
                            }
                            if (current.position.Z != node.position.Z)
                            {
                                diff += 1;
                            }
                            cost = current.cost + diff + node.position.GetDistanceSquared(end);

                            if (cost &lt; node.cost)
                            {
                                node.cost = cost;
                                node.next = current;
                            }

                            //If the node wasn't on the openList yet, add it
                            if (!node.onOpenList)
                            {
                                //Check to see if we're done
                                if (node.Equals(finish))
                                {
                                    node.next = current;
                                    return node;
                                }
                                node.onOpenList = true;
                                openList.Add(node);
                            }
                        }
                    }
                }
            }
            return null; //no path found
        }
</pre>
<p>(note the class also has a normal FindPath() method that switches start and end for you).</p>
<h3>Download.</h3>
<p>In case you missed the download link in the middle of the text, download the entire example: <a title="Download A*3D" href="http://cid-64e785655f2eee72.skydrive.live.com/self.aspx/.Public/XNA3/AStar3DUpdated.zip" target="_blank"> here </a></p>
<p><a href="http://www.gamedevkicks.com/kick/?url=http%3a%2f%2froyalexander.wordpress.com%2f2009%2f07%2f07%2fnew-version-a-pathfinding-in-3d%2f"><img src="http://www.gamedevkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2froyalexander.wordpress.com%2f2009%2f07%2f07%2fnew-version-a-pathfinding-in-3d%2f" border="0" alt="kick it on GameDevKicks.com" /></a></p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://roy-t.nl/index.php/2009/07/07/new-version-a-pathfinding-in-3d/' addthis:title='New Version A* pathfinding in 3D' ><a class="addthis_button_twitter"></a><a class="addthis_button_stumbleupon"></a><a class="addthis_button_reddit"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://roy-t.nl/index.php/2009/07/07/new-version-a-pathfinding-in-3d/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>Upcomming A* pathfinding in 2D and 3D code updated</title>
		<link>http://roy-t.nl/index.php/2009/06/29/upcomming-a-pathfinding-in-2d-and-3d-code-updated/</link>
		<comments>http://roy-t.nl/index.php/2009/06/29/upcomming-a-pathfinding-in-2d-and-3d-code-updated/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 16:44:59 +0000</pubDate>
		<dc:creator>Roy Triesscheijn</dc:creator>
				<category><![CDATA[General Gamedesign]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[2D]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[A Star]]></category>
		<category><![CDATA[A*]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Pathfinding]]></category>

		<guid isPermaLink="false">http://royalexander.wordpress.com/?p=168</guid>
		<description><![CDATA[So allot of people have commented on my A* tutorial I posted here a while ago, but they&#8217;ve also pointed out a few flaws in the design so I&#8217;ve been working on a new version. The new version also incorperates 2D AND 3D worlds so you can use this class for both, I&#8217;ve also optimized [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://roy-t.nl/index.php/2009/06/29/upcomming-a-pathfinding-in-2d-and-3d-code-updated/' addthis:title='Upcomming A* pathfinding in 2D and 3D code updated' ><a class="addthis_button_twitter"></a><a class="addthis_button_stumbleupon"></a><a class="addthis_button_reddit"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>So allot of people have commented on my A* tutorial I posted here a while ago, but they&#8217;ve also pointed out a few flaws in the design so I&#8217;ve been working on a new version.</p>
<p>The new version also incorperates 2D AND 3D worlds so you can use this class for both, I&#8217;ve also optimized a bunch, made everything allot more clear replaced those dodgy ints with nice structs and fixed an unseen bug.</p>
<p>I hope to be able to get it online tomorrow, there&#8217;s just to much to polish atm to post it right now, but I&#8217;ll keep you guys updated!</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://roy-t.nl/index.php/2009/06/29/upcomming-a-pathfinding-in-2d-and-3d-code-updated/' addthis:title='Upcomming A* pathfinding in 2D and 3D code updated' ><a class="addthis_button_twitter"></a><a class="addthis_button_stumbleupon"></a><a class="addthis_button_reddit"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://roy-t.nl/index.php/2009/06/29/upcomming-a-pathfinding-in-2d-and-3d-code-updated/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

