<?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>Join My Conversation &#187; Uncategorized</title>
	<atom:link href="http://www.jmckell.com/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jmckell.com</link>
	<description>Conversation and news topics</description>
	<lastBuildDate>Thu, 02 Sep 2010 23:53:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Independent Model Rate</title>
		<link>http://www.jmckell.com/independent-model-rate/</link>
		<comments>http://www.jmckell.com/independent-model-rate/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 18:47:45 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.heathersych.com/?p=87</guid>
		<description><![CDATA[Independent Model Rate
This section describes a method of programming incremental          animation that gives two advantages:

frame-rate independence for incremental            animation
high model rates for normal (&#60;100            [...]]]></description>
			<content:encoded><![CDATA[<p>Independent Model Rate<a name="demo"></a></p>
<p>This section describes a method of programming incremental          animation that gives two advantages:</p>
<ul>
<li><strong>frame-rate independence</strong> for incremental            animation</li>
<li><strong>high model rates</strong> for normal (&lt;100            fps) movie frame rates. High model rates make some incremental algorithms            function more accurately.</li>
</ul>
<p>This demo gives the <a href="http://www.jmckell.com/collisionspring.html">Collision          Using Spring Force</a> demo an independent model rate.</p>
<p>          </span></p>
<p><span>The Model</span><br />
The <strong>variables</strong> used in an incremental animation—position,          velocity, etc of a set of objects—along with the<strong> algorithm</strong> that makes them interact is called a <strong>model</strong>. At any point          in time the variables store the<strong> state of the model</strong>.</p>
<p>The model is <strong>separate from what appears on screen</strong>,          which is merely a <strong>visual representation</strong> of the state          of the model. Usually they are so closely related that they might seem          like the same thing. However, the model can execute just fine without          ever showing a representation of it on screen. The position and velocity          variables would <strong>still go through their various values</strong>,          depending on the model algorithm, even if the sprite properties are never          set accordingly.</p>
<p>The act of creating a visual representation of a model          is called <strong>rendering</strong>. To set sprite and cast member properties          based on a model is a form of rendering.</p>
<p><span>Model Rate</span><br />
Each time a model algorithm is executed, updating the state of the model,          it is <strong>one iteration</strong>, or <strong>step</strong>. I&#8217;ll call          this a <strong>&#8220;model frame&#8221;</strong> in keeping with Director          terminology. So the number of model frames per second is the &#8220;model          frame rate&#8221; or just <strong>&#8220;model rate&#8221;</strong>.</p>
<p>So far in the incremental animation demos, the <strong>model          rate has been tied to the movie frame rate</strong> because the <strong>model          algorithm was executed once per movie frame</strong>, in the exitframe          handler.</p>
<p>In this demo those two rates are separated. The script          below shows the form of the standard behavior script with a <strong>slight          modification</strong> made to provide the <strong>rate separation</strong>.</p>
<div><span>global</span> modelRateObj</p>
<p><span>property</span> sp<br />
<span>&#8211; and other properties declared</span></p>
<p><span>on </span><span>beginsprite</span><span>(me</span>)<br />
sp = <span>sprite</span>(<span>me</span>.<span>spritenum</span>)<br />
<span>&#8211; and other variables initialized</span></p>
<p>modelRateObj.addInstance(<span>me</span>)<br />
<span>end</span></p>
<p><span>on</span> modelFrame(<span>me</span>)<br />
<span>&#8211; perform incremental animation          algorithm</span><br />
<span>end</span></p>
<p><span>on</span> <span>exitframe</span>()<br />
sp.<span>loch</span> = x<br />
sp.<span>locv</span> = y<br />
<span> &#8212; and other sprite properties set</span><br />
<span>end</span></p>
</div>
<p><span><strong>Sprite behavior showing separation of movie        and model rates</strong></span></p>
<p>The <strong>primary difference</strong> is that the model          algorithm has been moved out of the exitframe handler and into a &#8216;modelFrame&#8217;          handler. Now the model algorithm can be <strong>executed independent of          the frame rate</strong> of the movie. The only statements left in the          exitframe handler are those that <strong>render</strong> the model.</p>
<p>All that is left to do is <strong>call the modelFrame handler</strong> of each object however many times per second is desired. For correct function          of the collision model it is necessary that the objects&#8217; modelFrame handlers          are called <strong>in step with each other</strong>.</p>
<p>In this demo this is done by a script I&#8217;ve named <strong>modelRate</strong>,          which is <a href="http://www.jmckell.com/glossary.html#instantiate">instantiated </a>when          the movie starts.</p>
<div><span>property</span> modelFPS<br />
<span>property</span> millis<br />
<span>property</span> instanceList</p>
<p><span>on new</span>(<span>me</span>,          mfps)<br />
instanceList = []<br />
modelFPS = mfps<br />
millis = <span>the</span> <span>milliseconds</span><br />
(<span>the</span> <span>actorlist</span>).<span>append</span>(<span>me</span>)<br />
<span> return me</span><br />
<span>end</span></p>
<p><span>on</span> <span>stepframe</span>()<br />
elapsed = <span>the</span> <span>milliseconds</span> &#8211; millis<br />
mframes = <span>integer</span>(modelFPS          * (elapsed /<span> 1000.0</span>))<br />
doModelFrames(mframes)<br />
<span> if</span> modelFPS &gt; <span>0</span> <span>then</span><br />
millis = millis + (mframes / <span>float</span>(modelFPS))          * <span>1000</span><br />
<span> end if<br />
end</span></p>
<p><span>on</span> doModelFrames(mframes)<br />
<span> repeat with </span>mf = <span>1</span> <span>to</span> mframes<br />
<span> repeat with </span>instance          <span>in</span> instanceList<br />
instance.modelFrame()<br />
<span> end repeat<br />
end repeat<br />
end</span></p>
<p><span>on</span> addInstance(<span>me</span>,          inst)<br />
instanceList.<span>append</span>(inst)<br />
<span>end</span></p>
</div>
<p><span><strong>script &#8220;modelRate&#8221; &#8211; </strong>an        explanation of &#8220;the actorlist&#8221; is given <a href="http://www.jmckell.com/OOfun.html#how">here</a></span></p>
<p>modelRate maintains a <strong>list of script instances</strong>,          and it calls the modelFrame handler of those instances so many times per          second. It is the responsibility of each object in the model to register          itself with modelRate by passing a <strong>reference to itself</strong> to modelRate&#8217;s <strong>addInstance</strong> handler. This was shown in          the behavior script above, with the statement <strong>modelRateObj.addInstance(me)</strong>.</p>
<p>When modelRate is instantiated, the desired model frame          rate is passed to the <strong>new()</strong> handler. After instantiation,          the model frame rate is adjusted by changing modelRate&#8217;s <strong>modelFPS</strong> property.</p>
<p>The function of this modelRate object is very similar to          Director&#8217;s use of<br />
<strong>the actorlist</strong>, except it calls <strong>modelFrame()</strong> so many times per second where <strong>stepframe()</strong> is called          once per frame. modelRate could be written to be <strong>based on the          frame rate</strong> and call modelFrame() so many times per movie frame.</p>
<p>One point about the line <strong>millis = millis + (mframes          / float(modelFPS)) * 1000</strong>. Usually this is just a reset of millis          with <strong>millis = the milliseconds</strong>. The way it is reset here          compensates for the rounding done to get mframes, so the resulting model          rate is correct.<a name="sliders"></a></p>
<p><span>Biased Slider Drivers</span><br />
This part is related to the material in the <a href="http://www.jmckell.com/parametric.html">parametric          animation</a> section.</p>
<p>The sliders in this demo are programmed as <a href="http://www.jmckell.com/drivingp.html">parametric          animation drivers</a>. The parameters they provide are then <a href="http://www.jmckell.com/biasingp.html"><strong>biased</strong></a>,          using a <strong>bias-out</strong> function. Notice that when the model          rate slider sits at the <strong>middle of its range its value is 500</strong>,          even though the slider goes from <strong>0 to 2000</strong>. By biasing          the parameter, finer adjustments can be made at the lower end of the range          (at the expense of adjustment at the high end). For finer adjustment at          the higher end, a <strong>bias-in</strong> function would be used.</p>
<p>There are two scripts attached to each of the sliders.                One provides the driver function <strong>p()</strong>, and the <strong>standard                slider driver code</strong>. The other script gets the value returned                by <strong>p()</strong> and uses it to set properties and variables                <strong>specific to this demo</strong>. This maintains the modularity                of the scripts.</p>
<img src="http://www.jmckell.com/?ak_action=api_record_view&id=87&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jmckell.com/independent-model-rate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Collision Using Spring Force</title>
		<link>http://www.jmckell.com/collision-using-spring-force/</link>
		<comments>http://www.jmckell.com/collision-using-spring-force/#comments</comments>
		<pubDate>Sun, 23 May 2010 00:11:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.heathersych.com/?p=89</guid>
		<description><![CDATA[Collision Using Spring Force
When two objects rebound after colliding with each other,          it is due to a spring force, even though it may not look          like it. When two billiard balls collide, the compression of the ball [...]]]></description>
			<content:encoded><![CDATA[<p>Collision Using Spring Force</p>
<p>When two objects rebound after colliding with each other,          it is due to a <strong>spring force</strong>, even though it may not look          like it. When two billiard balls collide, the compression of the ball          is limited to a miniscule fraction of its width but it still happens.</p>
<p>Using a spring force requires <strong>higher model rates</strong> to realistically collide faster, harder objects. The <strong>fps</strong> display in the demo shows the <strong>actual frame rate</strong>. The          <a name="demo"></a> movie itself is set to 999 fps. On a 1.3 GHz P3 it          averages about 980 actual fps.</p>
<p><span>repeat with </span>s = <span>1</span> <span>to</span> <span>4</span><br />
<span>if</span> s &lt;&gt; <span>me</span>.spritenum          <span>then</span><br />
spOther = <span>sprite</span>(s)</p>
<p><span>&#8211;distance components</span><br />
distX = (spOther.x) &#8211; x<br />
distY = (spOther.y) &#8211; y</p>
<p><span>&#8211;pythagorean theorem          to get distance</span><br />
dist = <span>sqrt</span>(<span>power</span>(distX,<span>2</span>)          + <span>power</span>(distY,<span>2</span>))</p>
<p><span>&#8211;spring force</span><br />
xSpring = <span>0</span><br />
ySpring = <span>0</span><br />
minDist = (sp.<span>width</span> + spOther.<span>width</span>) / <span>2.0</span> -<span> 10</span><br />
<span>if</span> dist &lt;          minDist <span>then</span><br />
springF = (minDist &#8211; dist) * <span>.02</span><br />
xSpring = springF * -(distX/dist)<br />
ySpring = springF * -(distY/dist)<br />
<span>end if</span></p>
<p>xTotalForce = xTotalForce + xSpring<br />
yTotalForce = yTotalForce + ySpring<br />
<span> end if<br />
end repeat </span></p>
<p>The algorithm is <strong>almost identical to <a href="http://www.jmckell.com/generalgravity.html#demo2">general          gravity</a></strong>, so only the repeat loop is shown. Instead of calculating          gravity, it calculates spring force. Spring force only acts when the objects          are within a certain distance of each other, expressed as <strong>dist          &lt; minDist</strong>.</p>
<p>The magnitude of the spring force is given by <strong>minDist          &#8211; dist</strong>. This is the <strong>restPosition &#8211; position </strong>equation          from the <a href="http://www.jmckell.com/springforces.html">Spring Forces</a> section. <strong>Scaling          by .02</strong> puts the force into proportion with other values in the          animation. It can be considered the <strong>stiffness of the object</strong>,          the higher it is the stiffer the object.</p>
<p>This demo gives each object a <strong><a href="http://www.jmckell.com/mass.html">mass</a></strong>,          and uses the mass in the<br />
<strong>accel = force / mass</strong> equation. The behavior of the large          object shows how mass affects acceleration. When two objects collide,          each experiences the same amount of force, acting in opposite directions.          This force translates into a <strong>smaller acceleration for more massive          objects</strong>.</p>
<p>Mass can be set however you&#8217;d like. In this demo it is          set to the cube of the sprite width, which would roughly correspond to          its mass if it was a<br />
3-dimensional sphere.</p>
<p>The type of collision modelled in this demo is for <strong>round          objects with no friction</strong> between them. Friction or non-round          shape would cause part of the energy in the collision to go into rotational          velocity, making the objects spin.</p>
<p>For comments on the rest of the script, see <a href="http://www.jmckell.com/generalgravity.html">General                Gravity</a>.</p>
<img src="http://www.jmckell.com/?ak_action=api_record_view&id=89&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jmckell.com/collision-using-spring-force/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
