# Vector Mathematics

## Intro

A vector is just a collection of two to four floating point values. You have likely already used and performed vector math without realizing it, as an RGB color is a Vector3, a UV coordinate is a Vector2, a light direction is a Vector3, and a vertex position is a Vector3.

*In HLSL, the shading language which unity uses, these are referred to as float2, float3, and float4 types. In GLSL, these are referred to as vec2, vec3, and vec4 types. In either language, the individual values of the vector an be accessed with either .x, .y, .z, .w notation, or .r, .g, .b, .a notation. Note that you can even access multiple channels with notation like myVector4.xz; this would give you the x and z component of your vector as a vector2.*

## Basic Math

You can perform most math operations on a vector with another vector of the same number of components, or with a vector and a single floating point value. As an example, if you have a vector3 equal to (1, 2, 3) and you multiply it by 4, the resulting vector would be (4, 8, 12). If you were to multiple that vector by another vector3 equal to (0, 2, 4), the result would be (0, 4, 12).

## Vector Management

Vectors can be split and assembled, in any order you like. For instance, here's a UV coordinate node, which contains Vector2 values, being split into separate channels using the Component Mask node, and then being reassembled again.

## Position Vectors

The position of an object in 3d space is represented as a Vector3 with X, Y, and Z values. Given two objects in space, some common things we might want to compute include the direction or distance between these two objects. These are both simple things to compute, but to better understand the math it's easier to imagine it in one dimension before going to three.

5 15 ____A___________________B_____

Lets say we have two positions on a line; position A is at 5, and position B is at 15.

What is the distance between these two points?

You likely don't need to think about the formula for this and answered 10, because B - A = 10.

If you want to move point B to point A, what do you have to do?

Hopefully you answered that you'd have to subtract 10 from B. Because B - (B - A) = 5 = A.

This applies equally well to 3d positional vectors. Given two objects, if we subtract one from the other, we get a new vector which is equal to the amount (and direction) we'd need to move one object to the other.

-5 15 ___A___________________B_____

Now, to be robust, we have to handle negative values. In this case, our formula of differenceVector = B - A breaks. However, this is easily fixed with the abs (absolute value) function. If we change our formula to differenceVector = abs(B-A), then it will handle negative values correctly.

Once we compute the a difference vector between two objects, we can think of it as having several properties. It represents both the direction and magnitude needed to move one object to another. However, we may need to work with these values independently of each other. For instance, if we want to work with two directions we likely need to compare them without the magnitude; if we want to compare the distance between two objects, we need a single float value to compare, not a vector with 3 values. Luckily, it's quite easy to get these values out of our vector.

If you want the direction without magnitude, you can normalize the vector. This will scale the vector such that all values fit within a -1 to 1 range.

If you want the magnitude without the direction, you take the length of the vector.

As an example, lets say we want to move point A 2 unit towards point B. For that, we could compute the differenceVector, normalize it to remove the magnitude, and then multiply the result by 2. If we add that result to point A, we would move point A 2 units towards point B.

## Direction Vectors

Dot, cross, example