<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Mike Talbot&#039;s Blog</title>
	<atom:link href="http://whydoidoit.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://whydoidoit.com</link>
	<description>Musing on software development in .NET/Silverlight and C#</description>
	<lastBuildDate>Fri, 18 May 2012 15:13:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='whydoidoit.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/d9da59a6b656ce63457986cc8f7babe0?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Mike Talbot&#039;s Blog</title>
		<link>http://whydoidoit.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://whydoidoit.com/osd.xml" title="Mike Talbot&#039;s Blog" />
	<atom:link rel='hub' href='http://whydoidoit.com/?pushpress=hub'/>
		<item>
		<title>Faster Invoke for reflected property access and method invocation with AOT compilation</title>
		<link>http://whydoidoit.com/2012/04/18/faster-invoke-for-reflected-property-access-and-method-invocation-with-aot-compilation/</link>
		<comments>http://whydoidoit.com/2012/04/18/faster-invoke-for-reflected-property-access-and-method-invocation-with-aot-compilation/#comments</comments>
		<pubDate>Wed, 18 Apr 2012 21:19:36 +0000</pubDate>
		<dc:creator>whydoidoit</dc:creator>
				<category><![CDATA[Project With Code]]></category>
		<category><![CDATA[AOT]]></category>
		<category><![CDATA[Invoke]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[Reflection]]></category>
		<category><![CDATA[unity]]></category>

		<guid isPermaLink="false">http://whydoidoit.com/?p=518</guid>
		<description><![CDATA[The bane of the iOS programmers life, when working with reflection in Mono, is that you can&#8217;t go around making up new generic types to ensure that your reflected properties and methods get called at decent speed. This is because Mono on iOS is fully Ahead Of Time compiled and simply can&#8217;t make up new [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&#038;blog=11474660&#038;post=518&#038;subd=whydoidoit&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.miketalbot.plus.com/DelegateSupport.cs" target="_blank"><br />
<img class="alignright size-full wp-image-181" title="Download" src="http://whydoidoit.files.wordpress.com/2010/08/download.png?w=620" alt="Download"   /></a><br />
The bane of the iOS programmers life, when working with reflection in Mono, is that you can&#8217;t go around making up new generic types to ensure that your reflected properties and methods get called at decent speed.  This is because Mono on iOS is fully Ahead Of Time compiled and simply can&#8217;t make up new stuff as you go along.  That coupled with the dire performance of Invoke when using reflected properties lead me to construct a helper class.</p>
<p>This works by registering a series of method signatures with the compiler, so that they are available to code running on the device.  In my tests property access was 4.5x faster and method access with one parameters was 2.4x faster.  Not earth shattering but every little helps.  If you knew what you wanted ahead of time, then you could probably do a lot better.  See <a href="http://stackoverflow.com/questions/1116073/can-delegate-dynamicinvoke-be-avoided-in-this-generic-code" target="_blank">here</a> for info.</p>
<p>You have to register signatures inside each class I&#8217;m afraid.  Nothing I can do about that.</p>
<p>So to register a signature you use:</p>
<p><pre class="brush: csharp;">
static MyClass()
{
     //All methods returning string can be accelerated
     DelegateSupport.RegisterFunctionType&lt;MyClass, string&gt;();         
     //All methods returning string and taking an int can be accelerated
     DelegateSupport.RegisterFunctionType&lt;MyClass, int, string&gt;();    
     //All methods returning void and taking a bool can be accelerated
     DelegateSupport.RegisterActionType&lt;MyClass, bool&gt;();             

}
</pre></p>
<p>Then when you have a MethodInfo you use the extension method FastInvoke(object target, params object[] parameters) to call it.  FastInvoke will default to using normal Invoke if you haven&#8217;t accelerated a particular type.</p>
<p><pre class="brush: csharp;">
       myObject.GetType().GetProperty(&quot;SomeProperty&quot;).GetGetMethod().FastInvoke(myObject);
       myObject.GetType().GetMethod(&quot;SomeMethod&quot;).FastInvoke(myObject, 1, 2);
</pre></p>
<p>You can download the source code for FastInvoke from <a href="http://www.miketalbot.plus.com/DelegateSupport.cs" target="_blank">here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/whydoidoit.wordpress.com/518/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/whydoidoit.wordpress.com/518/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/whydoidoit.wordpress.com/518/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/whydoidoit.wordpress.com/518/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/whydoidoit.wordpress.com/518/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/whydoidoit.wordpress.com/518/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/whydoidoit.wordpress.com/518/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/whydoidoit.wordpress.com/518/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/whydoidoit.wordpress.com/518/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/whydoidoit.wordpress.com/518/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/whydoidoit.wordpress.com/518/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/whydoidoit.wordpress.com/518/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/whydoidoit.wordpress.com/518/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/whydoidoit.wordpress.com/518/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&#038;blog=11474660&#038;post=518&#038;subd=whydoidoit&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://whydoidoit.com/2012/04/18/faster-invoke-for-reflected-property-access-and-method-invocation-with-aot-compilation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43adcf99cd9f3ccb588117bbdb855f5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">whydoidoit</media:title>
		</media:content>

		<media:content url="http://whydoidoit.files.wordpress.com/2010/08/download.png" medium="image">
			<media:title type="html">Download</media:title>
		</media:content>
	</item>
		<item>
		<title>Fixing the width of strings displayed in the Unity GUI</title>
		<link>http://whydoidoit.com/2012/04/12/fixing-the-width-of-strings-displayed-in-the-unity-gui/</link>
		<comments>http://whydoidoit.com/2012/04/12/fixing-the-width-of-strings-displayed-in-the-unity-gui/#comments</comments>
		<pubDate>Thu, 12 Apr 2012 19:56:07 +0000</pubDate>
		<dc:creator>whydoidoit</dc:creator>
				<category><![CDATA[Project With Code]]></category>
		<category><![CDATA[fixed width]]></category>
		<category><![CDATA[gui]]></category>
		<category><![CDATA[guilayout]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[unity]]></category>

		<guid isPermaLink="false">http://whydoidoit.com/?p=509</guid>
		<description><![CDATA[Firstly, I know what you are going to say &#8211; you should just use two aligned labels, rather than fiddling around with string formatting &#8211; but sometimes that just doesn&#8217;t work &#8211; like when you want the items in a popup to have columns like here: No chance for using labels in that component.  So [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&#038;blog=11474660&#038;post=509&#038;subd=whydoidoit&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Firstly, I know what you are going to say &#8211; you should just use two aligned labels, rather than fiddling around with string formatting &#8211; but sometimes that just doesn&#8217;t work &#8211; like when you want the items in a popup to have columns like here:</p>
<p><a href="http://whydoidoit.files.wordpress.com/2012/04/screen-shot-2012-04-12-at-20-47-52.png"><img class="size-full wp-image-510 alignnone" title="Screen Shot 2012-04-12 at 20.47.52" src="http://whydoidoit.files.wordpress.com/2012/04/screen-shot-2012-04-12-at-20-47-52.png?w=620" alt=""   /></a></p>
<p>No chance for using labels in that component.  So there is a way of effectively padding text to a given width.  You basically work out the width of a &#8221; &#8220;, a tab and the string, then use that combination to make a new string which is the correct width.</p>
<p>Just using string.PadRight(20) doesn&#8217;t work due to the variable width of characters in the font.</p>
<p><pre class="brush: csharp;">

public static class TextHelper
{
	public static string FixTo(this string str, float width, string type=&quot;label&quot;)
	{
	    var widthOfTab = GUI.skin.GetStyle(type).CalcSize(new GUIContent(&quot;\t&quot;)).x;
		var widthOfSpace = GUI.skin.GetStyle(type).CalcSize(new GUIContent(&quot; &quot;)).x;
		var widthOfString = GUI.skin.GetStyle(type).CalcSize(new GUIContent(str)).x;
	    return str + new String(' ', (int)((width-widthOfTab)/widthOfSpace)+1) + &quot;\t&quot;;
	}
}
</pre></p>
<p>You basically use <strong>myString.FixTo(150)</strong> to fix it based on the width of a &#8220;label&#8221; in the skin or you can override it to set a different font by using <strong>myString.FixTo(200, &#8220;box&#8221;)</strong>.</p>
<p>Should work fine in javascript so long as you make a .cs file out of this and put it in your plugins folder.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/whydoidoit.wordpress.com/509/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/whydoidoit.wordpress.com/509/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/whydoidoit.wordpress.com/509/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/whydoidoit.wordpress.com/509/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/whydoidoit.wordpress.com/509/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/whydoidoit.wordpress.com/509/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/whydoidoit.wordpress.com/509/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/whydoidoit.wordpress.com/509/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/whydoidoit.wordpress.com/509/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/whydoidoit.wordpress.com/509/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/whydoidoit.wordpress.com/509/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/whydoidoit.wordpress.com/509/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/whydoidoit.wordpress.com/509/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/whydoidoit.wordpress.com/509/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&#038;blog=11474660&#038;post=509&#038;subd=whydoidoit&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://whydoidoit.com/2012/04/12/fixing-the-width-of-strings-displayed-in-the-unity-gui/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43adcf99cd9f3ccb588117bbdb855f5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">whydoidoit</media:title>
		</media:content>

		<media:content url="http://whydoidoit.files.wordpress.com/2012/04/screen-shot-2012-04-12-at-20-47-52.png" medium="image">
			<media:title type="html">Screen Shot 2012-04-12 at 20.47.52</media:title>
		</media:content>
	</item>
		<item>
		<title>Extended Unity Coroutines</title>
		<link>http://whydoidoit.com/2012/04/07/extended-unity-coroutines/</link>
		<comments>http://whydoidoit.com/2012/04/07/extended-unity-coroutines/#comments</comments>
		<pubDate>Sat, 07 Apr 2012 12:33:17 +0000</pubDate>
		<dc:creator>whydoidoit</dc:creator>
				<category><![CDATA[Project With Code]]></category>
		<category><![CDATA[AI]]></category>
		<category><![CDATA[coroutine]]></category>
		<category><![CDATA[startcoroutine]]></category>
		<category><![CDATA[unity]]></category>
		<category><![CDATA[yieldinstruction]]></category>

		<guid isPermaLink="false">http://whydoidoit.com/?p=497</guid>
		<description><![CDATA[I really like the coroutine pattern in unity but unfortunately it&#8217;s not possible to directly extend it. What I would really like is to be able to write cut scene and NPC logic in a single routine, but I need to do things like wait for an animation to complete or an AI task goal [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&#038;blog=11474660&#038;post=497&#038;subd=whydoidoit&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.miketalbot.plus.com/coroutineExtension.unitypackage" target="_blank"><br />
<img class="alignright size-full wp-image-181" title="Download" src="http://whydoidoit.files.wordpress.com/2010/08/download.png?w=620" alt="Download"   /></a><br />
I really like the coroutine pattern in unity but unfortunately it&#8217;s not possible to directly extend it. What I would really like is to be able to write cut scene and NPC logic in a single routine, but I need to do things like wait for an animation to complete or an AI task goal to be achieved. Not being able to write my own YieldInstructions I took to the keyboard and wrote an extended coroutine system that uses as Unity&#8217;s existing stuff as a base.</p>
<p>To use it you can <a href="http://www.miketalbot.plus.com/coroutineExtension.unitypackage" target="_blank">download</a> and install the package, then when you want to start an extended coroutine you use:</p>
<h3>
StartCoroutine( <strong>RadicalRoutine.Run</strong>(<em>yourEnumeratorFunction</em>()) )<br />
</h3>
<p>YOU MUST insert the RadicalRoutine.Run inside the StartCoroutine call or not much will happen!</p>
<p>Note it only works with the none &#8220;text&#8221; based version of StartCoroutine at the moment.</p>
<p>Now you can write your own classes that indicate the completion status, they inherit from <strong>CoroutineReturn</strong> and can set or override two key values.</p>
<ul>
<li>finished &#8211; set to true / or override and return true when the action has finished</li>
<li>cancel &#8211; set to true / or override and return true if you want the whole coroutine to be aborted</li>
</ul>
<p>I&#8217;ve included one that waits for a part of an animation (end or some point in the middle) to complete and a version that waits for the weight of some animation to reach a value (I use this to keep my blended animations smooth in cut scenes). It&#8217;s called <strong>WaitForAnimation</strong> and there&#8217;s an extension class that adds methods to GameObject: gameObject.<strong>WaitForAnimation</strong>(name, normalizedTime /* default 1f */) and gameObject.<strong>WaitForAnimationWeight</strong>(name, weight /* default 0f */);</p>
<p>You can also yield any standard Unity YieldInstruction like WWW, WaitForSeconds etc.</p>
<p>You can use the coroutine functions from c# or javascript.</p>
<h2>Example</h2>
<p>Here&#8217;s an example of me using the extensions to run a complete sequence for an NPC. Obviously in this example I&#8217;m calling routines in my game that aren&#8217;t listed, but they all return CoroutineReturn instances that pause execution until the task is complete. You can see me using the WaitForAnimation and WaitForAnimationWeight methods directly.</p>
<p>What I want to do is move my character over to a table, pick up an envelope, show it to the player and then put it back down again afterwards.</p>
<p><pre class="brush: csharp;">
	IEnumerator MoveToPosition_EnterState()
	{
		Vector3 envAngle;

		//Store the current position of the envelope to be picked up
		var rot = msg.transform.rotation;
		var parent = msg.transform.parent;
		var pos = msg.transform.position;
		//Find the point that will carry the envelope
		var ch = gameObject.GetComponentInChildren();

		//Move the character to the envelope's location
		yield return MoveTo((lookPt = msg.transform.Find(&quot;EnvelopeLook&quot;)).position);
		//Look at the envelope
		yield return RotateTo(envAngle = lookPt.rotation.eulerAngles);
		yield return new WaitForSeconds(0.3f);
		//Play the pickup animation
		Animate(&quot;pick_up_from_table&quot;);
		//Wait until half way through
		yield return gameObject.WaitForAnimation(&quot;pick_up_from_table&quot;,0.5f);

		//Put the envelope in the carry point
		msg.transform.parent = ch.transform;
		msg.transform.localPosition = Vector3.zero;
		msg.transform.localRotation = Quaternion.Euler(0,0,0);

		//Wait for the pickup animation to complete
		yield return gameObject.WaitForAnimation(&quot;pick_up_from_table&quot;);

		yield return new WaitForSeconds(0.2f);
		//Turn to face the camera
		yield return RotateTo(new Vector3(0,10,0));
		//Wait for the rotation animations to complete
		yield return gameObject.WaitForAnimationWeight(&quot;left_turn&quot;); //Without a specified second parameter waits for 0
		yield return gameObject.WaitForAnimationWeight(&quot;right_turn&quot;);
		//Play the excited animation
		yield return Animate(&quot;excited&quot;);
		yield return new WaitForSeconds(0.5f);
		//Raise the hand containing the envelope
		yield return Animate(&quot;hand_raising&quot;);
		yield return new WaitForSeconds(1.5f);
		yield return Animate(&quot;hand_raising&quot;);
		yield return new WaitForSeconds(2.5f);
		//Wave move dramatically
		yield return Animate(&quot;waving&quot;);
		yield return new WaitForSeconds(0.5f);
		//Turn around to face the table
		yield return RotateTo(envAngle);
		//Play the pickup animation
		Animate(&quot;pick_up_from_table&quot;);
		//Wait for it to be 40% complete
		yield return gameObject.WaitForAnimation(&quot;pick_up_from_table&quot;,0.4f);
		//Put the envelope back on the table
		msg.transform.parent = parent;
		msg.transform.position = pos;
		msg.transform.rotation = rot;
		//Wait for the animation to complete
		yield return gameObject.WaitForAnimation(&quot;pick_up_from_table&quot;);
		//Rotate to face the camera
		yield return RotateTo(new Vector3(0,10,0));
		//Look over shoulder at envelope
		yield return Animate(&quot;looking_behind&quot;);
		//Sigh
		yield return Animate(&quot;relieved_sigh&quot;);
		yield return new WaitForSeconds(1);
		//Finish activity
		BlackboardComplete();

	}

</pre></p>
<h2>Additional</h2>
<p>You can also call RadicalRoutine.Create(yourCoRoutineFunction()) which returns an RadicalRoutine instance that can be cancelled. To use that you pass the instances enumerator property to StartCoroutine like this:</p>
<p>r = RadicalRoutine.Create(myRoutine());<br />
StartCoroutine(r.enumerator);</p>
<p>&#8230;</p>
<p>r.Cancel(); //Abort on the next iteration</p>
<p>Also the RadicalRoutine has events for cancellation and completion called Cancelled and Finished.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/whydoidoit.wordpress.com/497/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/whydoidoit.wordpress.com/497/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/whydoidoit.wordpress.com/497/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/whydoidoit.wordpress.com/497/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/whydoidoit.wordpress.com/497/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/whydoidoit.wordpress.com/497/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/whydoidoit.wordpress.com/497/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/whydoidoit.wordpress.com/497/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/whydoidoit.wordpress.com/497/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/whydoidoit.wordpress.com/497/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/whydoidoit.wordpress.com/497/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/whydoidoit.wordpress.com/497/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/whydoidoit.wordpress.com/497/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/whydoidoit.wordpress.com/497/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&#038;blog=11474660&#038;post=497&#038;subd=whydoidoit&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://whydoidoit.com/2012/04/07/extended-unity-coroutines/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43adcf99cd9f3ccb588117bbdb855f5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">whydoidoit</media:title>
		</media:content>

		<media:content url="http://whydoidoit.files.wordpress.com/2010/08/download.png" medium="image">
			<media:title type="html">Download</media:title>
		</media:content>
	</item>
		<item>
		<title>Unity curved path following with easing at constant speed</title>
		<link>http://whydoidoit.com/2012/04/06/unity-curved-path-following-with-easing/</link>
		<comments>http://whydoidoit.com/2012/04/06/unity-curved-path-following-with-easing/#comments</comments>
		<pubDate>Fri, 06 Apr 2012 13:10:08 +0000</pubDate>
		<dc:creator>whydoidoit</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Project With Code]]></category>
		<category><![CDATA[bezier]]></category>
		<category><![CDATA[constant speed]]></category>
		<category><![CDATA[easing]]></category>
		<category><![CDATA[iTween]]></category>
		<category><![CDATA[path]]></category>
		<category><![CDATA[path follow]]></category>
		<category><![CDATA[smooth]]></category>
		<category><![CDATA[unity]]></category>

		<guid isPermaLink="false">http://whydoidoit.com/?p=462</guid>
		<description><![CDATA[The Problem I have a character than needs to follow paths curved through a series of control points as it moves around the world. I also want to ease in and out to speed of movement as the path starts and ends. Sometimes in the middle of a path follow I need to change my [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&#038;blog=11474660&#038;post=462&#038;subd=whydoidoit&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.miketalbot.plus.com/easingAndCurves.unitypackage" target="_blank"><br />
<img class="alignright size-full wp-image-181" title="Download" src="http://whydoidoit.files.wordpress.com/2010/08/download.png?w=620" alt="Download"   /></a></p>
<h1>The Problem</h1>
<p>I have a character than needs to follow paths curved through a series of control points as it moves around the world. I also want to ease in and out to speed of movement as the path starts and ends. Sometimes in the middle of a path follow I need to change my mind and have some other path taken or perhaps change from path following to another state. This isn&#8217;t possible with iTween at constant speed unless you use PutOnPath, which doesn&#8217;t allow easing functions. I would also like to be able to move at a constant speed &#8211; either by ignoring the length of individual path elements (and thereby specifying at time fraction for the whole path) or by having a maximum speed my object can travel at.</p>
<h1>Solution</h1>
<p>Building on the great work done by <a href="http://forum.unity3d.com/viewtopic.php?p=218400#218400" target="_blank">Andeeee</a> and <a href="http://theinstructionlimit.com/flash-style-tweeneasing-functions-in-c">Renaud</a>, I&#8217;ve made a path following class that enables you to specify from 2 to N points and have it build a curved path (if it has more than 2 points!), you can then follow that path using a time from 0&#8230;1 and also apply an easing function to make the start and the end points smooth.</p>
<p>It supports Linear, Sine, Quadratic, Cubic, Quintic and Quartic easing functions with control on over whether the path is eased in and out separately.</p>
<p><img src="http://theinstructionlimit.com/wp-content/uploads/2009/07/easing.png" alt="" /><br />
Line 1 &#8211; EaseIn, Line 2 &#8211; EaseOut, Line 3 &#8211; EaseIn and EaseOut</p>
<p>This allows fine grained control of an eased, spline path in an Update function that is not possible using iTween &#8211; this enables you to abort a movement halfway through if something more important is happening .</p>
<p>To use it you <a href="http://www.miketalbot.plus.com/easingAndCurves.unitypackage" target="_blank">download</a> and import the unity package.</p>
<h2>Eased spline paths</h2>
<p>To use no easing call <strong>Spline.Interp(arrayOfPoints, time)</strong>. <em>Where time is a float between 0 and 1.</em></p>
<p>The array of points can be an array of Vector3s, GameObjects or Transforms which are implicitly converted to a Spline.Path instance containing an array of Vector3s. DON&#8217;T BOTHER to create your own Spline.Path instances!</p>
<p>e.g. transform.position = Spline.Interp(myPathObject.GetComponentsInChildren&lt;Transform&gt;(), time);</p>
<p>This would use the myPathObject and all of its children to define the path. (For example only, don&#8217;t go calling GetComponentsInChildren every frame).</p>
<p>To ease the function you can supply one or more of the easing parameters:</p>
<p><strong>Spline.Interp(arrayOfPoints, time, easingType </strong><em>/* e.g. EasingType.Sine */</em><strong>, easeIn </strong><em>/* e.g. true (default) */</em><strong>, easeOut </strong><em>/*e.g. false (default true) */</em> <strong> );</strong></p>
<p>The Spline class also supports Andeeee&#8217;s Velocity and GizmoDraw functions to help you with debugging.</p>
<p>PLEASE NOTE: the function uses a different algorithm for short paths so that you can get a number of different effects.  If your path has 2 points it does a linear interpolation, 3 points does a Quadratic spline and 4 points does a Cubic spline.  The 5 or more point version uses Catmul Rom splines and ensures that the path passes through every point.</p>
<p>You can double up start and end points easily with the Spline.Wrap() call on your path array &#8211; you may want to do this for short paths if you want to ensure that all of the points are passed through &#8211; or just do it yourself.  There should be no effect if you are using InterpConstantSpeed or MoveOnPath when you do this (See below).</p>
<h2>Constant Speed</h2>
<p>I&#8217;ve added an <strong>InterpConstantSpeed</strong> function too, this tries to make the speed of movement constant between sections. It works in the same way as Interp, with a time value between 0 and 1.</p>
<p>Now just a note on that.  If your path points are moving then you need to watch constant speed interpolations as one section getting much longer or shorter could cause the position to vary wildly.  If your path sections are increasing or decreasing in size then you are better off using Interp and trying to make sure that each path section is roughly the same size (short sections will appear to have lower velocity than large sections, so they need to be &#8220;kind of&#8221; the same size).  <strong>Interp</strong> says if your path has 5 sections, each one takes 1/5 of the time to cross.  If you are in section 2 and section 3 gets bigger, you will complete section 2 in the normal time.  Using constant speed, the position at time &#8220;t&#8221; is along the magnitude of the whole path, if it gets longer or shorter then the position at &#8220;t&#8221; will change between sections moving the Vector unrealistically forwards or backwards. This is a problem if you are in an early stage of the path and a later path point, far off screen, is moving.  The Vector will stutter for apparently no reason.  If you want constant speed then you need to ensure that your path points maintain exactly the same distance from each other when they move (they need to move in an arc centered on the previous end point).</p>
<p>The second point is that applying &#8220;Constant Speed&#8221; and &#8220;Easing&#8221; appears to create a paradox; but InterpConstantSpeed accepts easing functions! Well the &#8220;Constant&#8221; bit refers to the sizes of path sections being converted to a path magnitude and the easing is applied to the &#8220;time&#8221; fed into the path calculator: so you can have your cake and eat it <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Path Movement and Character Speed</h2>
<p>I&#8217;ve also added a speed limited version, this also solves the constant speed issue by having a maximum possible movement speed, you can therefore move those path elements as often as you like &#8211; but you can&#8217;t specify the time it takes to complete the path. It works kind of like a SmoothDamp &#8211; here&#8217;s how you go about it:</p>
<p><pre class="brush: csharp;">
public class TestFollow : MonoBehaviour {

	public Transform[] path;

	float t = 0;

	// Update is called once per frame
	void Update () {
		transform.position = Spline.MoveOnPath(path, transform.position, ref t, 0.5f);

	}
}
</pre></p>
<p>Basically you pass the path, the current position and a reference to a float that will contain the currently targeted path point, the final parameter shown here is the number of world units per second to move.</p>
<p>This routine also has the added benefit that it will move to the start of the path before following it.</p>
<p>Here are all of the parameters to MoveOnPath:</p>
<table>
<thead>
<tr>
<td><strong>Parameter</strong></td>
<td><strong>Meaning</strong></td>
</tr>
</thead>
<tbody>
<tr>
<td>pts</td>
<td>An array of transforms, Vector3s or GameObjects that describe the path</td>
</tr>
<tr>
<td>currentPosition</td>
<td>The current position of the object being moved</td>
</tr>
<tr>
<td>pathPosition</td>
<td>The position along the path, this is updated by the call so must be a variable of the class</td>
<tr>
<td><em>(Optional)rotation</em></td>
<td>A quaternion that will be updated to show the rotation that should be used. This can change quickly for slow moving objects, so you might want to smooth it. See below.</td>
</tr>
<tr>
<td>maxSpeed</td>
<td>The maximum number of world units to move in one second, default is 1</td>
</tr>
<tr>
<td>smoothnessFactor</td>
<td>The smoothing factor is the number of steps on the path that will be used, default is 100</td>
</tr>
<tr>
<td>ease</td>
<td>The type from EasingType.Linear, EasingType.Sine, EasingType.Cubic, EasingType.Quadratic, EasingType.Quartic and EasingType.Quintic. Default is Linear</td>
</tr>
<tr>
<td>easeIn</td>
<td>true if you want to ease in the function or false if you don&#8217;t. Default is true</td>
</tr>
<tr>
<td>easeOut</td>
<td>true if you want to ease out the function or false if you don&#8217;t. Default is true</td>
</tr>
</tbody>
</table>
<p>Here&#8217;s an example of the version that returns a rotation, in this example I use the <a href="http://whydoidoit.com/2012/04/01/smoothed-vector3-quaternions-and-floats-in-unity/">SmoothQuaternion</a> from a previous post to smooth out the rotations.</p>
<p><pre class="brush: jscript;">
#pragma strict

var pathPoints : Transform[];

var t : float;
var sr : SmoothQuaternion;

function Start() {
	sr = transform.rotation;
	sr.Duration = 0.5f;
}

function Update () {
	var q : Quaternion;
	transform.position = Spline.MoveOnPath(pathPoints, transform.position, t, q, 0.5f, 100, EasingType.Sine, true, true);
	sr.Value = q;
	transform.rotation = sr;

}
</pre></p>
<h1>Javascript</h1>
<p>You can use the Spline class from JavaScript, as long as the .cs files are somewhere inside a Assets/Plugins folder (the download does this) and that JavaScript is not inside a Plugins folder.</p>
<p>This example will follow a constant speed path for 20 seconds.</p>
<p><pre class="brush: jscript;">
#pragma strict

var pathPoints : Transform[];

var t : float;

function Update () {
	transform.position = Spline.InterpConstantSpeed(pathPoints, t, EasingType.Sine, true, true);
	t += Time.deltaTime/20;
}
</pre></p>
<p>The next one moves an item on a path at a maximum of 0.5 world units per second</p>
<p><pre class="brush: jscript;">
#pragma strict

var pathPoints : Transform[];

var t : float;

function Update () {
	transform.position = Spline.MoveOnPath(pathPoints, transform.position, t, 0.5f, 100, EasingType.Sine, true, true);

}
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/whydoidoit.wordpress.com/462/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/whydoidoit.wordpress.com/462/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/whydoidoit.wordpress.com/462/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/whydoidoit.wordpress.com/462/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/whydoidoit.wordpress.com/462/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/whydoidoit.wordpress.com/462/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/whydoidoit.wordpress.com/462/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/whydoidoit.wordpress.com/462/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/whydoidoit.wordpress.com/462/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/whydoidoit.wordpress.com/462/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/whydoidoit.wordpress.com/462/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/whydoidoit.wordpress.com/462/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/whydoidoit.wordpress.com/462/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/whydoidoit.wordpress.com/462/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&#038;blog=11474660&#038;post=462&#038;subd=whydoidoit&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://whydoidoit.com/2012/04/06/unity-curved-path-following-with-easing/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43adcf99cd9f3ccb588117bbdb855f5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">whydoidoit</media:title>
		</media:content>

		<media:content url="http://whydoidoit.files.wordpress.com/2010/08/download.png" medium="image">
			<media:title type="html">Download</media:title>
		</media:content>

		<media:content url="http://theinstructionlimit.com/wp-content/uploads/2009/07/easing.png" medium="image" />
	</item>
		<item>
		<title>Smoothed Vector3, Quaternions and floats in Unity</title>
		<link>http://whydoidoit.com/2012/04/01/smoothed-vector3-quaternions-and-floats-in-unity/</link>
		<comments>http://whydoidoit.com/2012/04/01/smoothed-vector3-quaternions-and-floats-in-unity/#comments</comments>
		<pubDate>Sun, 01 Apr 2012 21:29:11 +0000</pubDate>
		<dc:creator>whydoidoit</dc:creator>
				<category><![CDATA[Project With Code]]></category>
		<category><![CDATA[float damping]]></category>
		<category><![CDATA[lerp]]></category>
		<category><![CDATA[quaternion]]></category>
		<category><![CDATA[slerp]]></category>
		<category><![CDATA[smoothdamp]]></category>
		<category><![CDATA[smoothstep]]></category>
		<category><![CDATA[unity]]></category>
		<category><![CDATA[vector3]]></category>

		<guid isPermaLink="false">http://whydoidoit.com/?p=451</guid>
		<description><![CDATA[I&#8217;ve put together a set of structs to handle changing the values of floats, Vector3s and Quaternions over time. It&#8217;s very simple to use and is far easier to read than having lots of time recognising code or referenced veclocities in the middle of your game logic. You replace a Vector3 (say) with a SmoothVector3 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&#038;blog=11474660&#038;post=451&#038;subd=whydoidoit&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.miketalbot.plus.com/Smoothing.cs" target="_blank"><br />
<img class="alignright size-full wp-image-181" title="Download" src="http://whydoidoit.files.wordpress.com/2010/08/download.png?w=620" alt="Download"   /></a><br />
I&#8217;ve put together a set of structs to handle changing the values of floats, Vector3s and Quaternions over time. It&#8217;s very simple to use and is far easier to read than having lots of time recognising code or referenced veclocities in the middle of your game logic. You replace a Vector3 (say) with a SmoothVector3 in your code, set its target value, smoothing method and duration, then read its value over time in an Update() call.  I use this when I don&#8217;t have a gameObject to hand to apply iTween or when I may want to keep changing the position and don&#8217;t want to mess with iTween and need the control at every Update call.</p>
<p>You can choose a smoothing mode from slerp, lerp, SmoothDamp and SmoothStep.</p>
<p>To use this in your code, <a href="http://www.miketalbot.plus.com/Smoothing.cs" target="_blank">download</a> the .cs file and put it in your Assets/Plugins folder.</p>
<p>Here is an example from JavaScript on how to use the SmoothVector3 to smoothly move an object when a key is pressed.</p>
<p><pre class="brush: jscript;">

#pragma strict

var pos : SmoothVector3;

function Start () {
	//Construct a SmoothVector3 by assigning a normal Vector3
	//this initializes the position
	pos = transform.position;

	//The following are optional and override the standard settings

	//Duration in seconds of the transform
	pos.Duration = 1;
	//Smoothing mode from damp, smooth, lerp and slerp
	pos.Mode = SmoothingMode.damp;
}

function Update () {
	if(Input.anyKeyDown)
	{
		//Setting the Value of the SmoothVector3 starts the interpolation
		//This will move the item 1 unit in X,Y from the current position
		//(which may be an interpolated value)
		pos.Value = pos.Value + Vector3(1,1,0);

		//In this example this is equivalent to
		//pos.Value = transform.position + Vector3(1,1,0);

		//An alternative is to use the .Target property
		//which is the current targeted position - in our example
		//this maintains the object on exact boundaries and means
		//that it will accelerate if you click lots of times
		//pos.Value = pos.Target + Vector3(1,1,0);

		//Or of course you could just set the value
		//pos.Value = Vector3(100,100,0);

	}

	//A SmoothVector3 can be used in place of a Vector3 in assignments
	transform.position = pos;

}

</pre></p>
<p>With Vector3 and Quaternions you can directly affect the individual elements to set the targeted value.</p>
<p><pre class="brush: jscript;">
       mySmoothVec3.x = mySmoothVec3.x + 100;
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/whydoidoit.wordpress.com/451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/whydoidoit.wordpress.com/451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/whydoidoit.wordpress.com/451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/whydoidoit.wordpress.com/451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/whydoidoit.wordpress.com/451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/whydoidoit.wordpress.com/451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/whydoidoit.wordpress.com/451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/whydoidoit.wordpress.com/451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/whydoidoit.wordpress.com/451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/whydoidoit.wordpress.com/451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/whydoidoit.wordpress.com/451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/whydoidoit.wordpress.com/451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/whydoidoit.wordpress.com/451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/whydoidoit.wordpress.com/451/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&#038;blog=11474660&#038;post=451&#038;subd=whydoidoit&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://whydoidoit.com/2012/04/01/smoothed-vector3-quaternions-and-floats-in-unity/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43adcf99cd9f3ccb588117bbdb855f5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">whydoidoit</media:title>
		</media:content>

		<media:content url="http://whydoidoit.files.wordpress.com/2010/08/download.png" medium="image">
			<media:title type="html">Download</media:title>
		</media:content>
	</item>
		<item>
		<title>Unity terrain avoiding smart follow camera</title>
		<link>http://whydoidoit.com/2012/03/22/unity-terrain-avoiding-smart-follow-camera/</link>
		<comments>http://whydoidoit.com/2012/03/22/unity-terrain-avoiding-smart-follow-camera/#comments</comments>
		<pubDate>Thu, 22 Mar 2012 21:50:55 +0000</pubDate>
		<dc:creator>whydoidoit</dc:creator>
				<category><![CDATA[Project With Code]]></category>
		<category><![CDATA[camera]]></category>
		<category><![CDATA[follow]]></category>
		<category><![CDATA[terrain]]></category>
		<category><![CDATA[unity]]></category>

		<guid isPermaLink="false">http://whydoidoit.com/?p=439</guid>
		<description><![CDATA[I&#8217;ve been needing a terrain avoiding follow camera, I couldn&#8217;t find one that was exactly right so I produced my own. My script uses multiple different methods to keep the followed object in view: * Desired location with the camera adjusted for terrain height * Rise above structures * Zoom in to be closer to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&#038;blog=11474660&#038;post=439&#038;subd=whydoidoit&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.miketalbot.plus.com/OrbitCamera.cs" target="_blank"><br />
<img class="alignright size-full wp-image-181" title="Download" src="http://whydoidoit.files.wordpress.com/2010/08/download.png?w=620" alt="Download"   /></a><br />
I&#8217;ve been needing a terrain avoiding follow camera, I couldn&#8217;t find one that was exactly right so I produced my own.</p>
<p>My script uses multiple different methods to keep the followed object in view:</p>
<p>* Desired location with the camera adjusted for terrain height</p>
<p>* Rise above structures</p>
<p>* Zoom in to be closer to the followed object</p>
<p>* Camera rotation (cut view) to a different angle</p>
<p>Each method can be weighted to give the effect that you prefer.  Each method votes on how aggressive a movement it had to make in order to locate the camera in an unobstructed location, the winner gets to set the location of the camera.</p>
<p>You can download it <a href="http://www.miketalbot.plus.com/OrbitCamera.cs" target="_blank">here</a>.</p>
<p>Here is it in operation &#8211; I&#8217;m moving the camera a bit to put it in difficult places, but mostly it&#8217;s under the control of the system.</p>
<div id="v-mI5yGNyz-1" class="video-player" style="width:620px;height:506px">
<embed id="v-mI5yGNyz-1-video" src="http://s0.videopress.com/player.swf?v=1.03&amp;guid=mI5yGNyz&amp;isDynamicSeeking=true" type="application/x-shockwave-flash" width="620" height="506" title="Sequence 01_1" wmode="direct" seamlesstabbing="true" allowfullscreen="true" allowscriptaccess="always" overstretch="true"></embed></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/whydoidoit.wordpress.com/439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/whydoidoit.wordpress.com/439/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/whydoidoit.wordpress.com/439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/whydoidoit.wordpress.com/439/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/whydoidoit.wordpress.com/439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/whydoidoit.wordpress.com/439/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/whydoidoit.wordpress.com/439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/whydoidoit.wordpress.com/439/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/whydoidoit.wordpress.com/439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/whydoidoit.wordpress.com/439/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/whydoidoit.wordpress.com/439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/whydoidoit.wordpress.com/439/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/whydoidoit.wordpress.com/439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/whydoidoit.wordpress.com/439/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&#038;blog=11474660&#038;post=439&#038;subd=whydoidoit&#038;ref=&#038;feed=1" width="1" height="1" /><div><a href="http://whydoidoit.com/2012/03/22/unity-terrain-avoiding-smart-follow-camera/"><img alt="Sequence 01_1" src="http://videos.videopress.com/mI5yGNyz/sequence-01_1_std.original.jpg" width="160" height="120" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://whydoidoit.com/2012/03/22/unity-terrain-avoiding-smart-follow-camera/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	<enclosure url="http://videos.videopress.com/mI5yGNyz/sequence-01_1_std.mp4" length="5603840" type="video/mp4" />

		<media:content url="http://0.gravatar.com/avatar/43adcf99cd9f3ccb588117bbdb855f5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">whydoidoit</media:title>
		</media:content>

		<media:content url="http://whydoidoit.files.wordpress.com/2010/08/download.png" medium="image">
			<media:title type="html">Download</media:title>
		</media:content>

		<media:group>
			<media:content url="http://videos.videopress.com/mI5yGNyz/sequence-01_1_std.mp4" fileSize="5603840" type="video/mp4" medium="video" bitrate="796" isDefault="true" duration="55" width="400" height="326" />

			<media:content url="http://videos.videopress.com/mI5yGNyz/sequence-01_1_fmt1.ogv" fileSize="5603840" type="video/ogg" medium="video" bitrate="796" isDefault="false" duration="55" width="400" height="326" />

			<media:rating scheme="urn:mpaa">g</media:rating>
			<media:title type="plain">Sequence 01_1</media:title>
			<media:thumbnail url="http://videos.videopress.com/mI5yGNyz/sequence-01_1_std.original.jpg" width="256" height="209" />
			<media:player url="http://s0.videopress.com/player.swf?v=1.03&#38;guid=mI5yGNyz&#38;isDynamicSeeking=true" width="400" height="327" />
		</media:group>
	</item>
		<item>
		<title>Unity automatic MeshCollider generation</title>
		<link>http://whydoidoit.com/2012/03/22/unity-automatic-meshcollider-generation/</link>
		<comments>http://whydoidoit.com/2012/03/22/unity-automatic-meshcollider-generation/#comments</comments>
		<pubDate>Thu, 22 Mar 2012 17:53:50 +0000</pubDate>
		<dc:creator>whydoidoit</dc:creator>
				<category><![CDATA[Project With Code]]></category>
		<category><![CDATA[meshcollider]]></category>
		<category><![CDATA[Source code]]></category>
		<category><![CDATA[unity]]></category>
		<category><![CDATA[wizard]]></category>

		<guid isPermaLink="false">http://whydoidoit.com/?p=428</guid>
		<description><![CDATA[When ever I get a model that has many sub elements, but no standard mesh colliders I use the wizard listed here to automatically generate them.  Thought it might come in useful to others&#8230;<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&#038;blog=11474660&#038;post=428&#038;subd=whydoidoit&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When ever I get a model that has many sub elements, but no standard mesh colliders I use the wizard listed here to automatically generate them.  Thought it might come in useful to others&#8230;</p>
<p><pre class="brush: csharp;">

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Linq;
public class AddColliders : ScriptableWizard {

 [MenuItem(&quot;Wizards/Add Mesh Colliders&quot;)]
 static void CreateWizard()
 {
    ScriptableWizard.DisplayWizard&lt;AddColliders&gt;(&quot;Add mesh colliders&quot;, &quot;Add Colliders&quot;);
 }

 void OnWizardCreate()
 {
    if(UnityEditor.Selection.activeGameObject != null)
    {
       foreach(var c in UnityEditor.Selection.activeGameObject.GetComponentsInChildren&lt;MeshRenderer&gt;().Cast&lt;MeshRenderer&gt;().
       Where(mr=&gt;mr.GetComponent&lt;MeshCollider&gt;()==null))
       {
           c.gameObject.AddComponent(typeof(MeshCollider));
       }
    }
 }
 void OnWizardUpdate()
 {
    helpString = &quot;Add mesh colliders to all items that have a mesh renderer&quot;;
 }

}

</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/whydoidoit.wordpress.com/428/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/whydoidoit.wordpress.com/428/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/whydoidoit.wordpress.com/428/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/whydoidoit.wordpress.com/428/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/whydoidoit.wordpress.com/428/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/whydoidoit.wordpress.com/428/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/whydoidoit.wordpress.com/428/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/whydoidoit.wordpress.com/428/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/whydoidoit.wordpress.com/428/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/whydoidoit.wordpress.com/428/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/whydoidoit.wordpress.com/428/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/whydoidoit.wordpress.com/428/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/whydoidoit.wordpress.com/428/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/whydoidoit.wordpress.com/428/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&#038;blog=11474660&#038;post=428&#038;subd=whydoidoit&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://whydoidoit.com/2012/03/22/unity-automatic-meshcollider-generation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43adcf99cd9f3ccb588117bbdb855f5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">whydoidoit</media:title>
		</media:content>
	</item>
		<item>
		<title>Updated WordCloud Component</title>
		<link>http://whydoidoit.com/2011/08/17/updated-wordcloud-component/</link>
		<comments>http://whydoidoit.com/2011/08/17/updated-wordcloud-component/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 12:50:59 +0000</pubDate>
		<dc:creator>whydoidoit</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://whydoidoit.com/?p=412</guid>
		<description><![CDATA[I&#8217;ve updated the WordCloud component and added some documentation that you can find here: http://whydoidoit.com/wordcloud-for-silverlight The new component supports individual word colouring and word angle.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&#038;blog=11474660&#038;post=412&#038;subd=whydoidoit&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve updated the WordCloud component and added some documentation that you can find here: <a href="http://whydoidoit.com/wordcloud-for-silverlight/">http://whydoidoit.com/wordcloud-for-silverlight</a></p>
<p>The new component supports individual word colouring and word angle.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/whydoidoit.wordpress.com/412/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/whydoidoit.wordpress.com/412/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/whydoidoit.wordpress.com/412/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/whydoidoit.wordpress.com/412/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/whydoidoit.wordpress.com/412/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/whydoidoit.wordpress.com/412/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/whydoidoit.wordpress.com/412/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/whydoidoit.wordpress.com/412/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/whydoidoit.wordpress.com/412/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/whydoidoit.wordpress.com/412/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/whydoidoit.wordpress.com/412/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/whydoidoit.wordpress.com/412/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/whydoidoit.wordpress.com/412/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/whydoidoit.wordpress.com/412/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&#038;blog=11474660&#038;post=412&#038;subd=whydoidoit&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://whydoidoit.com/2011/08/17/updated-wordcloud-component/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43adcf99cd9f3ccb588117bbdb855f5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">whydoidoit</media:title>
		</media:content>
	</item>
		<item>
		<title>SilverlightSerializer Version 2</title>
		<link>http://whydoidoit.com/2011/08/05/silverlightserializer-version-2/</link>
		<comments>http://whydoidoit.com/2011/08/05/silverlightserializer-version-2/#comments</comments>
		<pubDate>Fri, 05 Aug 2011 20:22:11 +0000</pubDate>
		<dc:creator>whydoidoit</dc:creator>
				<category><![CDATA[Project With Code]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Binary Serialization]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MakeGenericType]]></category>
		<category><![CDATA[Missing]]></category>
		<category><![CDATA[MissingMethod]]></category>
		<category><![CDATA[Reflection]]></category>
		<category><![CDATA[Serialization]]></category>
		<category><![CDATA[Serializer]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[SilverlightSerializer]]></category>

		<guid isPermaLink="false">http://whydoidoit.com/?p=352</guid>
		<description><![CDATA[SilverlightSerializer version 2 Well, I know it&#8217;s been a long wait and I&#8217;ve been promising the new version of SilverlightSerializer for months, but it&#8217;s finally here, extracted from my core project and working standalone. This new version of the serializer is a major rewrite &#8211; with 2 goals in mind: 1. Performance Serialization performance is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&#038;blog=11474660&#038;post=352&#038;subd=whydoidoit&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h3>SilverlightSerializer version 2</h3>
<p>Well, I know it&#8217;s been a long wait and I&#8217;ve been promising the new version of SilverlightSerializer for months, but it&#8217;s finally here, extracted from my core project and working standalone.</p>
<p>This new version of the serializer is a major rewrite &#8211; with 2 goals in mind:</p>
<p style="padding-left:30px;">1. <strong>Performance</strong></p>
<p style="padding-left:60px;">Serialization performance is <strong>61%</strong> faster (my test harness serializes 100,000 objects in 1.03 seconds compared to 1.72)<br />
Deserialization performance is <strong>27%</strong> faster (my test hardness deserializes 100,000 objects in 1.61 seconds compared to 2.04)</p>
<p style="padding-left:60px;">These performance gains are on large object graphs containing multiple instances of the same types &#8211; it would nominally be slightly slower on small graphs or graphs that contain only one copy of each object &#8211; but of course that&#8217;s very unlikely in the real world.</p>
<p style="padding-left:30px;">2. <strong>Extensibility</strong></p>
<p style="padding-left:60px;">The new version of SilverlightSerializer abstracts the storage methods behind a new interface, IStorage.  There&#8217;s a complete implementation of a BinarySerializer that is compatible with the old version of SilverlightSerializer, but you could add your own.  Alterian has built versions that store data in XML and SQL Server tables.  This means one standard semantic serialization layer can be used with pluggable storage types to put the information where you need it for the application at hand.</p>
<p style="padding-left:60px;">That said, writing an IStorage is fairly involved process and I&#8217;ll write a post or a page on it if people want me to.</p>
<p>In addition the serializer now handles arrays and enums far better than before and hopefully should avoid the dreaded <strong>MissingMethod</strong> exception by providing a more developer friendly exception and message while offering the opportunity to construct classes without a parameterless constructor using a new CreateType event that will help some avoid the need to write custom serializers.</p>
<p>You can find the version 2 source code <a title="Silverlight Serializer" href="http://whydoidoit.com/silverlight-serializer/">here</a>.  In response to a couple of requests, the project is also accessible on GitHub, see the link at the top of the SilverlightSerializer page.</p>
<h3>Why is it faster?</h3>
<p>So read on if you&#8217;d like to know why it&#8217;s faster!</p>
<p>SilverlightSerializer is a reflection based serializer.  It examines an object to identify the available properties and fields. As anyone who is familiar with .NET will know, reflection isn&#8217;t exactly the fastest thing in the world.  SS v1 did its level best to cache everything possible, but it still relied on reflection to write and read properties and fields from the underlying objects.  This is why SS didn&#8217;t perform as well as the inbuilt serializers in .NET.</p>
<p>Now reflection in this case is a hard thing to get around, I don&#8217;t know the types in advance and I have no interest in getting the developer to have to write a custom serialization class for each of their types.</p>
<p>The answer to this performance challenge came from using generic classes and using .NET to construct a new generic class on the fly for each property of each class being serialized.  The generic class accesses the native property access functions directly, rather than have to use reflection.  Fields have to be handled slightly differently, with a reflection function, but at least it&#8217;s no slower than the previous method.</p>
<p>First let&#8217;s start with the base class that is used for all of the generic classes, it&#8217;s called GetSet.</p>
<p><pre class="brush: csharp;">

public abstract class GetSet
{
public PropertyInfo Info;
public string Name;
public FieldInfo FieldInfo;
public object Vanilla;
public bool CollectionType;
public abstract object Get(object item);
public abstract void Set(object item, object value);

}

</pre></p>
<p>From this base class I create a definition of a generic class, called GetSetGeneric.  This class creates Get and Set delegates using the class parameters, these delegates then map to the functions that will be exposed by the property getters and setters on the target class (neat huh).  This happens only once per type/property combination and the resulting code is the same speed as writing a function to get the value.</p>
<p><pre class="brush: csharp;">
public class GetSetGeneric&lt;T, TR&gt; : GetSet
{
public delegate TR GetValue(T obj);
public delegate void SetValue(T obj, TR value);
private readonly GetValue _get;
private readonly SetValue _set;

public GetSetGeneric(PropertyInfo info)
{
MethodInfo getMethod;
MethodInfo setMethod = null;
Name = info.Name;
Info = info;
CollectionType = Info.PropertyType.GetInterface(&quot;IEnumerable&quot;, true) != null;
getMethod = info.GetGetMethod();
setMethod = info.GetSetMethod();
_get = (GetValue)Delegate.CreateDelegate(typeof(GetValue), getMethod);
if (setMethod != null) _set = (SetValue)Delegate.CreateDelegate(typeof(SetValue), setMethod);
}

public GetSetGeneric(FieldInfo info)
{
MethodInfo getMethod;
MethodInfo setMethod = null;
Name = info.Name;
FieldInfo = info;
_get = new GetValue(GetFieldValue);
_set = new SetValue(SetFieldValue);
CollectionType = FieldInfo.FieldType.GetInterface(&quot;IEnumerable&quot;, true) != null;
return;
}

public GetSetGeneric(string name)
{
Name = name;
MethodInfo getMethod;
MethodInfo setMethod= null;
var t = typeof(T);
var p = t.GetProperty(name);
if (p == null)
{
FieldInfo = typeof(T).GetField(Name);
_get = new GetValue(GetFieldValue);
_set = new SetValue(SetFieldValue);
CollectionType = FieldInfo.FieldType.GetInterface(&quot;IEnumerable&quot;, true) != null;
return;
}
Info = p;
CollectionType = Info.PropertyType.GetInterface(&quot;IEnumerable&quot;, true) != null;
getMethod = p.GetGetMethod();
setMethod = p.GetSetMethod();
_get = (GetValue)Delegate.CreateDelegate(typeof(GetValue), getMethod);
if(setMethod != null) _set = (SetValue)Delegate.CreateDelegate(typeof(SetValue), setMethod);
}

private TR GetFieldValue(T obj)
{
return (TR)FieldInfo.GetValue(obj);
}

private void SetFieldValue(T obj, TR value)
{
FieldInfo.SetValue(obj, value);
}

public override object Get(object item)
{
return _get((T)item);
}

public override void Set(object item, object value)
{
_set((T)item, (TR)value);
}
}

</pre></p>
<p>Once that&#8217;s done it&#8217;s just a matter of creating the generic classes. Deserialization does this one property at a time form the input stream:</p>
<p><pre class="brush: csharp;">
entryConfiguration = new EntryConfiguration();

var pi = entry.OwningType.GetProperty(entry.Name);
if (pi != null)
{
entryConfiguration.Type = pi.PropertyType;
var gs = typeof(GetSetGeneric&lt;,&gt;);
var tp = gs.MakeGenericType(new Type[] { entry.OwningType, pi.PropertyType });
entryConfiguration.Setter = (GetSet)Activator.CreateInstance(tp, new object[] { pi });
}
else
{
var fi = entry.OwningType.GetField(entry.Name);
if (fi != null)
{
entryConfiguration.Type = fi.FieldType;
var gs = typeof(GetSetGeneric&lt;,&gt;);
var tp = gs.MakeGenericType(new Type[] { entry.OwningType, fi.FieldType });
entryConfiguration.Setter = (GetSet)Activator.CreateInstance(tp, new object[] { fi });
}
}
</pre></p>
<p>Here you can see I get either the property or the field information, use the type of the containing class and the property in a call to MakeGenericType to construct the correct class, then create an instance of that class that can be used to get and set properties on the object.<br />
Serialization works by creating a list of getters and setters for every sensible property on the object, in SilverlightSerializer that creates a few different flavours depending on whether you are using Checksums etc. If you want to see how I do that, have a look at the GetWriteableAttributes class and the GetAccessors() function.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/whydoidoit.wordpress.com/352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/whydoidoit.wordpress.com/352/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/whydoidoit.wordpress.com/352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/whydoidoit.wordpress.com/352/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/whydoidoit.wordpress.com/352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/whydoidoit.wordpress.com/352/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/whydoidoit.wordpress.com/352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/whydoidoit.wordpress.com/352/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/whydoidoit.wordpress.com/352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/whydoidoit.wordpress.com/352/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/whydoidoit.wordpress.com/352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/whydoidoit.wordpress.com/352/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/whydoidoit.wordpress.com/352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/whydoidoit.wordpress.com/352/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&#038;blog=11474660&#038;post=352&#038;subd=whydoidoit&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://whydoidoit.com/2011/08/05/silverlightserializer-version-2/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43adcf99cd9f3ccb588117bbdb855f5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">whydoidoit</media:title>
		</media:content>
	</item>
		<item>
		<title>Wordle Style Word Cloud or Tag Cloud Component For Silverlight</title>
		<link>http://whydoidoit.com/2011/07/28/wordle-style-word-cloud-component-for-silverlight/</link>
		<comments>http://whydoidoit.com/2011/07/28/wordle-style-word-cloud-component-for-silverlight/#comments</comments>
		<pubDate>Thu, 28 Jul 2011 17:45:03 +0000</pubDate>
		<dc:creator>whydoidoit</dc:creator>
				<category><![CDATA[Project With Code]]></category>
		<category><![CDATA[Component]]></category>
		<category><![CDATA[Jonathan Feinberg]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Stack Overflow]]></category>
		<category><![CDATA[Tag cloud]]></category>
		<category><![CDATA[Word Cloud]]></category>
		<category><![CDATA[Wordle]]></category>

		<guid isPermaLink="false">http://whydoidoit.com/?p=330</guid>
		<description><![CDATA[LATEST VERSION AND DOCUMENTATION AVAILABLE HERE I&#8217;ve been looking for a component that could show Wordle style word clouds.  The only Silverlight component I could find was Infragistics xamTagCloud, but it really didn&#8217;t produce the output I was looking for.  What I wanted was to be able to put lower importance words within the spaces [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&#038;blog=11474660&#038;post=330&#038;subd=whydoidoit&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class="mceTemp">
<div id="attachment_333" class="wp-caption alignleft" style="width: 470px"><a href="http://whydoidoit.files.wordpress.com/2011/07/wordcloudpicture.png"><img class="size-full wp-image-333" title="wordcloudpicture" src="http://whydoidoit.files.wordpress.com/2011/07/wordcloudpicture.png?w=620" alt="Word Cloud"   /></a><p class="wp-caption-text">Example Word Cloud</p></div>
</div>
<h1><a href="http://www.miketalbot.plus.com/TestCloud.zip" target="_blank"><img class="alignright size-full wp-image-181" title="Download" src="http://whydoidoit.files.wordpress.com/2010/08/download.png?w=620" alt="Download"   /></a>LATEST VERSION AND DOCUMENTATION AVAILABLE <a href="http://whydoidoit.com/wordcloud-for-silverlight/">HERE</a></h1>
<p>I&#8217;ve been looking for a component that could show <a href="http://www.wordle.net/" target="_blank">Wordle</a> style word clouds.  The only Silverlight component I could find was Infragistics xamTagCloud, but it really didn&#8217;t produce the output I was looking for.  What I wanted was to be able to put lower importance words within the spaces left around larger entries.</p>
<div id="attachment_332" class="wp-caption alignright" style="width: 310px"><a href="http://whydoidoit.files.wordpress.com/2011/07/word-cloud-2.png"><img class="size-medium wp-image-332" title="Word Cloud Example" src="http://whydoidoit.files.wordpress.com/2011/07/word-cloud-2.png?w=300&h=199" alt="Words with coloring" width="300" height="199" /></a><p class="wp-caption-text">Example Subtle Word Coloring</p></div>
<p>Having looked around the web I couldn&#8217;t find a component to buy or borrow so I decided to write my own.</p>
<p>I came across <a title="StackOverflow post" href="http://stackoverflow.com/questions/342687/algorithm-to-implement-something-like-wordle" target="_blank">this</a> post on Stack Overflow that had a response from the author of Wordle (Jonathan Feinberg), giving some tips on how Wordle works.  I used that and the <a title="Wordle Algorithm" href="http://static.mrfeinberg.com/bv_ch03.pdf" target="_blank">extra reference</a> Jonathan provided to build my component.</p>
<p>The key to word layout is to use a spiral (or another progressive positioning equation) to try to position words, every new word needs to be checked against the previously placed items to ensure that there is no overlap.</p>
<p>As collision is the expensvie part of the algorithm Jonathan suggests using a variety of techniques around collision rectangles and space partitioning to do the item testing; I felt this wasn&#8217;t necessary for my needs so I actually test the overlapping pixels to see if there is a hit.  This seems to give good enough performance, and if I wanted to improve it I would advocate making a second collision map at a lower resolution (1/8 scale) that would significantly cut down on the processing &#8211; but again, wasn&#8217;t necessary for me.</p>
<p>If you want to use something to layout instead of my spiral, you need to replace the GetSpiralPoint function with something that will return a progressive space based on an increaseing angle measured in radians.</p>
<p>The component also supports word hit testing and selection as well as coloring words based on a value as well as sizing them.</p>
<p>You can download the full source code for the component and the test project which will allow you to play with the parameters from the link at the top of this post.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/whydoidoit.wordpress.com/330/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/whydoidoit.wordpress.com/330/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/whydoidoit.wordpress.com/330/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/whydoidoit.wordpress.com/330/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/whydoidoit.wordpress.com/330/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/whydoidoit.wordpress.com/330/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/whydoidoit.wordpress.com/330/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/whydoidoit.wordpress.com/330/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/whydoidoit.wordpress.com/330/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/whydoidoit.wordpress.com/330/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/whydoidoit.wordpress.com/330/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/whydoidoit.wordpress.com/330/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/whydoidoit.wordpress.com/330/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/whydoidoit.wordpress.com/330/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&#038;blog=11474660&#038;post=330&#038;subd=whydoidoit&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://whydoidoit.com/2011/07/28/wordle-style-word-cloud-component-for-silverlight/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43adcf99cd9f3ccb588117bbdb855f5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">whydoidoit</media:title>
		</media:content>

		<media:content url="http://whydoidoit.files.wordpress.com/2011/07/wordcloudpicture.png" medium="image">
			<media:title type="html">wordcloudpicture</media:title>
		</media:content>

		<media:content url="http://whydoidoit.files.wordpress.com/2010/08/download.png" medium="image">
			<media:title type="html">Download</media:title>
		</media:content>

		<media:content url="http://whydoidoit.files.wordpress.com/2011/07/word-cloud-2.png?w=300" medium="image">
			<media:title type="html">Word Cloud Example</media:title>
		</media:content>
	</item>
	</channel>
</rss>
