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.