<?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>Video Game Math</title>
	<atom:link href="http://gamemath.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://gamemath.com</link>
	<description>Homepage of 3D Math Primer for Graphics and Game Development</description>
	<lastBuildDate>Mon, 21 Nov 2011 17:14:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Powerpoint Presentation Slides Posted</title>
		<link>http://gamemath.com/2011/11/powerpoint-presentation-slides-posted/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=powerpoint-presentation-slides-posted</link>
		<comments>http://gamemath.com/2011/11/powerpoint-presentation-slides-posted/#comments</comments>
		<pubDate>Sun, 20 Nov 2011 18:24:32 +0000</pubDate>
		<dc:creator>Fletcher</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[3D Math Primer]]></category>
		<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://gamemath.com/?p=260</guid>
		<description><![CDATA[The powerpoint presentation slides are now available on the download page]]></description>
			<content:encoded><![CDATA[<p><img align=top src="/presentation_slides.png" style="float:right;" /><br />
The powerpoint presentation slides are now available on the <a href="download/">download page</a></p>
]]></content:encoded>
			<wfw:commentRss>http://gamemath.com/2011/11/powerpoint-presentation-slides-posted/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Second Edition Now Available!</title>
		<link>http://gamemath.com/2011/11/second-edition-now-available/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=second-edition-now-available</link>
		<comments>http://gamemath.com/2011/11/second-edition-now-available/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 07:06:14 +0000</pubDate>
		<dc:creator>Fletcher</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[3D Math Primer]]></category>
		<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://gamemath.com/?p=253</guid>
		<description><![CDATA[The second edition is finally back from the printer! Buy it from your local bookstore, or online from amazon.com.]]></description>
			<content:encoded><![CDATA[<p>The second edition is finally back from the printer!  Buy it from your local bookstore, or online from <a href="http://www.amazon.com/Math-Primer-Graphics-Game-Development/dp/1568817231/">amazon.com</a>.</p>
<p><img src="/3d_math_primer_second_edition_released.png" /></p>
]]></content:encoded>
			<wfw:commentRss>http://gamemath.com/2011/11/second-edition-now-available/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Team Fortress 2: Halloween 2011 Update Released</title>
		<link>http://gamemath.com/2011/10/team-fortress-2-halloween-2011-update-released/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=team-fortress-2-halloween-2011-update-released</link>
		<comments>http://gamemath.com/2011/10/team-fortress-2-halloween-2011-update-released/#comments</comments>
		<pubDate>Thu, 27 Oct 2011 17:22:04 +0000</pubDate>
		<dc:creator>Fletcher</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Team Fortress 2]]></category>

		<guid isPermaLink="false">http://gamemath.com/?p=247</guid>
		<description><![CDATA[The very scary Halloween update has been released. A new terrifying boss, MONOCULUS, roams the new map, the Eyeaduct! Collectible Halloween loot. Read the new comic, the bombinomicon!! Control point enabled. Get going!]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.teamfortress.com/images/posts/demo_eye.jpg" alt="Team Fortress 2 Halloween 2011 update" /></p>
<p>The <a href="http://www.teamfortress.com/veryscaryhalloween/">very scary Halloween</a> update has been released.  A new terrifying boss, MONOCULUS, roams the new map, the Eyeaduct!  Collectible Halloween loot.  Read the new comic, the <a href=http://www.teamfortress.com/bombinomicon/>bombinomicon</a>!!</p>
<p>Control point enabled.  Get going!</p>
]]></content:encoded>
			<wfw:commentRss>http://gamemath.com/2011/10/team-fortress-2-halloween-2011-update-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Interpolation and Control System Tricks, Part 1: Signals and Systems</title>
		<link>http://gamemath.com/2011/10/interpolation-and-control-system-tricks-part-1-signals-and-systems/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=interpolation-and-control-system-tricks-part-1-signals-and-systems</link>
		<comments>http://gamemath.com/2011/10/interpolation-and-control-system-tricks-part-1-signals-and-systems/#comments</comments>
		<pubDate>Sun, 16 Oct 2011 18:42:42 +0000</pubDate>
		<dc:creator>Fletcher</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://gamemath.com/?p=195</guid>
		<description><![CDATA[This post is the first in a series on a group of related tricks we can file under the category of &#8220;interpolation&#8221; or &#8220;control systems.&#8221; This can seem very abstract, but there&#8217;s a reason: it&#8217;s applicable in so many different &#8230; <a class="more-link" href="http://gamemath.com/2011/10/interpolation-and-control-system-tricks-part-1-signals-and-systems/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This post is the first in a series on a group of related tricks we can file under the category of &#8220;interpolation&#8221; or &#8220;control systems.&#8221;  This can seem very abstract, but there&#8217;s a reason: it&#8217;s applicable in so many different areas, as this first post will attempt to show.  In this post, I want to discuss some general principles of signals, systems, and interpolation, and give practical examples of where these things come up in everyday video game programming.</p>
<h2>Signals</h2>
<p>Many of the data we deal with in video games can be viewed as a &#8220;signal,&#8221; or to use an equivalent term, a &#8220;function.&#8221;  Quite often we deal with signals in the time domain, or more simply, we have some data that changes over time.  Here are some examples:</p>
<ul>
<li>The player&#8217;s input values.  (Mouse movement rates, whether or not keys are depressed at that moment, etc)</li>
<li>The player character&#8217;s velocity.</li>
<li>The health of a character.</li>
<li>The visual size of the character&#8217;s health meter.</li>
<li>The positions and orientations of player characters and other objects in the world.</li>
<li>The current lock-on target.</li>
<li>Which context-sensitive camera mode is currently active?  For example, a close-up up on a taunt, a hint showing a door that opened as a result of the lever you just pulled, an adjustment to zoom to highlight an acrobatic element that can be interacted with, and so forth.</li>
<li>The &#8220;ideal&#8221; camera position, given the current state affairs in the game world: the selected camera mode, character positions, obstacles that must be avoided, etc.</li>
<li>The player&#8217;s current selection from some menu.</li>
<li>The screen-space position oi the visual cursor showing which menu element is highlighted.</li>
<li>A target&#8217;s position that an AI entity is trying to aim at</li>
<li>The authoritative position of an object, received periodically from the game server, when we are a client.</li>
<li>The proper position a character should be so that his animation lines up with some other object or character he is participating with.</li>
<li>The raw cursor position from the Wiimote.</li>
<li>The position of the cursor on the screen.</li>
<li>The data we get from Kinect libraries telling us where it thinks the player&#8217;s arms and legs are.</li>
<li>The actual positions and orientation of the bones used to animate the on-screen representation of the player.</li>
<li>&#8230;and I&#8217;m sure you can think of many more examples.</li>
</ul>
<h3>Continuous and discrete signals</h3>
<p>You will notice that some of the examples signals take on values from a continuous domain, such as the positions of characters and other objects in the world, or mouse movement rates.  Those are the signals that we store using floating point data types, whether they be 1D scalar values or 2D or 3D vectors.  Other signals may only assume values from a discrete set, for example, menu selection, discrete camera modes, lock-on targets, whether or not the &#8216;W&#8217; key is currently depressed, and so forth.  We use bools, ints, and enums for these.</p>
<p><img src="/figs/signals_discrete_continuous.png" /></p>
<p>Just because a signal comes from a continuous domain does <i>not</i> mean it is continuous function.  For example, a character&#8217;s health may be constant and then take a sudden jump up or down if they get shot or pick up a potion, etc.  An AI entity may suddenly switch targets, so that even if each individual target moves around in a continuous fashion, the signal representing &#8220;the position that the AI wants to be aiming at&#8221; may exhibit discontinuities.  Characters can warp and spawn.  A client depending upon a game server for the authoritative value of some continuous variable is only going to receive that variable in bursts.  One of the main reasons to understand control systems theory is to know how best to mask these discontinuities, making a smooth output signal out of a jumpy input one.</p>
<p>Of course, since we are working in a digital computer, in reality <i>all</i> of our signals are not truly continuous in time, because we only know (or care about) its value for particular time values, for example, perhaps at 30 different times per second.  But we still consider the underlying signal to be continuous, even if we are only sampling an input signal or updating an output signal once per frame.</p>
<h3>Must every signal be a function of time?</h3>
<p>All of the examples we&#8217;ll look at are time-domain signals, but before moving one, it&#8217;s worth mentioning that there are certainly signals from other domains.  For example, a visual image can be thought of as a function of the <i>continuous</i> screen space coordinates, defined at any (<i>x</i>,<i>y</i>) coordinates, not just at the pixel &#8220;centers.&#8221;  (We could use raytracing to determine the value.)  Practical images are represented as a discrete grid of pixel values (a bitmap); how to best sample the continuous signal to determine the pixel colors is a very important topic, one that can only be fully understood using the tools of signal processing theory.  (It is NOT simply the value of the continuous signal at the &#8220;center&#8221; of the pixel!)</p>
<h2>Systems</h2>
<p>Video games contain lots of code that, when viewed from the standpoint of signal processing theory, is a <i>system</i>.  A system is basically any process, function, algorithm &#8212; or block of video game code &#8212; that takes one or more input signals, and produces one or more output signals.  The list of example signals given previously was hopefully highly suggestive, and you already thought of these examples of systems:</p>
<ul>
<li>We typically do not map the player&#8217;s movement <i>directly</i> from the input.  For example, when the player presses the &#8216;W&#8217; key to move forward, he doesn&#8217;t snap into motion;  likewise, when he releases it, he doesn&#8217;t instantly stop.  There is a some logic that decides how to nicely ramp his velocity in and out.  A <i>system</i> is taking the raw input signal of the current status of the `W&#8217; key and mapping it to the actual player velocity.<br />
<br/><br />
<img src="/figs/signal_w_vs_velocity.png"/><br />
<br/>
</li>
<li>Console controllers are notorious for being &#8220;noisy&#8221;, if we just scaled the raw value as our velocity, you couldn&#8217;t stand still.</li>
<li>We might want the chracter&#8217;s health bar to visually animate, for an extra level of polish, even if the underlying health value jumps.</li>
<li>Camera control is full of control systems.  Often, there is one system that takes as input the current character position, lock on target, special interaction, etc, and produces an &#8220;ideal&#8221; camera position.  But this position cannot be used directly, it is extremely jerky.  The camera would snap suddenly when you switched lock-on targets.  The character movement might need to be &#8220;jerky&#8221; for gameplay reasons (be able to stop on a dime), but we might want the camera to not have this rigid, mechanical feeling.  So another system takes the raw ideal position and smooths it out, to provide the transitions between different modes and smooth out jerky movement.</li>
<li>The AI cannot just snap to aim at his desired target.  The assassin&#8217;s arm, the gun, the sentry turret, or whatever, would snap with infinite velocity; this is not realistic.  And if the AI happens to be shooting at you, it probably won&#8217;t seem fair or fun, either.</li>
<li>We might want the visual highlight over the user&#8217;s selection to animate smoothly, rather than just snapping into place.</li>
<li>The raw data from the Wiimote and the Kinect camera are noisy.  We must filter it or else the pointer and avatar will be jitterly</li>
</ul>
<p>As you can see, many of these examples can be put into the category of &#8220;making things feel right.&#8221;  Different types of systems will &#8220;feel&#8221; differently and have different performance characteristics.  It&#8217;s very helpful to have a library of systems to choose from.</p>
<p>In the next post, I will consider some very special signals that are able to capture common behaviour in signals: they are the impulse, step, ramp, and sinosoid.  By analyzing how our system responds to these special signals, we will learn a lot about how our system behaves in general.</p>
]]></content:encoded>
			<wfw:commentRss>http://gamemath.com/2011/10/interpolation-and-control-system-tricks-part-1-signals-and-systems/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Team Fortress 2 Manniversary update released!</title>
		<link>http://gamemath.com/2011/10/team-fortress-2-manniversary-update-released/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=team-fortress-2-manniversary-update-released</link>
		<comments>http://gamemath.com/2011/10/team-fortress-2-manniversary-update-released/#comments</comments>
		<pubDate>Fri, 14 Oct 2011 08:04:07 +0000</pubDate>
		<dc:creator>Fletcher</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Team Fortress 2]]></category>

		<guid isPermaLink="false">http://gamemath.com/?p=230</guid>
		<description><![CDATA[The Manniversary update has been released for Team Fortress 2! The TF2 store is one year old! (Which is about six months longer than I&#8217;ve been working on the game.) Read about it on the TF2 blog.]]></description>
			<content:encoded><![CDATA[<p><img src="/wordpress/wp-content/uploads/2011/10/lil_saxton_hale.jpg"/></p>
<p>The <i><a href="http://www.teamfortress.com/manniversary/">Manniversary</a></i> update has been released for Team Fortress 2!  The TF2 store is one year old!  (Which is about six months longer than I&#8217;ve been working on the game.)  Read about it on the <a href="http://www.teamfortress.com/post.php?id=6508">TF2 blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://gamemath.com/2011/10/team-fortress-2-manniversary-update-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Constructing a Rotation Matrix From Basis Vectors</title>
		<link>http://gamemath.com/2011/10/constructing-a-rotation-matrix-from-basis-vectors/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=constructing-a-rotation-matrix-from-basis-vectors</link>
		<comments>http://gamemath.com/2011/10/constructing-a-rotation-matrix-from-basis-vectors/#comments</comments>
		<pubDate>Thu, 06 Oct 2011 19:59:41 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[3D]]></category>

		<guid isPermaLink="false">http://gamemath.com/?p=189</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<div id="attachment_191" class="wp-caption aligncenter" style="width: 310px"><a href="http://youtu.be/4JTcfsf8R1I"><img class="size-medium wp-image-191" title="Rotation Matrix from Basis Vectors" src="http://gamemath.com/wordpress/wp-content/uploads/2011/10/ss1-300x225.png" alt="" width="300" height="225" /></a><p class="wp-caption-text">Click Here for a YouTube Video</p></div>
]]></content:encoded>
			<wfw:commentRss>http://gamemath.com/2011/10/constructing-a-rotation-matrix-from-basis-vectors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Geometric Primitives Lectures</title>
		<link>http://gamemath.com/2011/09/geometric-primitives-lectures/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=geometric-primitives-lectures</link>
		<comments>http://gamemath.com/2011/09/geometric-primitives-lectures/#comments</comments>
		<pubDate>Sat, 24 Sep 2011 23:00:34 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://gamemath.com/?p=167</guid>
		<description><![CDATA[I&#8217;m going to be out of town on September 27-30, 2011 presenting two papers at GAMEON-NA, so here are videos of the lectures that I would have given to my Game Math and Physics class at the University of North &#8230; <a class="more-link" href="http://gamemath.com/2011/09/geometric-primitives-lectures/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m going to be out of town on September 27-30, 2011 presenting two papers at GAMEON-NA, so here are videos of the lectures that I would have given to my Game Math and Physics class at the University of North Texas. A big shout-out to my students who are supposed to be watching them in my absence.</p>
<div id="attachment_168" class="wp-caption aligncenter" style="width: 522px"><a href="http://youtu.be/AM8YUPowfVM"><img class="size-full wp-image-168 " title="Lecture 1" src="http://gamemath.com/wordpress/wp-content/uploads/2011/09/Lecture1ss.png" alt="Lecture 1 at YouTube" width="512" height="384" /></a><p class="wp-caption-text">Click to see a video of Lecture 1 at youtube.com</p></div>
<div id="attachment_171" class="wp-caption aligncenter" style="width: 522px"><a href="http://youtu.be/C8jyLB9ji6g"><img class="size-full wp-image-171  " title="AABB Animation" src="http://gamemath.com/wordpress/wp-content/uploads/2011/09/Ned3DAABBss1.png" alt="AABB Animation" width="512" height="370" /></a><p class="wp-caption-text">Click to see an animation of Axially Aligned Bounding Boxes.</p></div>
<div id="attachment_172" class="wp-caption aligncenter" style="width: 522px"><a href="http://youtu.be/6Jw_oHkwGjw"><img class="size-full wp-image-172  " title="Lecture 2" src="http://gamemath.com/wordpress/wp-content/uploads/2011/09/Lecture2ss1.png" alt="Lecture 2" width="512" height="385" /></a><p class="wp-caption-text">Click to see a video of Lecture 2 at youtube.com.</p></div>
]]></content:encoded>
			<wfw:commentRss>http://gamemath.com/2011/09/geometric-primitives-lectures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gimbal Lock</title>
		<link>http://gamemath.com/2011/09/gimbal-lock/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=gimbal-lock</link>
		<comments>http://gamemath.com/2011/09/gimbal-lock/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 20:21:36 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[3D]]></category>

		<guid isPermaLink="false">http://gamemath.com/?p=159</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<div id="attachment_161" class="wp-caption aligncenter" style="width: 310px"><a href="http://youtu.be/A5NQ8PJ1PBc"><img class="size-medium wp-image-161" title="Gimbal Lock" src="http://gamemath.com/wordpress/wp-content/uploads/2011/09/nogimballock-300x217.jpg" alt="Gimbal lock video." width="300" height="217" /></a><p class="wp-caption-text">Click image for Gimbal Lock video at YouTube.</p></div>
]]></content:encoded>
			<wfw:commentRss>http://gamemath.com/2011/09/gimbal-lock/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Detecting Whether Two Convex Polygons Overlap</title>
		<link>http://gamemath.com/2011/09/detecting-whether-two-convex-polygons-overlap/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=detecting-whether-two-convex-polygons-overlap</link>
		<comments>http://gamemath.com/2011/09/detecting-whether-two-convex-polygons-overlap/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 15:27:01 +0000</pubDate>
		<dc:creator>Fletcher</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[2D]]></category>

		<guid isPermaLink="false">http://gamemath.com/?p=138</guid>
		<description><![CDATA[In a previous post I described how to tell whether two axially aligned bounding boxes (AABB&#8217;s) intersected. The solution was presented as a process of elimination: if box A is to the left of B, to the the right of &#8230; <a class="more-link" href="http://gamemath.com/2011/09/detecting-whether-two-convex-polygons-overlap/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://gamemath.com/2011/09/detecting-whether-two-boxes-overlap/">a previous post</a> I described how to tell whether two axially aligned bounding boxes (AABB&#8217;s) intersected.  The solution was presented as a process of elimination: if box <i>A</i> is to the left of <i>B</i>, to the the right of <i>B</i>, above <i>B</i>, or below <i>B</i>, then they cannot intersect.</p>
<p>This rule is a special case application of the <a href="http://en.wikipedia.org/wiki/Separating_axis_theorem"><i>separating axis theorem</i></a>.  This theorem says that if two 2D convex polygons don&#8217;t intersect, then there exists some line (known as a &#8220;separating axis&#8221;), such that when the polygons are projected onto that line, the projections of the two polygons will not overlap.</p>
<p>Here are two non-overlapping polygons.  The black line is a separating axis for these two polygons, because the projections of the polygons onto that axis (the thick green and blue blues) do not overlap.</p>
<p><img src="/figs/separating_axis_convex_polygon.png" /></p>
<p>A separating axis is really just a <i>direction</i> (we would describe the axis as a vector).  Translating the axis does not affect the polygon projections.</p>
<p>This immediately suggests an algorithm to determine if two convex polygons intersect: check all the potential separating axes; if the polygon projections overlap on all such axes, then they polygons intersect.  Otherwise, as soon as a separating axis is found, we can declare the polygons to be non-intersecting.</p>
<p>Unfortunately, there are an infinite number of directions.  Fortunately, as it turns out, we must only try a limited subset of those directions.  In 2D, we need only test the directions that are perpendicular a polygon edge.  For example, in the example above, the separating axis is perpendicular to the leftmost edge of the polygon on the right.  For 2D AABB&#8217;s, all of the polygon edges are horizontal or vertical, so there are only two possible separating axis, one vertical and horizontal.  (There were four cases to test, two per axis, because nonoverlapping polygons may be on either side of each other.)</p>
<p>We now have a strategy for detecting the overlap of 2D convex polygons.  Scan all the edges of both polygons.  For each edge, we form a candidate separating axis that is perpendicular to that edge.  We project the two polygons onto the axis, and test whether their projections overlap.  If not, the polygons are non-intersecting, and we are done.  If they overlap, keep searching.  If the polygons are overlapping on all candidate separating axes, then we can declare them to be overlapping &#8212; that is what the separating axis theorem guarantees us.</p>
<p>But what exactly does it mean to project the polygons onto an axis?  How do we represent that projection, in such a way that we can tell if the projections overlap?  The dot product is the answer.  Let&#8217;s translate our axis so that it contains the origin.  (We said that this does not change whether the projections overlap.)  Now we can describe the the projection of any given polygon vertex onto this axis by giving the signed distance from the projected vertex to the origin.  This distance just so happens to be precisely what we get when we dot the vertex with a unit vector parallel to the axis, which we&#8217;ll call <b>v</b>.</p>
<p><img src="/figs/separating_axis_translate_to_origin.png" /></p>
<p>The dot product gives us a tool to describe the projection of a polygon onto each candidate separating axis <b>v</b>.  For each polygon vertex, we take the dot product of the vertex with <b>v</b>, which gives us a signed distance from the origin.  We collect the minimum and maximum of these distance, which gives us the extents of the projection, labeled <i>a</i><sub>min</sub> and <i>a</i><sub>max</sub> in the figure above.  Note that we we don&#8217;t know or care ahead of time which points will be <b>a</b><sub>1</sub> and <b>a</b><sub>2</sub> as the figure above might lead you to believe; all we need are <i>a</i><sub>min</sub> and <i>a</i><sub>min</sub>.  A simple one dimensional overlap test of <i>a</i><sub>min</sub> and <i>a</i><sub>max</sub> against the <i>b</i><sub>min</sub> and <i>b</i><sub>max</sub> tells us if the projections overlap.</p>
<p>In fact, <b>v</b> need not be a unit vector at all.  If we scale <b>v</b> by some factor <i>k</i>, then that will scale the numeric values of <i>a</i><sub>min</sub>, <i>a</i><sub>max</sub>, <i>b</i><sub>min</sub>, and <i>b</i><sub>max</sub> by this same factor, and they will no longer measure the distance to the origin, but it will not affect whether the intervals overlap.</p>
<p>The following code snippet illustrates these ideas.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Very simple vector class</span>
<span style="color: #993333;">struct</span> Vec2D <span style="color: #009900;">&#123;</span> <span style="color: #993333;">float</span> x<span style="color: #339933;">,</span>y<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Dot product operator</span>
<span style="color: #993333;">float</span> dot<span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> Vec2D <span style="color: #339933;">&amp;</span>a<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> Vec2D <span style="color: #339933;">&amp;</span>b<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> a.<span style="color: #202020;">x</span><span style="color: #339933;">*</span>b.<span style="color: #202020;">x</span> <span style="color: #339933;">+</span> a.<span style="color: #202020;">y</span><span style="color: #339933;">*</span>b.<span style="color: #202020;">y</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Here is our high level entry point.  It tests whether two polygons intersect.  The</span>
<span style="color: #666666; font-style: italic;">// polygons must be convex, and they must not be degenerate.</span>
bool convexPolygonOverlap<span style="color: #009900;">&#40;</span>
    <span style="color: #993333;">int</span> aVertCount<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> Vec2D <span style="color: #339933;">*</span>aVertList<span style="color: #339933;">,</span>
    <span style="color: #993333;">int</span> bVertCount<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> Vec2D <span style="color: #339933;">*</span>bVertList
<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// First, use all of A's edges to get candidate separating axes</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>findSeparatingAxis<span style="color: #009900;">&#40;</span>aVertCount<span style="color: #339933;">,</span> aVertList<span style="color: #339933;">,</span> bVertCount<span style="color: #339933;">,</span> bVertList<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Now swap roles, and use B's edges</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>findSeparatingAxis<span style="color: #009900;">&#40;</span>bVertCount<span style="color: #339933;">,</span> bVertList<span style="color: #339933;">,</span> aVertCount<span style="color: #339933;">,</span> aVertList<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// No separating axis found.  They must overlap</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Helper routine: test if two convex polygons overlap, using only the edges of</span>
<span style="color: #666666; font-style: italic;">// the first polygon (polygon &quot;a&quot;) to build the list of candidate separating axes.</span>
bool findSeparatingAxis<span style="color: #009900;">&#40;</span>
    <span style="color: #993333;">int</span> aVertCount<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> Vec2D <span style="color: #339933;">*</span>aVertList<span style="color: #339933;">,</span>
    <span style="color: #993333;">int</span> bVertCount<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> Vec2D <span style="color: #339933;">*</span>bVertList
<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Iterate over all the edges</span>
    <span style="color: #993333;">int</span> prev <span style="color: #339933;">=</span> aVertCount<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> cur <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">;</span> cur <span style="color: #339933;">&lt;</span> aVertCount <span style="color: #339933;">;</span> <span style="color: #339933;">++</span>cur<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Get edge vector.  (Assume operator- is overloaded)</span>
        Vec2D edge <span style="color: #339933;">=</span> aVertList<span style="color: #009900;">&#91;</span>cur<span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> aVertList<span style="color: #009900;">&#91;</span>prev<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Rotate vector 90 degrees (doesn't matter which way) to get</span>
        <span style="color: #666666; font-style: italic;">// candidate separating axis.</span>
        Vec2D v<span style="color: #339933;">;</span>
        v.<span style="color: #202020;">x</span> <span style="color: #339933;">=</span> edge.<span style="color: #202020;">y</span><span style="color: #339933;">;</span>
        v.<span style="color: #202020;">y</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>edge.<span style="color: #202020;">x</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Gather extents of both polygons projected onto this axis</span>
        <span style="color: #993333;">float</span> aMin<span style="color: #339933;">,</span> aMax<span style="color: #339933;">,</span> bMin<span style="color: #339933;">,</span> bMax<span style="color: #339933;">;</span>
        gatherPolygonProjectionExtents<span style="color: #009900;">&#40;</span>aVertCount<span style="color: #339933;">,</span> aVertlist<span style="color: #339933;">,</span> v<span style="color: #339933;">,</span> aMin<span style="color: #339933;">,</span> aMax<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gatherPolygonProjectionExtents<span style="color: #009900;">&#40;</span>bVertCount<span style="color: #339933;">,</span> bVertlist<span style="color: #339933;">,</span> v<span style="color: #339933;">,</span> bMin<span style="color: #339933;">,</span> bMax<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Is this a separating axis?</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>aMax <span style="color: #339933;">&lt;</span> bMin<span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>bMax <span style="color: #339933;">&lt;</span> aMin<span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Next edge, please</span>
        prev <span style="color: #339933;">=</span> cur<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Failed to find a separating axis</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Gather up one-dimensional extents of the projection of the polygon</span>
<span style="color: #666666; font-style: italic;">// onto this axis.</span>
<span style="color: #993333;">void</span> gatherPolygonProjectionExtents<span style="color: #009900;">&#40;</span>
    <span style="color: #993333;">int</span> vertCount<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> Vec2D <span style="color: #339933;">*</span>vertList<span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// input polygon verts</span>
    <span style="color: #993333;">const</span> Vec2D <span style="color: #339933;">&amp;</span>v<span style="color: #339933;">,</span>                       <span style="color: #666666; font-style: italic;">// axis to project onto</span>
    <span style="color: #993333;">float</span> <span style="color: #339933;">&amp;</span>outMin<span style="color: #339933;">,</span> <span style="color: #993333;">float</span> <span style="color: #339933;">&amp;</span>outMax          <span style="color: #666666; font-style: italic;">// 1D extents are output here</span>
<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Initialize extents to a single point, the first vertex</span>
    outMin <span style="color: #339933;">=</span> outMax <span style="color: #339933;">=</span> dot<span style="color: #009900;">&#40;</span>v<span style="color: #339933;">,</span> vertList<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Now scan all the rest, growing extents to include them</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span> <span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> vertCount <span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #993333;">float</span> d <span style="color: #339933;">=</span> dot<span style="color: #009900;">&#40;</span>v<span style="color: #339933;">,</span> vertList<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span>      <span style="color: #009900;">&#40;</span>d <span style="color: #339933;">&lt;</span> outMin<span style="color: #009900;">&#41;</span> outMin <span style="color: #339933;">=</span> d<span style="color: #339933;">;</span>
        <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>d <span style="color: #339933;">&gt;</span> outMax<span style="color: #009900;">&#41;</span> outMax <span style="color: #339933;">=</span> d<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Let&#8217;s hasten to add a few words of warning.  The first is a warning concerning robustness: this code assumes valid convex polygons.  If any of the edges are degenerate, or if the polygon is concave, this routine can produce unexpected results.  Of course, the separating axis theorem applies only to convex polygons.  Detecting the intersection of concave polygons is a trickier business.  The second warning is concerning performance.  The algorithm above is clearly <i>O</i>(<i>N</i><sup>2</sup>).  For large values of <i>N</i>, different algorithms can be faster.  A standard approach is to sort the vertices vertically, and then sweep a horizontal line through the polygons, stopping at &#8220;event points&#8221; to see if the polygons have horizontal overlap at the given location.  With a small bit of extra work, this approach can also be made to work for concave polygons.</p>
<p>The separating axis extends naturally into 3D, to test whether convex polyhedra overlap.  It is this use of the theorem that gets the most attention in video games, since this test is needed in collision detection.  The set of potential separating axes starts with vectors perpendicular to the polygon faces, which is analogous to what we did here in 2D.  Unfortunately, in 3D things are a bit trickier and this list is not sufficient; there are cases where the polyhedra to not intersect, but a separating axis cannot be found among this simple set.  We must also include certain cross products in the candidate set.</p>
]]></content:encoded>
			<wfw:commentRss>http://gamemath.com/2011/09/detecting-whether-two-convex-polygons-overlap/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Detecting Whether Two Boxes Overlap</title>
		<link>http://gamemath.com/2011/09/detecting-whether-two-boxes-overlap/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=detecting-whether-two-boxes-overlap</link>
		<comments>http://gamemath.com/2011/09/detecting-whether-two-boxes-overlap/#comments</comments>
		<pubDate>Thu, 08 Sep 2011 11:02:22 +0000</pubDate>
		<dc:creator>Fletcher</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[2D]]></category>
		<category><![CDATA[Geometric Tests]]></category>
		<category><![CDATA[Interview Questions]]></category>

		<guid isPermaLink="false">http://gamemath.com/?p=80</guid>
		<description><![CDATA[Questions asked on job interviews also make for interesting blog post topics. (And very rarely, they are also practical!) This post discusses one of my favorite interview questions: &#8220;How can you tell if two axially-aligned 2D boxes intersect?&#8221; The adjective &#8230; <a class="more-link" href="http://gamemath.com/2011/09/detecting-whether-two-boxes-overlap/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Questions asked on job interviews also make for interesting blog post topics.  (And very rarely, they are also practical!)  This post discusses one of my favorite interview questions: &#8220;How can you tell if two axially-aligned 2D boxes intersect?&#8221;  The adjective &#8220;axially-aligned&#8221; means that the sides of the box are parallel to the <i>x</i>- and <i>y</i>-axes; the box is not arbitrarily oriented.  The acronym <i>AABB</i> is often used for axially-aligned bounding box.</p>
<p>I like this question because it is both a test of experience and problem solving ability.  Entry-level applicants aren&#8217;t necessarily expected to know the answer, but they should be able to suggest an approach (with very high frequency they suggest the incorrect solution discussed below), work through some examples, and arrive at the correct one.  Most experienced web/GUI programmers have worked with 2D boxes and have encountered the problem, and should know the proper solution.  However, they often do not immediately perceive the principle that extends the idea beyond AABB&#8217;s to arbitrarily-oriented boxes.  In summary, it&#8217;s a problem with a deceptively simple starting point and many branching points depending on the skill of the applicant, which is precisely why it&#8217;s a great interview question.</p>
<p>Let&#8217;s say that we have basic 2D vector and bounding box classes such as:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Simple 2D vector class</span>
<span style="color: #993333;">struct</span> Vec2D
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">float</span> x<span style="color: #339933;">,</span>y<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 2D axially-aligned bounding box.</span>
<span style="color: #993333;">struct</span> Box2D
<span style="color: #009900;">&#123;</span>
    Vec2D min<span style="color: #339933;">,</span> max<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p><img src="/figs/aabb_code_notation.png"/></p>
<p>So the goal of the question is a function with a prototype such as</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">bool BoxesIntersect<span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> Box2D <span style="color: #339933;">&amp;</span>a<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> Box2D <span style="color: #339933;">&amp;</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>When faced with this problem, inexperienced programmers produce a particular solution that <i>doesn&#8217;t</i> work with such regularity, that it is worth mentioning.  They suggest to check the four corners of box <i>A</i>, to see if any are containing within box <i>B</i>, and also check <i>B</i>&#8216;s corners to see if they are contained within <i>A</i>.  But this approach fails in the following example.</p>
<p><img src="/figs/aabb_intersect_no_corners_contained.png"/></p>
<p>The correct approach is to work by process of elimination.  What are some situations when it is clear that the two AABB&#8217;s do <i>not</i> intersect?  We can immediately think of an obvious case: if box <i>A</i> is completely to the left of <i>B</i>, then the boxes cannot intersect.  Likewise if <i>A</i> is completely to the right of <i>B</i>, or above or below it.  Are there any other cases to consider?  No.  If <i>A</i> is completely to the left or right of <i>B</i>, then <i>the vertical positions of the boxes do not matter</i>.  If two boxes do not intersect, they will fit into at at least one of the four cases just mentioned.  (If you are not convinced of this, a useful exercise is to try to categorize all the possible possible cases.)  So the only remaining task is to express &#8220;<i>A</i> is completely to the left of <i>B</i>,&#8221; and so forth, in C.</p>
<p>This is the correct answer:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">bool BoxesIntersect<span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> Box2D <span style="color: #339933;">&amp;</span>a<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> Box2D <span style="color: #339933;">&amp;</span>b<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>a.<span style="color: #202020;">max</span>.<span style="color: #202020;">x</span> <span style="color: #339933;">&lt;</span> b.<span style="color: #202020;">min</span>.<span style="color: #202020;">x</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// a is left of b</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>a.<span style="color: #202020;">min</span>.<span style="color: #202020;">x</span> <span style="color: #339933;">&gt;</span> b.<span style="color: #202020;">max</span>.<span style="color: #202020;">x</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// a is right of b</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>a.<span style="color: #202020;">max</span>.<span style="color: #202020;">y</span> <span style="color: #339933;">&lt;</span> b.<span style="color: #202020;">min</span>.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// a is above b</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>a.<span style="color: #202020;">min</span>.<span style="color: #202020;">y</span> <span style="color: #339933;">&gt;</span> b.<span style="color: #202020;">max</span>.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// a is below b</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// boxes overlap</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>(By the way, whether &#8220;&lt;&#8221; and &#8220;&gt;&#8221; should be &#8220;&lt;=&#8221; and &#8220;&gt;=&#8221; depends on exactly what you mean by boxes &#8220;intersecting.&#8221;  If you are using floating point coordinates and this distinction matters, you are probably doing something wrong.)</p>
<p>Most everybody understands the solution to this problem, and it extends in a straightforward way into 3D.  So that&#8217;s pretty much all there is to say about AABB&#8217;s.</p>
<p>But what about boxes that are arbitrarily oriented?  How about convex polygons (or polyhedra in 3D) with an arbitrary number of sides?  To solve these problems requires that we distill from our ad-hoc solution a more fundamental principle, known as the <a href="http://en.wikipedia.org/wiki/Separating_axis_theorem"><i>separating axis theorem</i></a>.  It can be used to <a href="http://gamemath.com/2011/09/detecting-whether-two-convex-polygons-overlap">detect whether two convex polygons overlap</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://gamemath.com/2011/09/detecting-whether-two-boxes-overlap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

