<?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>Cheezeworld &#187; Games</title>
	<atom:link href="http://cheezeworld.com/category/games/feed/" rel="self" type="application/rss+xml" />
	<link>http://cheezeworld.com</link>
	<description>The Cheeze to Your Macaroni - Games, Open Source Programming, and Other Odd Ramblings.</description>
	<lastBuildDate>Mon, 04 Jan 2010 18:23:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Programming Tidbit #3 &#8211; Constraining Rotation to a Max Turn Rate Per Second</title>
		<link>http://cheezeworld.com/constraining-rotation-max-turn-rate/</link>
		<comments>http://cheezeworld.com/constraining-rotation-max-turn-rate/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 14:57:58 +0000</pubDate>
		<dc:creator>Colby Cheeze</dc:creator>
				<category><![CDATA[Games]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Tidbits]]></category>
		<category><![CDATA[actionscript 3]]></category>
		<category><![CDATA[Cannon]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[Rotations]]></category>
		<category><![CDATA[Turn Rate]]></category>
		<category><![CDATA[Turret]]></category>

		<guid isPermaLink="false">http://cheezeworld.com/?p=177</guid>
		<description><![CDATA[
This is the third post in the series “Programming Tidbits.” These articles consist of useful bits of information or code that I would like to share, but are not necesarily full length articles.
Sometimes you may wish to constrain the amount of rotation an entity can rotate per second or tick or whatever, such as the [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-medium wp-image-178" title="d_turret" src="http://cheezeworld.com/wp-content/uploads/2009/01/d_turret-300x225.jpg" alt="d_turret" width="300" height="225" /></p>
<p>This is the third post in the series “Programming Tidbits.” These articles consist of useful bits of information or code that I would like to share, but are not necesarily full length articles.</p>
<p>Sometimes you may wish to constrain the amount of rotation an entity can rotate per second or tick or whatever, such as the turret on a tank or the turning of the tank itself. This can be done with a bit of math. It could seem a bit complicated without using the math libraries I have already created.</p>
<p align="left">
<h4>Here it is using my code:</h4>
<p>We are going to assume the maxTurnRate will be in degrees per second.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> maxTurnRate <span style="color: #66cc66;">&amp;</span>gt; <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&amp;</span>amp;<span style="color: #66cc66;">&amp;</span>amp; currentVelocity.<span style="color: #006600;">angleTo</span><span style="color: #66cc66;">&#40;</span> newVelocity <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;</span>gt; <span style="color: #66cc66;">&#40;</span> maxTurnRate <span style="color: #66cc66;">*</span> MathUtils.<span style="color: #006600;">DEG_TO_RAD</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> stepSize <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
 	<span style="color: #000000; font-weight: bold;">var</span> mat:Matrix2D = <span style="color: #000000; font-weight: bold;">new</span> Matrix2D<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	mat.<span style="color: #006600;">rotate</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span> maxTurnRate <span style="color: #66cc66;">*</span> MathUtils.<span style="color: #006600;">DEG_TO_RAD</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> stepSize <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> heading.<span style="color: #006600;">sign</span><span style="color: #66cc66;">&#40;</span> newVelocity <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
 	mat.<span style="color: #006600;">transformVector</span><span style="color: #66cc66;">&#40;</span> currentVelocity<span style="color: #66cc66;">&#41;</span>;
 	mat.<span style="color: #006600;">transformVector</span><span style="color: #66cc66;">&#40;</span> heading <span style="color: #66cc66;">&#41;</span>;
 <span style="color: #66cc66;">&#125;</span>
 <span style="color: #b1b100;">else</span>
 <span style="color: #66cc66;">&#123;</span>
 	velocity.<span style="color: #006600;">x</span> = _newVel.<span style="color: #006600;">x</span>;
 	velocity.<span style="color: #006600;">y</span> = _newVel.<span style="color: #006600;">y</span>;
 <span style="color: #66cc66;">&#125;</span></pre></div></div>

<h2>The Code Explained</h2>
<p>So let&#8217;s take a look at what is happening there. First, we are checking to see if maxTurnRate is &gt; 0&#8230;because we will assume if it is set as 0 then the entity should be able to turn freely. If it is greater, then we will get the angle from our current velocity and check to see if it is greater than the max turn rate multiplied by the step size. The step size is generally just: tickSize / 1000.</p>
<p>The math to get the angle from one vector to another is:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> angleTo<span style="color: #66cc66;">&#40;</span> vector1:Vector2D, vector2:Vector2D <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">acos</span><span style="color: #66cc66;">&#40;</span> dotProduct<span style="color: #66cc66;">&#40;</span> vector1, vector2 <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #66cc66;">&#40;</span> vector1.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">*</span> vector2.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> dotProduct<span style="color: #66cc66;">&#40;</span> vector1:Vector2D, vector2:Vector2D <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span> vector1.<span style="color: #006600;">x</span> <span style="color: #66cc66;">*</span> vector2.<span style="color: #006600;">x</span> <span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span> vector1.<span style="color: #006600;">y</span> <span style="color: #66cc66;">*</span> vector2.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Next, if the rotation is greater than the max amount aloud&#8230;we will use a matrix to rotate the velocity vector to it&#8217;s correct position and transform it and the heading ( the direction the entity is going ). Note: The &#8220;sign&#8221; function just returns -1 if vector is counterclockwise, else 1 if clockwise. We need this to know which direction to rotate obviously.</p>
<h2>The Matrix Code</h2>
<p>Here is some of the matrix code, though I definitely don&#8217;t plan on explaining matrices in this post. Which is why it&#8217;s best to just use the library if you don&#8217;t actually understand the math part itself. <img src='http://cheezeworld.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**
 * Use to rotate.
 * ( Considers that the Vector object represents a point, not an actual vector )
 * @param rot
 *
 */</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> rotate<span style="color: #66cc66;">&#40;</span>rot:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">sin</span>:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sin</span><span style="color: #66cc66;">&#40;</span>rot<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">cos</span>:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">cos</span><span style="color: #66cc66;">&#40;</span>rot<span style="color: #66cc66;">&#41;</span>;
	multiply<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> Matrix2D<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">cos</span>, <span style="color: #0066CC;">sin</span>, <span style="color: #cc66cc;">0</span>, -<span style="color: #0066CC;">sin</span>, <span style="color: #0066CC;">cos</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/**
 * Use to multiply two Matrixes...mainly used internally.
 * @param matrix The Matrix to multiply by.
 *
 */</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> multiply<span style="color: #66cc66;">&#40;</span>matrix:Matrix2D<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> m:Matrix2D = <span style="color: #000000; font-weight: bold;">new</span> Matrix2D<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//first row</span>
	m.<span style="color: #006600;">a1</span> = <span style="color: #66cc66;">&#40;</span>a1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>a2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>a3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c1</span><span style="color: #66cc66;">&#41;</span>;
	m.<span style="color: #006600;">a2</span> = <span style="color: #66cc66;">&#40;</span>a1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a2</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>a2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b2</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>a3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c2</span><span style="color: #66cc66;">&#41;</span>;
	m.<span style="color: #006600;">a3</span> = <span style="color: #66cc66;">&#40;</span>a1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a3</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>a2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b3</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>a3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c3</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//second row</span>
	m.<span style="color: #006600;">b1</span> = <span style="color: #66cc66;">&#40;</span>b1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c1</span><span style="color: #66cc66;">&#41;</span>;
	m.<span style="color: #006600;">b2</span> = <span style="color: #66cc66;">&#40;</span>b1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a2</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b2</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c2</span><span style="color: #66cc66;">&#41;</span>;
	m.<span style="color: #006600;">b3</span> = <span style="color: #66cc66;">&#40;</span>b1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a3</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b3</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c3</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//third row</span>
	m.<span style="color: #006600;">c1</span> = <span style="color: #66cc66;">&#40;</span>c1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c1</span><span style="color: #66cc66;">&#41;</span>;
	m.<span style="color: #006600;">c2</span> = <span style="color: #66cc66;">&#40;</span>c1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a2</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b2</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c2</span><span style="color: #66cc66;">&#41;</span>;
	m.<span style="color: #006600;">c3</span> = <span style="color: #66cc66;">&#40;</span>c1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a3</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b3</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c3</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #0066CC;">Set</span><span style="color: #66cc66;">&#40;</span>m.<span style="color: #006600;">a1</span>,m.<span style="color: #006600;">a2</span>,m.<span style="color: #006600;">a3</span>,m.<span style="color: #006600;">b1</span>,m.<span style="color: #006600;">b2</span>,m.<span style="color: #006600;">b3</span>,m.<span style="color: #006600;">c1</span>,m.<span style="color: #006600;">c2</span>,m.<span style="color: #006600;">c3</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/**
 * This is used to do the final transformation upon the Vector
 * @param point The Vector to apply this Matrix transformation to.
 *
 */</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> transformVector<span style="color: #66cc66;">&#40;</span>point:Vector2D<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> tempX:<span style="color: #0066CC;">Number</span> = <span style="color: #66cc66;">&#40;</span>a1<span style="color: #66cc66;">*</span>point.<span style="color: #006600;">x</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b1<span style="color: #66cc66;">*</span>point.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c1<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> tempY:<span style="color: #0066CC;">Number</span> = <span style="color: #66cc66;">&#40;</span>a2<span style="color: #66cc66;">*</span>point.<span style="color: #006600;">x</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b2<span style="color: #66cc66;">*</span>point.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c2<span style="color: #66cc66;">&#41;</span>;
	point.<span style="color: #006600;">x</span> = tempX;
	point.<span style="color: #006600;">y</span> = tempY;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h2>And that&#8217;s all!</h2>
<p>So anyways&#8230;I&#8217;ve shown the internal workings of the math code there for you, though the first code block is all that should really matter to you in the end&#8230;since it shows how to actually use the math.</p>
<p>No example today, since I&#8217;m pretty sure you can imagine a turret rotating at a max speed&#8230;lol. However, it will be used in the next <a href="http://cheezeworld.com/game-structure/">Game Structure</a> post for the entities.</p>
]]></content:encoded>
			<wfw:commentRss>http://cheezeworld.com/constraining-rotation-max-turn-rate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Breakball</title>
		<link>http://cheezeworld.com/breakball/</link>
		<comments>http://cheezeworld.com/breakball/#comments</comments>
		<pubDate>Sat, 29 Mar 2008 20:38:01 +0000</pubDate>
		<dc:creator>Colby Cheeze</dc:creator>
				<category><![CDATA[Games]]></category>
		<category><![CDATA[action]]></category>
		<category><![CDATA[ball]]></category>
		<category><![CDATA[Break Ball]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[free]]></category>
		<category><![CDATA[game]]></category>

		<guid isPermaLink="false">http://cheezeworld.com/games/breakball/</guid>
		<description><![CDATA[Description:
Control your Break Ball through a series of challenging levels. Can you beat the game?
This is the first game that I completed. If you create any of your own custom levels be sure to post a comment with the level code so others can try it!

Instructions:
Use the arrow keys to control the ball.
Press Spacebar to [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Description:</strong><br />
Control your Break Ball through a series of challenging levels. Can you beat the game?<br />
This is the first game that I completed. If you create any of your own custom levels be sure to post a comment with the level code so others can try it!<span id="more-26"></span></p>
<p><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="600" height="400" title="BreakBall"><param name="movie" value="http://www.cheezeworld.com/files/breakball.swf" /><param name="quality" value="high" /><embed src="http://www.cheezeworld.com/files/breakball.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="600" height="400"></embed></object></p>
<p><strong>Instructions:</strong><br />
Use the arrow keys to control the ball.<br />
Press Spacebar to activate the &#8220;Slowclock&#8221; powerup.</p>
]]></content:encoded>
			<wfw:commentRss>http://cheezeworld.com/breakball/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

