ROBLOX uses vectors to represent positions and directions of things in the game world.
This is a more mathy post than the CFrame one, but there are no proofs. This is all very useful information.
ROBLOX has a Vector3 and Vector2 class which handles most vector operations. For the purpose of this post, I’ll be describing most things in terms of 2D vectors. All standards are ROBLOX standards (such as x being right, y being up…)
Vector Basics
A vector is a number that can contain any amount of elements. The number of elements which constitute a vector is described as a dimension. A 1D vector contains one element, and may look like this: <5>, While a 3D vector contains 3 elements and may look like <-2, 8, 5>.
Vectors are typically used to represent points in space. A 3D vector has an x component, y component and z component, <x, y, z>.
The x component determines how far right the vector is.
The y component determines how far up the vector is.
The z component determines how far back the vector is.
Vectors (in my mind and in this post), can typically represent 2 things when it comes to space. Positions, and directions.
Positions are drawn as points, and directions are drawn as arrows.
VectorA is a position vector and represents the position <1, 1>.
VectorB is a direction vector and represents the direction <-0.5, 0.5>.
No matter where position VectorA is, direction VectorB will always point in the direction, <-0.5, 0.5>.
In ROBLOX, you can construct a new Vector3 at the position <0, 5, 8> with:
Vector3.new(0,5,8)
Vector Operations
Addition and Subtraction
VectorA = <3, 1>
VectorB = <1, 2>
VectorA + VectorB = VectorA + VectorB
Vectors are added component wise.
This means that if we have two vectors, <1, 2, 3> and <4, 5, 6>, they can be added like
<1, 2, 3>
+<4, 5, 6>
=
<1+4, 2+5, 3+6>
=
<5, 7, 9>
On ROBLOX, Vectors can be added like so:
Vector3.new(1,2,3)+Vector3.new(4,5,6)
Subtraction works the same way, except with minus signs.
Division
Division for vectors isn’t really defined. There are multiple ways to do vector division, but ROBLOX does them in two ways.
Vector / scalar division
VectorA = <1, 2>
B = 2
VectorA / B = VectorA / B
In vector / scalar division, you divide each of the components in the vector by a single number.
This means that if you have a vector, <5, 10, 15> and a scalar 5, they can be divided like
<5, 10, 15>
/5
=
<5/5, 10/5, 15/5>
=
<1, 2, 3>
On ROBLOX, Vector Scalar division can be done like so:
Vector3.new(5,10,15)/5
Vector component division
In vector component division, you divide each component by the corresponding component in the second vector.
This means that if you have a vector, <5, 10, 15> and another vector <1, 5, 3>, they can be divided like
<5, 10, 15>
/<1, 5, 3>
=
<5/1, 10/5, 15/3>
=
<5, 2, 5>
On ROBLOX, Vector component division can be done like so:
Vector3.new(5,10,15)/Vector3.new(1,5,3)
Multiplication
Just like division, multiplication is not defined for vectors either. There are four common ways to multiply vectors.
Vector scalar multiplication
This works exactly as you would expect it to. Like division except instead multiplication.
Vector component multiplication
This also works exactly as you would expect it to. Like division except instead multiplication.
Dot product
Dot product is one of the most useful vector operations which can be performed. It works a lot like component-wise multiplication except each of the components of the resultant vector are summed. This means that it returns a scalar, not a vector.
The general way to understand dot product is:
If you have a vector <1, 3, 5> and another vector <-1, 1, 3>, the dot product can be computed like so:
<1, 3, 5> dot <-1, 1, 3>
=
1*-1 + 3*1 + 5*3
=
17
Dot product doesn’t explicitly relate to triangles. In fact, even trig doesn’t really even distinctly relate with triangles either, but that’s another thing. Dot product can be best understood in terms of directions and distances, as I’ve drawn at the bottom.
It is especially useful when you have a unit vector direction u and a vector p, u dot p tells you how far p goes in the direction of u. A lot of math involving dot product, pretty much all you or I will deal with, involves this idea. For example:
How far vector p goes in the direction of unit direction u:
u dot p
If p is orthogonal to u, then it travels no distance in the direction of u. In an orthogonal case like
u=<1,0,0>, p=<0,3,0>, u dot p = 0
If p is parallel to u, then it travels the maximum distance in the direction of u. In a parallel case like
u=<1,0,0>, p=<3,0,0>, u dot p = 3
If you have a vector u of any length, the magnitude squared can be computed with the dot of itself:
If you instead have two unit vectors, u and v, the cosine of the angle between the vectors can be computed like so:
On ROBLOX, you can find the dot product with a Vector3 member function:
Vector3.new(1,3,5):Dot(Vector3.new(-1,1,3))
Cross product
a cross b returns a vector which is perpendicular to both a and b, and has a magnitude equal to the area of the parallelogram defined by a and b.