<?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>Higher-Order Fun &#187; c++</title>
	<atom:link href="http://higherorderfun.com/blog/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://higherorderfun.com/blog</link>
	<description>Game Design &#38; Game Programming</description>
	<lastBuildDate>Sun, 31 Aug 2014 00:02:17 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.39</generator>
	<item>
		<title>Math for game programmers 02 &#8211; Vectors 101</title>
		<link>http://higherorderfun.com/blog/2009/06/07/math-for-game-programmers-02-vectors-101/</link>
		<comments>http://higherorderfun.com/blog/2009/06/07/math-for-game-programmers-02-vectors-101/#comments</comments>
		<pubDate>Sun, 07 Jun 2009 20:56:17 +0000</pubDate>
		<dc:creator><![CDATA[amz]]></dc:creator>
				<category><![CDATA[math]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[vectors]]></category>

		<guid isPermaLink="false">http://higherorderfun.wordpress.com/?p=18</guid>
		<description><![CDATA[If you don&#8217;t already use vectors (and I mean vectors in the MATHEMATICAL sense, not in the &#8220;dynamic array&#8221; sense that languages like C++ or Java use), this is probably going to be the single most useful piece of math you can learn for writing games. It&#8217;s easy, efficient, and makes your code much shorter. [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>If you don&#8217;t already use vectors (and I mean vectors in the MATHEMATICAL sense, not in the &#8220;dynamic array&#8221; sense that languages like C++ or Java use), this is probably going to be the single most useful piece of math you can learn for writing games. It&#8217;s easy, efficient, and makes your code much shorter. In fact, provided that you&#8217;re using a language that supports definition of operators for user-defined types (like, say, C++ or Haskell), using vectors is going to be extremely intuitive and simple.</p>
<p><strong>Motivation: The Ugly Code<br />
</strong></p>
<p>Consider that you want to simulate some simple physics, such as an object accelerating in 3D space. We&#8217;re going to use the formulas Î”s = vâ‚€t + Â½Â·aÂ·tÂ² and Î”v = aÂ·t (where s is the position, vâ‚€ is the speed at the start, a is the acceleration, and t is the elapsed time). You might declare the object as something like this:</p>
<pre class="brush: cpp; title: ; notranslate">

class PhysicsObject {
  public:
    float pos_x, pos_y, pos_z; // Position
    float vel_x, vel_y, vel_z; // Velocity
    float imass; // The inverse of the mass (1/m)

    void addImpulse(float force_x, float force_y, float force_z, float t) // t is time, in seconds
    {
        float halft2 = 0.5f * t * t;

        float accel_x = force_x * imass;
        float accel_y = force_y * imass;
        float accel_z = force_z * imass;

        pos_x += vel_x * t + accel_x * halft2;
        pos_x += vel_y * t + accel_y * halft2;
        pos_x += vel_z * t + accel_z * halft2;

        vel_x += accel_x * t;
        vel_y += accel_y * t;
        vel_z += accel_z * t;
    }
};

</pre>
<p>Not very pretty &#8211; there&#8217;s a lot of redundancy &#8211; whatever you do for one axis, you do for the other two. If only there was a way to do that all at once&#8230;</p>
<p><strong>Enter vectors: The Pretty Code<br />
</strong></p>
<p>Here&#8217;s the same code, but this time using a hypothetical 3d vector class:</p>
<pre class="brush: cpp; title: ; notranslate">

class PhysicsObject {
  public:
    vector3f pos; // Position
    vector3f vel; // Velocity
    float imass; // The inverse of the mass (1/m)

    void addImpulse(vector3f force, float t) // t is time, in seconds
    {
        vector3f accel = force * imass;
        pos += vel * t + accel * (0.5f * t * t);
        vel += accel * t;
    }
};

</pre>
<p>Much shorter, isn&#8217;t it? You now have half as many lines to write, test and debug. Not only that, but they&#8217;re also simpler, and you avoid a whole class of &#8220;copy-paste but oops forgot to change a letter in one place&#8221; bugs. So, after all, what ARE these vectors? How do they work?</p>
<p>P.S.: If you&#8217;re using a language which does not allow you to define your own operators, the code above will get quite uglier&#8230; Java, I&#8217;m looking at you.</p>
<p><strong>Defining vectors</strong></p>
<p>If you go check <a title="Euclidean Vector at Wikipedia" href="http://en.wikipedia.org/wiki/Euclidean_vector" target="_blank">Euclidean Vector at Wikipedia</a>, you&#8217;ll see that it&#8217;s defined as a geometric object that has magnitude (length) and direction. For our purposes, while important to remember that definition, we&#8217;ll think of vectors as an n-tuple of a given type. In the example about, we have a 3-tuple (&#8220;a triple&#8221;) of floats. In other words, our definition would be something like this:</p>
<pre class="brush: cpp; title: ; notranslate">

class vector3 {
  public:
    float x, y, z;

    inline vector3 operator + (const vector3 &amp;amp;param) const
    {
        return vector3(x + param.x, y + param.y, z + param.z);
    }

    inline vector3&amp;amp; operator += (const vector3 &amp;amp;param)
    {
        x += param.x;
        y += param.y;
        z += param.z;
        return *this;
    }

    inline vector3 operator * (float param) const
    {
        return vector3(x * param, y * param, z * param);
    }

    // Lots of other methods here
};

</pre>
<p>Basically, all it does (so far) is take basic arithmetic operators and apply them to 3 values, instead of just one. When you add two vectors, it adds each component &#8211; x to x, y to y and z to z. There&#8217;s also the &#8220;scale&#8221; operator, which is the multiplication by a scalar (a scalar is a non-vectorial number, like a float). When you multiply a vector by a scalar, you simply multiply all components by it. So, if you want to make your vector twice as big, just multiply it by 2.</p>
<p>That&#8217;s it! That&#8217;s the basic idea! Whenever you have two or more points and always want to perform the same set of operations on them, use a vector! In fact, most properties that apply to scalar numbers will also work here&#8230; If you happen to know that you need to multiply velocity by time to get a change in position, you can do that with vectors in just the same way you would with scalars.</p>
<p>When should you use vectors like this? <em>Whenever it makes sense</em>. If you&#8217;re passing along two or three values which all represent the same point, do yourself a favor and pack them in a vector. Not only will it be much harder to accidentally mix up stuff, your code will be shorter. Even if they aren&#8217;t a POSITION, they might still be represented by a vector &#8211; for example, the size of a rectangle is a vector where x is the width and y is the height. That way, if you have a vector representing the starting position of the rectangle, and a vector representing its size, you can easily find its ending position by adding them up.</p>
<p>But vectors are far more powerful than this. On the next article, we&#8217;ll explore other common operations done on them, such as length, normalization, dot product and cross product. You wouldn&#8217;t believe me if I told you just how damn useful those can be.</p>
]]></content:encoded>
			<wfw:commentRss>http://higherorderfun.com/blog/2009/06/07/math-for-game-programmers-02-vectors-101/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
