<?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>PixelStation &#187; vectors</title>
	<atom:link href="http://pixelstation.ca/tag/vectors/feed/" rel="self" type="application/rss+xml" />
	<link>http://pixelstation.ca</link>
	<description>Development and Design</description>
	<lastBuildDate>Mon, 21 Dec 2009 19:51:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Vectors &#8211; Part 1</title>
		<link>http://pixelstation.ca/2009/12/2d-and-3d-math-part-1-vectors/</link>
		<comments>http://pixelstation.ca/2009/12/2d-and-3d-math-part-1-vectors/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 06:23:26 +0000</pubDate>
		<dc:creator>Jordan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[vectors]]></category>

		<guid isPermaLink="false">http://pixelstation.ca/?p=56</guid>
		<description><![CDATA[One of the biggest challenges for a game programmer is understanding how to represent position and movement in 2D and 3D space. In this tutorial I will teach you the basics of understanding vectors in 2D and 3D space. Vectors For the most part, we use vectors. In math and physics, vectors are usually represented [...]]]></description>
			<content:encoded><![CDATA[<p>One of the biggest challenges for a game programmer is understanding how to represent position and movement in 2D and 3D space. In this tutorial I will teach you the basics of understanding vectors in 2D and 3D space.</p>
<h1>Vectors</h1>
<p>For the most part, we use <a title="Euclidean vector - Wikipedia" href="http://en.wikipedia.org/wiki/Euclidean_vector" target="_blank">vectors</a>. In math and physics, vectors are usually represented by a <strong>direction (angle)</strong> and a <strong>magnitude (length)</strong>.<br />
See the diagram below (imagine the hypotenuse as an arrow):</p>
<p><img class="alignnone size-medium wp-image-64" title="Triangle" src="http://pixelstation.ca/wp-content/uploads/2009/12/triangle-300x160.GIF" alt="Triangle" width="300" height="160" /></p>
<p><strong>Theta</strong> (displayed as θ) is your angle in either degrees or radians (I will always talk about angles in degrees). The <strong>hypotenuse</strong> is the longest side of the triangle (your length or magnitude). The <strong>adjacent</strong> side is the side that touches the angle you&#8217;re working with, the <strong>opposite</strong> side is the side across from the angle. The adjacent and opposite sides are often your <strong>X and Y components</strong>, respectively.</p>
<p><span id="more-56"></span>In programming, we use the X and Y components so often that we just store and use them instead of the angle and magnitude.</p>
<p>Say we have a triangle with and angle and a magnitude, but we want the components. Here&#8217;s how to get them:</p>
<p>To get the components we use <strong><em>SOH CAH TOA</em></strong>, meaning this</p>
<p><img class="alignnone size-full wp-image-66" title="sohcahtoa" src="http://pixelstation.ca/wp-content/uploads/2009/12/sohcahtoa.gif" alt="sohcahtoa" width="161" height="117" /></p>
<p>so,<br />
<em>sine(θ)=O/H<br />
cosine(θ)=A/H<br />
tangent(θ)=O/A</em><br />
*(θ just represents the angle)</p>
<p>keeping in mind that opposite will be our Y component and adjacent will be our X component, we come up with this</p>
<p><em>sine(theta) = Y/H<br />
cosine(theta) = X/H</em><br />
(we don&#8217;t need tangent at the moment)</p>
<p>and finally, this can be rearranged to make</p>
<p><em>Y = H × sine(theta)<br />
X = H × cosine(theta)</em></p>
<h1>Finally, Time for Some Code</h1>
<p>We&#8217;ve got a good grasp on vectors now, so we need to make some code. How do we represent a vector in C++? Easy, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> Vector2D
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">double</span> x, y<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>There, we now have a type Vector2D with the inner components &#8216;x&#8217; and &#8216;y&#8217;.</p>
<p>Next, we have a vector. What can we use it for? The answer to that is &#8220;just about anything you want.&#8221; Vectors are used for so many applications that it&#8217;s nearly impossible to tell you what to use it for. The main things (in game programming) are position, direction and velocity.</p>
<h1>2D Is So Old. I Need 3D.</h1>
<p>So just add the Z component. It&#8217;s that easy (for now).</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> Vector3D
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">double</span> x, y, z<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Later, things won&#8217;t be so simple.</p>
<h1>Coming Up Next&#8230;</h1>
<p>We&#8217;ll be working with adding, subtracting vectors and multiplying and dividing scalars (magnitudes with no direction, aka regular numbers).</p>
]]></content:encoded>
			<wfw:commentRss>http://pixelstation.ca/2009/12/2d-and-3d-math-part-1-vectors/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
