Vectors are used to store multiple components of a variable, usually the axes of spacial dimensions. As a result they are usually either used in 2D or 3D.
There are technically two ways to represent a vector:
2D:
This could be defined as "a rotation and a magnitude" or as a "displacement in x and a displacement in y".
3D:
This could be defined as "a rotation in xz, a rotation in y and a magnitude" or as a "displacement in x, a displacement in y and a displacement in z".
Generally for games we use the displacement in dimensions version rather than the magnitude and rotation.
Vectors can come in a variety of forms:
i, j, k form - Where i means 1 unit in the x-axis, j means 1 unit in the y-axis and if you're in 3D, k means 1 unit in the z-axis
In 2D it looks like: where 3 is 3 units in the positive x-axis and 4 is 4 units in the positive y-axis.
In 3D it looks like: where 6 is 6 units in the positive x-axis, -3 is 3 units in the negative y-axis and 5 is 5 units in the positive z-axis.
Column vector - This is the most common vector form.
In 2D, it looks like: where 3 is 3 units in the positive x-axis and 4 is 4 units in the positive y-axis.
In 3D, it looks like: where 6 is 6 units in the positive x-axis, -3 is 3 units in the negative y-axis and 5 is 5 units in the positive z-axis.
Row vector - This is the least common vector form.
In 2D, it looks like: where 3 is 3 units in the positive x-axis and 4 is 4 units in the positive y-axis.
In 3D, it looks like: where 6 is 6 units in the positive x-axis, -3 is 3 units in the negative y-axis and 5 is 5 units in the positive z-axis.
When writing vectors you can denote it like this:
or just
When going from one location to another (from A to B), it can be written like:
What do you do when there is a constant multiplied/divided by a constant? You simply apply the multiplication/division to all components of the vector:
Multiplication:
Division:
What about when there is a vector multiplied by another vector? There are two ways that are defined for vector multiplication: vector dot product and vector cross product.
Unit length vectors are used when the magnitude isn't important, only the direction is. In 2D it's based around the unit circle and in 3D around the unit sphere; the values for the x, y and z will be between -1 and 1.
The notation for this is:
To turn a vector into a unit length vector, use the following equation:
, this is known as normalising the vector.
As a vector is made up of many parts, the numbers used for each dimension show the distance in each direction. In order to find the distance from point to point diagonally across the dimensions, you need to find the magnitude of the vector.
The magnitude of a 2D vector is given by (according to the Pythagorean Theorem):
A position vector is a vector that is treated like a coordinate, so it's a vector from the origin to a point. The letter O is often used to refer to the origin.
A position vector can be written as:
is the same as saying there's a point which has coordinates:
Calculating the vector between two position vectors
A basic vector equation consists of a vector and a variable coefficient of another vector. The standalone vector is known as the position vector and the vector with the variable coefficient is known as the direction vector. The position vector indicates where in the 3D space the vector is located, and the direction vector shows which way the vector pointing. The vector for the position vector can be the position vector of any point on the line.
So for this example, you'll need two lines, line 1: and line 2:
You then need to set the equations equal to each other.
Then subtract one set of constants to keep the constants on one side.
From this you can then make an equation from each row:
When finding the values of t and u, you'll only want to use two of the equations, that way you can check for a match on the third (this step for checking is not needed in 2D). I'm going to use equation 1 and equation 2:
Use any type of simultaneous equation solving method you want
→ Doubled both sides of this equation
got equation 1, and subtracted from it equation 2
solved for u
substituted u back into equation 1
solved for t
Now to check if it works in the final equation, substitute the values for u and t into the equation that you didn't use.
If you get a value expression (a constant equals itself) then this means that they intersect, else they don't. This is known as a skew.
The closest point to another point on a line equation and the distance between them
So let's say there's a vector equation and there's a point P with a position vector: . We want to find both the coordinate of the point on the line that is closest to P and the distance between that point and P.
We'll refer to this point on the line as X.
First we need to use our dot product rule, because the line from P to X will be perpendicular to the line itself. So to do this we'll need to find the direction of the line and the vector from P to X.
The direction of the line is just the direction vector of the line (which we'll call A):
As for the direction of P to X, we'll need to find X first, even if it's in terms of t.
X is simply the line equation itself written out as one vector
The cross product is defined as:
where is the angle between them and is a unit vector that is perpendicular to both and . The cross product only works in 3 and in 7 dimensions (you won't need the 7D version).
There are two vectors that will be perpendicular to both A and B, one that's pointing out of the page/screen, and one that's pointing into the screen. One is and the other is . As they are both pointing in different directions and the magnitude is the same, this means that: . You can find the direction of the cross vector by using the right-hand-rule.
As your computer doesn't have a 'right hand' you can use this formula that does the same thing:
A normal vector is a unit-length vector that points outwards from a surface of geometry. For every piece of flat geometry, there are two normals, one facing directly up (relative to the surface), the other facing directly down (relative to the surface), such that one is the negative of the other.
To calculate a normal vector you'll first need to find a corner of the geometry. There there will be two edges that come from that point. You need to find the vectors for these two edges. From then you'll need to find the cross product of edge 1 (E1) and edge 2 (E2). After that you'll need to normalise the vector to make it unit length.
Rewritten as one equation:
Note: This is mathematically better, however it's computationally worse, use the first version with computers to avoid calculation of the cross product twice. This will require the storage of an extra variable, but it's much easier for the processor.
To find the other normal (n2) you can use one of the following equations: