Home > 3d Programming > 3d Programming basics: What is a vector

3d Programming basics: What is a vector

So you probably already know what a vector is. and maybe you already have a way of visualizing them. I once saw a post on the forums where the poster asked “if a vector is like a line, and since a line has two end points, how could you describe a vector by just listing one point”. He asked if there was a universal understanding that the other end point is always supposed to be the origin. He was trying to draw the correct picture in his mind’s eye of what a vector should look like.

A vector can represent a position in your game world. Or it can represent a direction. Let’s take two examples

Vector3 myModelPosition = new Vector3( 1.5f, 1.5f, 1.5f);

Vector3 myModelVelocity = new Vector3(0, 0, -0.5f);

Here is a graphic displaying those two vectors

So other that the component values of these two vectors, they are represented here the same. Both look like black rays emanating from the world origin capped off with a little arrow-head. Yet, in a game, they would be used in a different manner. When you use them in a different manner, it might be useful to visualize them differently.

There at 3 items of information that each vector carries with it at all times. They are:

1.) The direction the vector is pointing.

2.) The length of the vector.

3.) The world space location of the arrowhead end of the vector.

When you use a vector in a game. You generally don’t need all three items of information for every vector. For instance, for the modelPosition vector above, I don’t care what direction the vector is pointing or how long it is. I only care bout the world space location that is represents. In the modelVelocity vector, I couldn’t give a hoot about the world space location. What I care about with that one is the direction it is pointing (because that’s the direction the model will go if I apply this velocity to it) and its length (because I will use the length to set the speed).

Generally speaking, the useful information you will want to extract from any given vector will fall into one of the following 3 categories.

Most common uses of vectors

1.) To hold location information only. Used for setting object positions.

2.) To hold direction and length information only. Used for velocities, forces, movements.

3.) To hold direction information only (a unit length vector). Useful for math calculations (physics, collision, AI… etc.).

When I’m using a vector to represent a location (such as the modelPosition vector above), I don’t visualize it as a ray (arrow). I simply think of it as a point in space. But when I use a vector for the other two purposes, I do think of it as a ray with the arrowhead. Since I’m using modelVelocity for just its direction and length and I don’t care about its location, I can place it any where and still retain its pertinent information.

So to re-draw the above graphic to eliminate the non-useful bits of information and re arrange it to be a more effective visualization, I would probably see this in my mind’s eye:

I colored modelPosition yellow so it could better differentiate itself here. So, in my mind’s eye only, the modelVelocity retained its direction and length (which I care about) but altered it’s location (which I don’t care about). The modelPosition lost it’s direction and length (which I don’t care about) but retained its location information (which I care about). So although we are visualizing two of the same things here (both are vectors) we can visualize them differently but in a way that makes practical sense. If you look back and forth between the two graphics, I hope you would agree that the second one makes it easier to visualize where your model is and which way & how fast it is going.

Categories: 3d Programming
  1. February 20, 2010 at 1:15 pm

    These 3d basisc, vector and xna tutorials are god-sent to some people who can absorb all this knowledge. I have been programming in 3d for 2 years now (povray-xna) and never comprehended 3d rotations since, I believe, we were wrongly lead on by other tuts and books. Like you said 3d rotations are (must be) easier if we do it by matrices instead of 3 angles. I havent tried your methods yet but I have a feeling they will help me far more in my 3d adventures than anything else I read upon so far. God bless you and all this site. Thabk you. Ibrahim Dogan, Turkey.

  2. Steve
    January 13, 2011 at 9:14 pm

    Many thanks for your articles on Vectors. There are many articles around on Vectors, most seem to assume that you are a rocket scientist…..

    Yours however make it VERY understandable.

    I wish you would write a book in your style, i think it would be a very welcome addition to the books out there that make too many assumptions, and do not explain things in a simple but effective manner.

    So many thanks for your time and dedication to these articles, and i look forward to more from you.

    Regards Steve :- Florida USA

  3. November 14, 2011 at 6:13 pm

    Very useful information and well presented. Thanks Steve.

  4. April 19, 2012 at 11:24 pm

    you are a life saver or i might say ego saver

  1. February 26, 2010 at 3:50 pm
  2. February 26, 2010 at 3:53 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: