Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
65 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

 Vector Operations

 Printable version
 Discuss this article
 in the forums

The Series
 Vectors and Planes

Prelude to a Greater Understanding

I got a lot of feedback from the first article, much of it complaining about its simplicity. Well, soon you’ll be screaming at me to go back to the simple stuff. This time, I’ll be discussing basic vector math and we will also be taking a look at planes. The following information is the kind of stuff that you need to understand if you wish to succeed in the world of 3-D graphics. So listen up well, and soon you’ll be doing all kinds of neat things.

What is a Vector?

When I discussed points I mentioned that a vector consisted of similar properties to a point. Well here is the point structure again.

struct Point
  float x;
  float y;
  float z;

And introducing our vector structure…

struct Vector
  float x;
  float y;
  float z;

"Holy Jamoly", I can hear you say, "They look exactly the same". Well, yes that is true, but where they differ is in what they represent mathematically. Remember a point is used to reference a particular position in our coordinate system. Well rather than position, a vector represents two new concepts: magnitude and direction.

When we speak of magnitude we speak of length, and direction obviously means which way the vector is pointing.

Figure 1. A coordinate system with a vector

Imagine that each axis on this coordinate system is 5.0 units in length. You can see our vector < 2.0x, 4.0y, 0.0z >, which is pointing 4.0 units up the y axis and 2.0 units across the x axis. How long is it? Let’s find out.

How long is your Vector?

In many vector operations which are performed, prior knowledge of the length of the vector is required. To find the length of a vector you take the square root of the sum of squares of each vector component as shown below.

|V| = sqrt( x2 + y2 + z2 )

Note that our vector V is denoted using the | | symbol. This means that we are referring to the vector's length. Using the above formula our code may look something like this:

inline float Length( Vector &v )
  return( sqrtf( (v.x*v.x) + (v.y*v.y) + (v.z*v.z) ) )

Using the vector in Figure 1, we will find the length. The vector length equation is perfomed on our vector < 2.0x , 4.0y, 0.0z >, and we find the length equal to 4.47214.

Figure 2. Our vector's length

Normalized Vector

Also known as the unit vector, a normalized vector's length is equal to exactly 1. To find the normal vector we must calculate the length, and then divide each component of the vector by that length.

Normal = V/ |V|

V = ( 3.0x, 6.0y, 2.0z )

|V| = 7.0

V.x /= |V|
V.y /= |V|
V.z /= |V|

V.x = 0.428
V.y = 0.857
V.z = 0.285

|V| = 1.0

So we have our vector V with its values described above. We calculate the magnitude of the vector and we find that its length is 7.0. Following that we divide each component by the magnitude. If we calculate the length of the vector again we now find that it is equal to 1.0, the very value which makes it a normal vector. Remember that although the vector's length may now be equal to 1.0 its direction is still the same. Here is a way we can do it in code.

inline Vector Normalize( Vector &v )
  float temp = 1/Length( v );
  v.x *= temp;
  v.y *= temp;
  v.z *= temp;
  return( v );

Notice how we cached the division. That’s a nice optimization because multiplication is faster than division.

Next : Vector Operations