What’s your Vector Victor
What a great movie and the line holds as true there as it does for us in 3D. Anything that is stationary or moving has all sorts of vectors associated with it that describe where it is in space, how fast and in what direction and how it is oriented in 3D space. Vectors exist in your 2D packages as well, paint a line in Photoshop and it is laying down vectors on a Cartesian graph for each paint stroke or for everything line and shape in Illustrator. For instance the Transform Matrix uses vectors to describe the translation, rotation and scale of a node in the scene.
If you are a budding or experienced TD/TA you really should know what they can do for you and how to use them.
I should point out that I have a long winded Youtube tutorial called Max Math. This was an on line master class that was three hours long and I use 3DS Max to show all the examples but it applies to all 3D applications just the same. https://www.youtube.com/watch?v=-MNXw7q-rjE
What is a point or vector?
First things first, lets define what a vector or point is for those like myself that figured none of this was going to be of any use when they were taking high school math.
A vector is a point in space, a singularity with no rotation or scale component, it is just a point on a Cartesian graph and in this case it is in 3D space with an X, Y and Z component often written as [X,Y,Z]. Each value is a float meaning it has a decimal. A vector at the center of world space would look like [0.0, 0.0, 0.0] or a vector that is one unit out on the X and Y axis would be [1.0, 1.0, 0.0]. To ensure that this text is as readable as possible I will use integer values where I can to reduce the amount of decimals and zeros floating around.
You can break your entire 3D environments down to vectors on a graph, the position, rotation and scale of a node as in the Transform Matrix are all points and vectors as is every vertex in a mesh or the direction a particle is moving through a scene. We will go with the idea that everything is defined with a point or vector for now.
Length or Magnitude of a Vector
A vector can be said to have a length or a distance between two vectors. Lets start with length of a single vector.
The length of a vector is measured from the origin or [0,0,0] to the vector in a straight line. To get the length of a vector we need to use a square root function. We don’t need the [0,0,0] world value as it is assumed we just need the vector that we are working with.
In code that would look more like this and we will assume a vector of [2.0, 2.0, 0].
Depending on what language and math library you are using the commands might be a little different but this should be close enough.
len = sqrt(2.0^2 + 2.0^2 + 0^2) print len 2.8284
What is nice is that any decent math library will include a length function and you don’t have to remember the square root function. This will also clean up your code and make it easier to read as the word length is obvious to you and the next person that is reading your code.
V=[2.0, 2.0, 0]
len = length V
print len
2.8284
Distance is much the same but this time we will start with two vectors and we will call them p0 and p1 as the start point and the end point. We want to get the distance from p0 to p1 and we can use this simple equation to return a vector that is relative to [0,0,0].
Once again any decent library will have a distance function that takes the two points as inputs and outputs a length / distance value.
p0 = [1.0, 0.0, 0.0]
p1 = [2,0, 0.0, 0.0]
dist=distance p0 p1
print dist
1.0
For instance lets calculate a point between two other points as if you have position constrained a node between two others so that it sits somewhere in between. The equation to do that is very simple. (t) in the equation refers to time but that can be confusing for non-math nerds. Think of it as a weight between the two original points. 0 is at the start and 1 being at the 1 so 0.5 would be right in the middle.
So if we plot that out piece by piece on a graph we can see from what we did above that (p1 – p0) will return a vector the same length but starting at [0,0,0] world space. If you were to multiply each component of the new vector by 0.5 (t) we would have a vector pointing in the same direction but half as long. Then add the value of p0 onto that and it moves the new point between the original two at exactly half way between. This is effectively a position constraint between two points with a weight. The exact same equation is used to calculate a curve believe it or not. We will have to look into that more in another tutorial as to how that is done.
Normalized / Unit Vector
A unit vector is a vector that has a length of exactly 1.0. For instance with the transform matrix the first three vectors are normally unit vectors, if they are not this means that the matrix has been scaled and this is something that you usually want to avoid when ever possible especially in complex hierarchies like character rigs.
The math is simple on this now that we know how to get the length of a vector. We divide each of the X, Y and Z components by the length of the vector.
So if we take the vector below that has a length of 2.0 and divide each of the components by 2.0 we will end up with a vector pointing in the same direction as the original but the length of the vector will be 1.0. We call this normalizing a vector.
Once again there are usually built in functions for normalizing a vector if you don’t remember the math for it.
nV = normalize [2.0, 0, 0]
print nV
[1.0, 0, 0]
So big deal, whats the point of doing this?
We already mentioned that it is used in a transform matrix to ensure that it is not scaled but has the same rotation. We can also use it to project a line in a given direction with a specific length. If the vector has a length of 1 all you need to do is multiply the vector by the length that you want it to be.
Dot Product
Now that you have got this far you are probably saying to your self that it wasn’t a good week to stop sniffing glue.
Dot Product allows us to gain some more information from vectors. In this case we can work with three vectors, one that is implied (we will call this the root), [0,0,0] and two others that we can use to gain information about the angle between them and the root vector.
Dot Product will return a value from -1 to 1 much like the sine of a value will. How ever this will help us determine the angle the vectors we create.
Lets look at three examples and see what values will be returned.
/* First */ v0 = [1,0,0] v1 = [1,0,0] dot v0 v1 = 1.0 /* Second */ v0 = [1,0,0] v1 = [0,1,0] dot v0 v1 = 0.0 /* Third */ v0 = [1,0,0] v1 = [-1,0,0] dot v0 v1 = -1.0
With the first set you can see that both vectors are pointing in the same direction along the X axis and the dot product of those two vectors returns a value of 1.0 telling us they are point in the same direction.
For the second example they are at a right angle to each other and the value returned is 0.0. Note that it does not mater which right angle being positive, negative on either the Y ot Z axis it will still return 0.0.