<?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>nklein software &#187; game design</title>
	<atom:link href="http://nklein.com/tags/game-design/feed/" rel="self" type="application/rss+xml" />
	<link>http://nklein.com</link>
	<description>software development and consulting</description>
	<lastBuildDate>Tue, 22 May 2012 03:48:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>A Recurrence of Constrained Shuffles</title>
		<link>http://nklein.com/2009/06/a-recurrence-of-constrained-shuffles/</link>
		<comments>http://nklein.com/2009/06/a-recurrence-of-constrained-shuffles/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 05:52:51 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[game design]]></category>
		<category><![CDATA[recurrence relations]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=478</guid>
		<description><![CDATA[Earlier, I posted about finding the Fibonacci numbers when counting a very simple constrained shuffle. In that article, I was concerned with the question: How many permutations are there in such that for all ? For that problem, the answer was . The number of permutations on items where no item moves more than one [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier, I <a href="http://nklein.com/2009/05/i-discovered-the-fibonacci-numbers/">posted about finding the Fibonacci numbers</a> when counting a very simple constrained shuffle.  In that article, I was concerned with the question:  <q>How many permutations are there in <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_1aba8e8186e3ec0beefc6db1f718e449.png" title="\Sigma_n" style="vertical-align:-20%;" class="tex" alt="\Sigma_n" /> such that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_186426730ead7ce2dac6ae5849e841e9.png" title="\left| \pi(i) - i \right| \le 1" style="vertical-align:-20%;" class="tex" alt="\left| \pi(i) - i \right| \le 1" /> for all <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e8e7c9058db45694b72005d4f4b41bb0.png" title="i \in [1,n]" style="vertical-align:-20%;" class="tex" alt="i \in [1,n]" />?</q></p>
<p>For that problem, the answer was <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e47ea81a9fb42594de930f03d9c022d7.png" title="F_{n+1}" style="vertical-align:-20%;" class="tex" alt="F_{n+1}" />.  The number of permutations on <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> items where no item moves more than one space from where it started is the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_ad29876a0c7f492a05327ad527cf8d4a.png" title="(n+1)" style="vertical-align:-20%;" class="tex" alt="(n+1)" />-th Fibonacci number.  Now, the obvious question is: <q>How many permutations are there in <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_1aba8e8186e3ec0beefc6db1f718e449.png" title="\Sigma_n" style="vertical-align:-20%;" class="tex" alt="\Sigma_n" /> such that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_043f5e06c892696b1a2d68676957086a.png" title="\left| \pi(i) - i \right| \le k" style="vertical-align:-20%;" class="tex" alt="\left| \pi(i) - i \right| \le k" /> for <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e8e7c9058db45694b72005d4f4b41bb0.png" title="i \in [1,n]" style="vertical-align:-20%;" class="tex" alt="i \in [1,n]" /> and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_98d86ac06986d02b6e40f483093b61e0.png" title="k" style="vertical-align:-20%;" class="tex" alt="k" /> is an arbitrary positive integer?</q></p>
<h3>Let&#8217;s not start so arbitrary just yet though&#8230;</h3>
<p>Let&#8217;s just look for a moment at how much trickier things become when we let <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e6a29db9de076157e600ff201406e059.png" title="k = 2" style="vertical-align:-20%;" class="tex" alt="k = 2" />.  It&#8217;s easy enough for <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_63d46ba0a78b01e8ff176ed16ce409ae.png" title="n = 1" style="vertical-align:-20%;" class="tex" alt="n = 1" />.  There&#8217;s only one shuffle possible and it satisfies the constraint.  For that matter, the same is (conventionally) true for <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_401e3786eabc9ae0c19764a05040a35e.png" title="n = 0" style="vertical-align:-20%;" class="tex" alt="n = 0" />.  When you have zero items, there is only one (very Zen) way to shuffle them, and it certainly does not violate the constraints.</p>
<p>For <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_15a5e27cb7797287a56125149455e6d0.png" title="n = 2" style="vertical-align:-20%;" class="tex" alt="n = 2" />, there are two shuffles.  Both satisfy the constraints.  Similarly, with <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_57adc49a1855f637d336bb87f20451ff.png" title="n = 3" style="vertical-align:-20%;" class="tex" alt="n = 3" />, there are <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_0f284b9717afa2fc3d73feb2507df8e9.png" title="3! = 6" style="vertical-align:-20%;" class="tex" alt="3! = 6" /> shuffles.  All of them satisfy the constraints.</p>
<p>For <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_ff17260025ecb747696ef4b00e0dfd8b.png" title="n = 4" style="vertical-align:-20%;" class="tex" alt="n = 4" />, we get to the first interesting case.  Here, some of the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_27b0f6cfd9c75f6235cdc9ceeeb5f5ff.png" title=" 4! = 24 " style="vertical-align:-20%;" class="tex" alt=" 4! = 24 " /> shuffles violate the constraints.  So, how many shuffles are still possible?  To tackle this, we are going to use an <a href="http://en.wikipedia.org/wiki/Inclusion-exclusion_principle">inclusion-exclusion</a> counting.  The only items that could be out of place are the first and the last.  So, how many of the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_fb15860410c3c88df6c2ffb26dd8ea34.png" title=" 4! " style="vertical-align:-20%;" class="tex" alt=" 4! " /> shuffles have either the first element move to the back or the last element move to the front?</p>
<p>There are <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_dede0797e910bf3bafdde772dd82bfc9.png" title=" 3! " style="vertical-align:-20%;" class="tex" alt=" 3! " /> permutations that send the first element to the back.  There are <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_dede0797e910bf3bafdde772dd82bfc9.png" title=" 3! " style="vertical-align:-20%;" class="tex" alt=" 3! " /> permutations that send the last element to the front.  We might then think that there are <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_f1345b8099a3e44b970e9534f11d1694.png" title=" 4! - 2\cdot 3! " style="vertical-align:-20%;" class="tex" alt=" 4! - 2\cdot 3! " /> valid shuffles.  Alas, there are some shuffles that move both the first element to the back and the last element to the front.  We have counted those twice.  There are <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_7e12d4921e8fdcd3a3caf34ec399ca47.png" title=" 2! = 2" style="vertical-align:-20%;" class="tex" alt=" 2! = 2" /> of those.</p>
<p>So, for <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_ff17260025ecb747696ef4b00e0dfd8b.png" title="n = 4" style="vertical-align:-20%;" class="tex" alt="n = 4" />, there are <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_aa6ecb4d3355c6192df380a00633c6d7.png" title=" 4! - 2\cdot 3! + 2! = 24 - 12 + 2 = 14" style="vertical-align:-20%;" class="tex" alt=" 4! - 2\cdot 3! + 2! = 24 - 12 + 2 = 14" /> shuffles satisfying our constraint that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e69aa45a64575b9175a80e0f3e183c73.png" title="\left| \pi(i) - i \right| \le 2" style="vertical-align:-20%;" class="tex" alt="\left| \pi(i) - i \right| \le 2" />.</p>
<p>What then of <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_fdff41d5733ef6dab92131c8eebc1a38.png" title="n = 5" style="vertical-align:-20%;" class="tex" alt="n = 5" />?  Well, it&#8217;s bedtime here.  This is gonna have to wait.</p>
<p>Goodnight.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/06/a-recurrence-of-constrained-shuffles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I Discovered The Fibonacci Numbers</title>
		<link>http://nklein.com/2009/05/i-discovered-the-fibonacci-numbers/</link>
		<comments>http://nklein.com/2009/05/i-discovered-the-fibonacci-numbers/#comments</comments>
		<pubDate>Sun, 31 May 2009 06:19:22 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[game design]]></category>
		<category><![CDATA[recurrence relations]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=469</guid>
		<description><![CDATA[Alright, I wasn&#8217;t the first. But, I wasn&#8217;t particularly expecting to find them either. One nascent game concept that I have in my head requires me to do some constrained shuffling. As an example constraint, imagine that you need to shuffle a deck of cards while ensuring that no card moved more than five spots [...]]]></description>
			<content:encoded><![CDATA[<p>Alright, I wasn&#8217;t the first.  But, I wasn&#8217;t particularly expecting to find them either.</p>
<p>One nascent game concept that I have in my head requires me to do some constrained shuffling.  As an example constraint, imagine that you need to shuffle a deck of cards while ensuring that no card moved more than five spots away from where it started.  Or, imagine that you had to shuffle the deck so that a card of rank <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_de3adc820dbc4655c45b5555765fe84b.png" title="r" style="vertical-align:-20%;" class="tex" alt="r" /> could only move to spot that had been held by a card of rank <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_07981036a9403915ca58421b5035d3a4.png" title="r-1" style="vertical-align:-20%;" class="tex" alt="r-1" />, <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_de3adc820dbc4655c45b5555765fe84b.png" title="r" style="vertical-align:-20%;" class="tex" alt="r" />, or <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_0320f7d96f69a29b9d070d6e6203d522.png" title="r+1" style="vertical-align:-20%;" class="tex" alt="r+1" />.</p>
<p>In the general case, you have a directed graph with vertexes <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e4144550fde1519669847e2542ddb78.png" title="\left\{ v_1, v_2, v_3, \ldots, v_n \right\}" style="vertical-align:-20%;" class="tex" alt="\left\{ v_1, v_2, v_3, \ldots, v_n \right\}" /> and a permutation <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_45e9944b76bbb5b9e0dc513417fadf63.png" title="\pi" style="vertical-align:-20%;" class="tex" alt="\pi" /> such that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_6dbb0f5b8fd6dc5ebabc4b018ba28420.png" title="\pi(i)" style="vertical-align:-20%;" class="tex" alt="\pi(i)" /> can only equal <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_33c9ab784156bc3b75c894b91ea4ecf8.png" title="j" style="vertical-align:-20%;" class="tex" alt="j" /> if there is an edge in the graph from <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_ad2c9ff366d76710bea05a69e6c3a857.png" title="v_i" style="vertical-align:-20%;" class="tex" alt="v_i" /> to <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e3728333578d04c1d0065fcbc6f3187.png" title="v_j" style="vertical-align:-20%;" class="tex" alt="v_j" />.</p>
<p>Well, as this is for a game, it would get pretty boring if there were only a few possible shuffles.  On the other hand, if there are a large number of possible shuffles, then I haven&#8217;t really restricted the shuffling at all for all practical purposes.  So, then the question is:  for a given directed graph, how many possible shuffles are there?  Can I generate them without generating any of the invalid permutations, too?</p>
<p>Today, I started with the <q>How many?</q>-question.  I started with a very simple graph.  The vertexes are spaced evenly along an east-west line.  Each vertex is connected only to itself, its nearest neighbor to the east, and its nearest neighbor to the west.  Put another way, <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_186426730ead7ce2dac6ae5849e841e9.png" title="\left| \pi(i) - i \right| \le 1" style="vertical-align:-20%;" class="tex" alt="\left| \pi(i) - i \right| \le 1" /> for <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e8e7c9058db45694b72005d4f4b41bb0.png" title="i \in [1,n]" style="vertical-align:-20%;" class="tex" alt="i \in [1,n]" />.</p>
<p>Let&#8217;s say we already had a row of <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4782f2b0264558004f0824db5700cf6d.png" title="n-1" style="vertical-align:-20%;" class="tex" alt="n-1" /> vertexes.  If we add another vertex at the beginning of the row, then there are two things that a shuffle can do to that new vertex:  leave it where it is or swap it with what had been the first vertex in the row.  Now, suppose that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_53316fc356252c4cc4c536e64db68f2c.png" title="c(n)" style="vertical-align:-20%;" class="tex" alt="c(n)" /> is the count of valid shuffles for <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> vertexes in this configuration.  The case where our new vertex stays where it is leaves <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_fa393d0c91ed7fde639a1aedc30f4381.png" title="c(n-1)" style="vertical-align:-20%;" class="tex" alt="c(n-1)" /> possibilities for the rest of the row.  The case where our new vertex swaps places with the old first vertex leaves <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_092d8f19093c611fb58af36f06cf78f3.png" title="c(n-2)" style="vertical-align:-20%;" class="tex" alt="c(n-2)" /> possibilities.  As such, <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_1503d16977135f2e1a8c55f6f0131b6d.png" title="c(n) = c(n-1) + c(n-2)" style="vertical-align:-20%;" class="tex" alt="c(n) = c(n-1) + c(n-2)" />.</p>
<p>Cha-ching.  Note that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_1182f6f5e262beb08f3cc2a73fae5b6a.png" title="c(1) = 1" style="vertical-align:-20%;" class="tex" alt="c(1) = 1" /> and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5601b17e73c45f0a3335669744079a38.png" title="c(2) = 2" style="vertical-align:-20%;" class="tex" alt="c(2) = 2" />, then <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_9fd81997a1905e65456c3a87a0085178.png" title="c(n) = F_{n+1}" style="vertical-align:-20%;" class="tex" alt="c(n) = F_{n+1}" /> where <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_6bcb9f483df51a4c41c69caa991e3e4c.png" title="F_i" style="vertical-align:-20%;" class="tex" alt="F_i" /> are the <a href="http://en.wikipedia.org/wiki/Fibonacci_number">Fibonacci numbers</a>.</p>
<p>Things get a great deal messier if we move up to <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e69aa45a64575b9175a80e0f3e183c73.png" title="\left| \pi(i) - i \right| \le 2" style="vertical-align:-20%;" class="tex" alt="\left| \pi(i) - i \right| \le 2" />.  More on that some other time&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/05/i-discovered-the-fibonacci-numbers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Make a Weighted Random Choice</title>
		<link>http://nklein.com/2009/05/how-to-make-a-weighted-random-choice/</link>
		<comments>http://nklein.com/2009/05/how-to-make-a-weighted-random-choice/#comments</comments>
		<pubDate>Wed, 06 May 2009 03:27:15 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[game design]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=280</guid>
		<description><![CDATA[Today seemed like a dull day at the keyboard. I beat my head against Objective C a lot. And, I implemented a weighted random choice which I have done a thousand times before. Dull, right? I needed a weighted random choice in two different places, and I implemented it differently in each place. Weird, right? [...]]]></description>
			<content:encoded><![CDATA[<p>Today seemed like a dull day at the keyboard.  I beat my head against Objective C a lot.  And, I implemented a weighted random choice which I have done a thousand times before.  Dull, right?</p>
<p>I needed a weighted random choice in two different places, and I implemented it differently in each place.  Weird, right?  Even weirder, I think I did the right thing in each place.</p>
<h3>Picking a card from a deck</h3>
<p>In the first case, I needed to choose a flash card from a deck.  I want to favor cards that have given the player trouble in the past.  I zip through the list once to sum up the weights.  Then, I pick a random number greater or equal to zero and less than the sum of the weights.  Then, I run through the list again summing the weights until my running sum exceeds my random number.  (Here in Perl, um, for clarity?):</p>
<div class="codecolorer-container perl blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$total</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$item</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">@list_of_items</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$total</span> <span style="color: #339933;">+=</span> weight<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$item</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$thresh</span> <span style="color: #339933;">=</span> <span style="color: #000066;">rand</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$total</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$found</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$item</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">@list_of_items</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$thresh</span> <span style="color: #339933;">-=</span> weight<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$item</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$thresh</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$found</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$item</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">last</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div>
<p>This requires that the <em>weight()</em> of a given item remains constant for the duration of the function.  The <em>weight()</em> can change between choices, however.  If the <em>weight()</em> will remain constant for large portions of the program then you can cache the total and only bother with the second half of the loop.</p>
<h3>Choosing a letter of the alphabet</h3>
<p>In a different portion of the program, I need to make random letter tiles.  I feel like there should be more <em>E</em>&#8216;s than <em>Q</em>&#8216;s.  I also want to leave a pathway to use the same artwork for different languages.  Because of all of this, I opted to have the weights to come in from a file.</p>
<p>My data file is <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> encoded.  I could, I suppose, do something like this (given the <a href="http://en.wikipedia.org/wiki/Letter_frequencies">English letter frequencies</a>):</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">{
    &quot;english&quot; : {
        &quot;A&quot;: 8.167,
        &quot;B&quot;: 1.492,
        &quot;C&quot;: 2.782,
        ...
    }
}</pre></div>
<p>But, that hurts to even think about.  Instead, I opted to round them off to integer proportions.  Then, I could just do this:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">{
    &quot;english&quot;: &quot;AAAAAAAABBCCC...&quot;
}</pre></div>
<p>Now, making a random choice simply involves selecting one character from the string.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/05/how-to-make-a-weighted-random-choice/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Exponential Spirals for Game Effects</title>
		<link>http://nklein.com/2009/05/exponential-spirals-for-game-effects/</link>
		<comments>http://nklein.com/2009/05/exponential-spirals-for-game-effects/#comments</comments>
		<pubDate>Mon, 04 May 2009 17:51:31 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[game design]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=265</guid>
		<description><![CDATA[In earlier posts, I mentioned finding polynomials, riffing off of damped harmonic motion, and then hitting on exponential spirals all trying to come up with a nice looking way to snap game tiles back into place when they are released. I want them to overshoot and then settle into place rather than snap directly into [...]]]></description>
			<content:encoded><![CDATA[<p>In earlier posts, I mentioned <a href="http://nklein.com/2009/04/find-the-polynomial-youve-been-looking-for/">finding polynomials</a>, <a href="http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/">riffing off of damped harmonic motion</a>, and then <a href="http://nklein.com/2009/05/a-eureka-moment/">hitting on exponential spirals</a> all trying to come up with a nice looking way to snap game tiles back into place when they are released.  I want them to overshoot and then settle into place rather than snap directly into their spot.</p>
<h3>The Basic Spiral</h3>
<p>I am talking about a simple spiral of the form:<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5c7a5dfe252b1e1d04ae968831814d30.png" title="\theta(t) = Kt" style="vertical-align:-20%;" class="tex" alt="\theta(t) = Kt" /></center> <center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_8fcd17db24f8e548df04d4249cc15894.png" title="r(t) = \alpha\left(Kt\right)^{n}" style="vertical-align:-20%;" class="tex" alt="r(t) = \alpha\left(Kt\right)^{n}" /></center> for some integer <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" />.</p>
<p>That would be an equation for a spiral that starts at the origin and heads outward as <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> increases.  For my application though, I want to end at the origin so I need to substitute <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_6c9b1142f71b0b5a6b24c00b99c8f1a5.png" title="1-t" style="vertical-align:-20%;" class="tex" alt="1-t" /> in for <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" />.  <center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_c08f3589a4375d6b7976b2258bab8abc.png" title="\theta(t) = K(1-t)" style="vertical-align:-20%;" class="tex" alt="\theta(t) = K(1-t)" /></center> <center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_8fd9e991927911824d5cbc68b33e46f8.png" title="r(t) = \alpha\left(K(1-t)\right)^{n}" style="vertical-align:-20%;" class="tex" alt="r(t) = \alpha\left(K(1-t)\right)^{n}" /></center></p>
<p>The math is also going to work out slightly better if I use <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_d51a208cc9d5f74919f9a5d0ca1e09ac.png" title="n-2" style="vertical-align:-20%;" class="tex" alt="n-2" /> in place of <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> in the above equations: <center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_c08f3589a4375d6b7976b2258bab8abc.png" title="\theta(t) = K(1-t)" style="vertical-align:-20%;" class="tex" alt="\theta(t) = K(1-t)" /></center> <center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_69c047e0a9e6eb9696354cebcadb18dc.png" title="r(t) = \alpha\left(K(1-t)\right)^{n-2}" style="vertical-align:-20%;" class="tex" alt="r(t) = \alpha\left(K(1-t)\right)^{n-2}" /></center></p>
<p>I don&#8217;t want the piece to spiral into place when released though.  So, really, I am concerned with just the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_1a507c5494969dc6de305770cadc6630.png" title="x" style="vertical-align:-20%;" class="tex" alt="x" /> coordinate from the above equations: <center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5e7ac3bf4d7efe0da6ec76be9e9bafcc.png" title="x(t) = r(t) \cos \theta(t) = \alpha K^{n-2} (1-t)^{n-2} \cos \left(K\left(1-t\right)\right)" style="vertical-align:-20%;" class="tex" alt="x(t) = r(t) \cos \theta(t) = \alpha K^{n-2} (1-t)^{n-2} \cos \left(K\left(1-t\right)\right)" /></center></p>
<p>To normalize everything, I am going to let <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_0e5e44c9a79f0a02bb2f353dc1e46d1a.png" title="\alpha = K^{2-n}" style="vertical-align:-20%;" class="tex" alt="\alpha = K^{2-n}" />.  And, since I want my interpolation value to go from zero to one instead of one to zero, I am again going to subtract this all from one: <center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_2195d2979907e1d41edc2160ee9e4aeb.png" title="x(t) = 1 -  (1-t)^{n-2} \cos \left(K\left(1-t\right)\right)" style="vertical-align:-20%;" class="tex" alt="x(t) = 1 -  (1-t)^{n-2} \cos \left(K\left(1-t\right)\right)" /></center></p>
<p><span id="more-265"></span></p>
<h3>Parametrizing by Arc Length</h3>
<p>If I just plot the spiral as is, then I am stuck with the same problem that I had with damped spring motion:  the frequency is constant.  The rate at which it would shimmy would not increase.  I want it to really settle into place.  So, I have to walk through the spiral at a fixed rate.  I need to rescale the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_ffb09132255189b1b96b1e35156ebac1.png" title="(1-t)" style="vertical-align:-20%;" class="tex" alt="(1-t)" /> into something else so that for any given time interval, I cover the same arc length on the spiral.</p>
<p>The first step then is to figure out how much arc length I sweep out with any given <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" />.  Call this arc length <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_91c8733fdad1e8960345a6ed7c2876fc.png" title="s(t)" style="vertical-align:-20%;" class="tex" alt="s(t)" />: <center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_6dc70cc2b7891d7417847ba6f0bc877e.png" title="s(t) = \int_0^{t} r(\tau) \theta(\tau) d\theta = \int_0^{t} - \alpha K^{n} (1 - \tau)^{n-1} d\tau" style="vertical-align:-20%;" class="tex" alt="s(t) = \int_0^{t} r(\tau) \theta(\tau) d\theta = \int_0^{t} - \alpha K^{n} (1 - \tau)^{n-1} d\tau" /></center> which comes out to: <center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_54c0b6811ba2868b7342943cbee8bad2.png" title="s(t) = \frac{\alpha K^n}{n} \left(\left(1-t\right)^n-1\right)" style="vertical-align:-20%;" class="tex" alt="s(t) = \frac{\alpha K^n}{n} \left(\left(1-t\right)^n-1\right)" /></center></p>
<p>Then, to rescale my <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" />, I want to use <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_83d0feb46e59fb31e04c28f080b85af2.png" title="t'" style="vertical-align:-20%;" class="tex" alt="t'" /> instead so that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_813a51472b6d24723a45e35a2807e18e.png" title="t' = \frac{s(t)}{s(1)}" style="vertical-align:-20%;" class="tex" alt="t' = \frac{s(t)}{s(1)}" />.  So, when <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_029c003e4631eb7982d298b06bef9e36.png" title="t' = \frac{1}{4}" style="vertical-align:-20%;" class="tex" alt="t' = \frac{1}{4}" />, for example, I will need to find the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> such that the arc length covered in the first <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> seconds is <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_c78400c32272b1ce00c8785c0d99408d.png" title="\frac{1}{4}" style="vertical-align:-20%;" class="tex" alt="\frac{1}{4}" />-th of the arc length covered in the whole interval.  Solving for <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> in the equation <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_813a51472b6d24723a45e35a2807e18e.png" title="t' = \frac{s(t)}{s(1)}" style="vertical-align:-20%;" class="tex" alt="t' = \frac{s(t)}{s(1)}" /> comes out to:  <center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_ca375f933602b93fc0bdef182df31c78.png" title="t = 1 - \sqrt[n]{1-t'}" style="vertical-align:-20%;" class="tex" alt="t = 1 - \sqrt[n]{1-t'}" /></center></p>
<p>Plugging all of that back into the equation for <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_2980c069e2e147070875ec659bb90764.png" title="x(t)" style="vertical-align:-20%;" class="tex" alt="x(t)" /> gives me:<br />
<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_52e53acfaac85d56299824d890c35f2c.png" title="x(t) = 1 - \sqrt[n]{(1-t)^{n-2}} \cos \left(K\sqrt[n]{1-t}\right)" style="vertical-align:-20%;" class="tex" alt="x(t) = 1 - \sqrt[n]{(1-t)^{n-2}} \cos \left(K\sqrt[n]{1-t}\right)" /></center><br />
The simplest spiral here has <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_57adc49a1855f637d336bb87f20451ff.png" title="n = 3" style="vertical-align:-20%;" class="tex" alt="n = 3" />, and we will go around four times with <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_630e308a4470b6fbbde955e475a3b403.png" title="K = 8\pi" style="vertical-align:-20%;" class="tex" alt="K = 8\pi" />:  <center><a href="http://nklein.com/wp-content/uploads/2009/05/simple-spiral.png"><img src="http://nklein.com/wp-content/uploads/2009/05/simple-spiral.png" alt="simple-spiral" title="simple-spiral" width="480" height="320" class="alignnone size-full wp-image-266" /></a></center></p>
<p>You can see how the period speeds up toward the end.  This was a good starting point.  However, the first oscillation goes almost as far beyond the target as our initial point was.  So, I upped the exponent to <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_ff961597b47bca30f2bc0d971a7fba65.png" title="n = 9.5" style="vertical-align:-20%;" class="tex" alt="n = 9.5" />:  <center><a href="http://nklein.com/wp-content/uploads/2009/05/higher-order-spiral.png"><img src="http://nklein.com/wp-content/uploads/2009/05/higher-order-spiral.png" alt="higher-order-spiral" title="higher-order-spiral" width="480" height="320" class="alignnone size-full wp-image-267" /></a></center></p>
<p>This was pretty much the effect I wanted.  Unfortunately, it involves taking an <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_b09ede879256f79ac633ff419d6ad90e.png" title="7.5" style="vertical-align:-20%;" class="tex" alt="7.5" />-th power, an <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_89a491e439f20085a37a460884f22858.png" title="9.5" style="vertical-align:-20%;" class="tex" alt="9.5" />-th root, and a cosine of calculation.  I decided that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_296cffcf7e9622b92396b77dae19c81f.png" title="\frac{7.5}{9.5}" style="vertical-align:-20%;" class="tex" alt="\frac{7.5}{9.5}" /> was close enough to one to give it a whirl without needing the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_b09ede879256f79ac633ff419d6ad90e.png" title="7.5" style="vertical-align:-20%;" class="tex" alt="7.5" />-th power.  <center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_b7198479b4f8171c1f2b53bc85119d8d.png" title="x(t) = 1 - (1-t) \cos \left(K\sqrt[n]{1-t}\right)" style="vertical-align:-20%;" class="tex" alt="x(t) = 1 - (1-t) \cos \left(K\sqrt[n]{1-t}\right)" /></center></p>
<p>Here you can see <a href="http://nklein.com/wp-content/uploads/2009/05/tile-slide.mov">the final result</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/05/exponential-spirals-for-game-effects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://nklein.com/wp-content/uploads/2009/05/tile-slide.mov" length="1334665" type="video/quicktime" />
		</item>
		<item>
		<title>A Eureka Moment</title>
		<link>http://nklein.com/2009/05/a-eureka-moment/</link>
		<comments>http://nklein.com/2009/05/a-eureka-moment/#comments</comments>
		<pubDate>Fri, 01 May 2009 17:23:02 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[game design]]></category>
		<category><![CDATA[spell-it]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=254</guid>
		<description><![CDATA[I was pondering the Phony Physics again as I set to work on my iPhone app. In the previous post, I twiddled the equations for damped spring motion until I found something visually pleasing. Last night, I went back to the drawing (a.k.a. white) board. What if I used an exponential spiral (parameterized by arc [...]]]></description>
			<content:encoded><![CDATA[<p>I was pondering the <a href="http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/">Phony Physics</a> again as I set to work on my <a href="http://nklein.com/2009/05/sneak-peak-artwork-for-spelling-iphone-app/">iPhone app</a>.</p>
<p>In the previous post, I twiddled the equations for damped spring motion until I found something visually pleasing.  Last night, I went back to the drawing (a.k.a. <q>white</q>) board.</p>
<p>What if I used an exponential spiral (parameterized by arc length).  Then, I could easily adjust the number of times it bounces back and forth.  I could use a spiral like <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_423e44d60380dbc2ae4d5767f351d4fd.png" title="r(t) = \alpha K (1-t)^n" style="vertical-align:-20%;" class="tex" alt="r(t) = \alpha K (1-t)^n" /> and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_c08f3589a4375d6b7976b2258bab8abc.png" title="\theta(t) = K(1-t)" style="vertical-align:-20%;" class="tex" alt="\theta(t) = K(1-t)" /> where <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_7989e0ac09d9b7e9e3b541e36a5cb514.png" title="K" style="vertical-align:-20%;" class="tex" alt="K" /> is some multiple of <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_2865ca0c5331aac3fceea41c25b3f97f.png" title="2\pi" style="vertical-align:-20%;" class="tex" alt="2\pi" />.  Then, I could walk along the spiral getting to the origin when <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4eea309dec2e2b71e1f70b7a4895f3ac.png" title="t = 1" style="vertical-align:-20%;" class="tex" alt="t = 1" /> going around the origin <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_0f8b4dccb0d9958e9165026a8b86e493.png" title="\frac{K}{2\pi}" style="vertical-align:-20%;" class="tex" alt="\frac{K}{2\pi}" /> times in the process.  If I follow the curve at a fixed rate, then I guarantee that my oscillations will pick up speed as I approach the origin.</p>
<p>Rather than have it spiral into the center, I am just using the x-coordinate of the spiral as my new <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> value to interpolate with.  I like the effect for the most part.  It overshoots a little bit far on the first oscillation.  I may tweak it some more before it&#8217;s all over.  For now though, I am sticking with it.</p>
<p>I will post some graphs soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/05/a-eureka-moment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Phony Physics (a.k.a. Fun with Interpolation)</title>
		<link>http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/</link>
		<comments>http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 15:23:07 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[game design]]></category>
		<category><![CDATA[polynomials]]></category>
		<category><![CDATA[spell-it]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=187</guid>
		<description><![CDATA[In a previous post, I mentioned looking for a polynomial for an application. I am working on an application that involves clicking or dragging tiles around. Once you release the tile, I want it to snap to where it&#8217;s supposed to be. The easiest way to do this is to just warp it to where [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://nklein.com/2009/04/find-the-polynomial-youve-been-looking-for/">previous post</a>, I mentioned looking for a polynomial for an application.  I am working on an application that involves clicking or dragging tiles around. Once you release the tile, I want it to snap to where it&#8217;s supposed to be.</p>
<p>The easiest way to do this is to just warp it to where it&#8217;s supposed to go.  This isn&#8217;t very visually pleasing.  The next best thing is to set up a time-interval on which it moves into place.  Then, linearly interpolate from where it is to where it&#8217;s going (here with <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> normalized to range between zero and one):</p>
<p><center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_bc65e9f2a636dba26b596808b7606619.png" title="(1-t)x_0 + tx_1" style="vertical-align:-20%;" class="tex" alt="(1-t)x_0 + tx_1" /></center></p>
<p>That&#8217;s much better than just warping, but it doesn&#8217;t have any sort of fade-in or fade-out.  It instantaneously has its maximum velocity and then instantaneously stops at the end.</p>
<p>Typically, then one uses a cubic spline to scale the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> value before interpolating to get a speed up in the beginning and then slow down in the end.<br />
<span id="more-187"></span></p>
<p><center><a href="http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/attachment/3/" rel="attachment wp-att-190"><img src="http://nklein.com/wp-content/uploads/2009/04/3.png" alt="interp-3" title="interp-3" width="362" height="332" class="size-full wp-image-190" /></a></center><br />
<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_192613d43790f7f6d1a03b285815852a.png" title="t' = 3t^2 - 2t^3" style="vertical-align:-20%;" class="tex" alt="t' = 3t^2 - 2t^3" /></center></p>
<p>I might like to decelerate faster than I accelerate though so I might employ a fancier (higher-order) polynomial like:</p>
<p><center><a href="http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/attachment/4/" rel="attachment wp-att-189"><img src="http://nklein.com/wp-content/uploads/2009/04/4.png" alt="interp-4" title="interp-4" width="362" height="332" class="size-full wp-image-189" /></a></center><br />
<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_56e7a6a5cab5289a0fbe06302ed04c7b.png" title="t' = \frac{1}{2}t^2 + 6t^3 - \frac{17}{2}t^4 + 3x^5" style="vertical-align:-20%;" class="tex" alt="t' = \frac{1}{2}t^2 + 6t^3 - \frac{17}{2}t^4 + 3x^5" /></center></p>
<p>But, in this case, I wanted the tile to overshoot the target a bit and then nestle/wiggle into place.  So, I experimented with a bunch of different polynomials.  I was having real trouble getting them to behave in a way that felt right.  So, I went back to the physics of it.</p>
<p>What I wanted was pretty similar to damped spring motion.  Imagine you&#8217;ve tied a heavy weight to a spring and suspended it all in a jar of oil.  If you perturb the weight, it will bounce up and down but<br />
each bounce is closer to the equilibrium point than the previous bounce. (Actually, it&#8217;s more complicated than that.  If the spring is too weak or the oil too viscous, the weight will just slowly return to the equilibrium point without ever going past it.  That&#8217;s called <q>overdamped</q> harmonic oscillation.  What I want is called <q>underdamped</q> harmonic oscillation and that&#8217;s all I will concern myself with here.)  The equation for this motion is:</p>
<p><center><a href="http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/attachment/1/" rel="attachment wp-att-188"><img src="http://nklein.com/wp-content/uploads/2009/04/1.png" alt="interp-1" title="interp-1" width="362" height="332" class="size-full wp-image-188" /></a></center><br />
<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_183e283439724e2d53461f2d35e45c55.png" title="e^{-kt}\cos(\omega t + \phi)" style="vertical-align:-20%;" class="tex" alt="e^{-kt}\cos(\omega t + \phi)" /></center></p>
<p>To get it to wiggle an appropriate amount, I was using <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_09e765cf1309e741b7d63896b0e5f131.png" title="\omega = 4\pi" style="vertical-align:-20%;" class="tex" alt="\omega = 4\pi" /> and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_0ecb976ae09df37ce3eb04c2e5476ee1.png" title="\phi = -\frac{\pi}{2}" style="vertical-align:-20%;" class="tex" alt="\phi = -\frac{\pi}{2}" /> effectively making the cosine into a sine.</p>
<p>This is all well and good, but to really scale my t, I need this to start at zero and approach one.  So, I was really using:</p>
<p><center><a href="http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/attachment/2/" rel="attachment wp-att-193"><img src="http://nklein.com/wp-content/uploads/2009/04/2.png" alt="interp-2" title="interp-2" width="362" height="332" class="size-full wp-image-193" /></a></center><br />
<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_c6b4c32d153a59887d18aa66851ee824.png" title="t' = t + e^{-kt}\sin(\omega t)" style="vertical-align:-20%;" class="tex" alt="t' = t + e^{-kt}\sin(\omega t)" /></center></p>
<p>Well, now it starts zig-zagging on the way to its destination.  It already starts cutting back long before it makes it to where it&#8217;s going.  I needed to get close to the destination much sooner.  So, I jiggered the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> term:</p>
<p><center><a href="http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/attachment/8/" rel="attachment wp-att-194"><img src="http://nklein.com/wp-content/uploads/2009/04/8.png" alt="interp-8" title="interp-8" width="362" height="332" class="size-full wp-image-194" /></a></center><br />
<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_ea300b9cde964c1f248728a4e8518ef3.png" title="t' = 1 - (1-t)^4 + e^{-kt}\sin(\omega t)" style="vertical-align:-20%;" class="tex" alt="t' = 1 - (1-t)^4 + e^{-kt}\sin(\omega t)" /></center></p>
<p>This looks terrible though.  I hit the destination is less than a tenth of a second and then not again until about half a second.  The reason for this is that the oscillations happen at a fixed rate.  <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_0824a1fdf135170467671326d5780e2b.png" title="\omega" style="vertical-align:-20%;" class="tex" alt="\omega" /> never changes and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> moves steadily along so that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_0ce8d212a10d8619e00e43a97ce75527.png" title="\sin(\omega t)" style="vertical-align:-20%;" class="tex" alt="\sin(\omega t)" /> keeps the same frequency throughout my interval.  So, I jiggered the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> going into the sine function to make my oscillations start out slowly and really speed up toward the end.</p>
<p><center><a href="http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/attachment/5/" rel="attachment wp-att-195"><img src="http://nklein.com/wp-content/uploads/2009/04/5.png" alt="interp-5" title="interp-5" width="362" height="332" class="aligncenter size-full wp-image-195" /></a></center><br />
<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_d206dd1b8a9975dac7a20d2f485d302c.png" title="t' = 1 - (1-t)^4 + e^{-kt}\sin(\omega t^8)" style="vertical-align:-20%;" class="tex" alt="t' = 1 - (1-t)^4 + e^{-kt}\sin(\omega t^8)" /></center></p>
<p>This is much better, but it still didn&#8217;t give the feeling of settling in because the second oscillation was almost as high as the first.  I could up the value of <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_98d86ac06986d02b6e40f483093b61e0.png" title="k" style="vertical-align:-20%;" class="tex" alt="k" /> and get somewhere.  But, I wanted it to be even more dramatic.  So, I changed the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> in the exponent to <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_2dfd71f3d0d6caca184676440dd9f788.png" title="t^3" style="vertical-align:-20%;" class="tex" alt="t^3" /> (and used a slightly higher <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_98d86ac06986d02b6e40f483093b61e0.png" title="k" style="vertical-align:-20%;" class="tex" alt="k" />: was 3 and is now 4):</p>
<p><center><a href="http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/attachment/6/" rel="attachment wp-att-196"><img src="http://nklein.com/wp-content/uploads/2009/04/6.png" alt="interp-6" title="interp-6" width="362" height="332" class="size-full wp-image-196" /></a></center><br />
<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_ab2071d4a434e9ab5ec3479bb94847f3.png" title="t' = 1 - (1-t)^4 + e^{-kt^3}\sin(\omega t^8)" style="vertical-align:-20%;" class="tex" alt="t' = 1 - (1-t)^4 + e^{-kt^3}\sin(\omega t^8)" /></center></p>
<p>This was pretty decent, but I felt the initial attack was a bit strong.  So, I dropped down the exponent on my base curve from 4 to 2:</p>
<p><center><a href="http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/attachment/7/" rel="attachment wp-att-197"><img src="http://nklein.com/wp-content/uploads/2009/04/7.png" alt="interp-7" title="interp-7" width="362" height="332" class="size-full wp-image-197" /></a></center><br />
<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_91b53d4e338118fb129365a6a9871631.png" title="t' = 1 - (1-t)^2 + e^{-kt^3}\sin(\omega t^8)" style="vertical-align:-20%;" class="tex" alt="t' = 1 - (1-t)^2 + e^{-kt^3}\sin(\omega t^8)" /></center></p>
<p>This is pretty nice.  It looks pretty natural (despite making an entire mess out of damped harmonic motion).  It doesn&#8217;t ease in at all though so I might still need to replace the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_2440a4af622544eadc0e1791b4221865.png" title="1 - (1-t)^2" style="vertical-align:-20%;" class="tex" alt="1 - (1-t)^2" /> with something of a higher order.  But, I might not mind the tile achieving instant velocity.  It gives it that <q>I&#8217;m really supposed to be over there</q> sort of urgency.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

