How to think about Vectors

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>.

image

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

image

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

image

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:

image

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

image

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

image

If you have a vector u of any length, the magnitude squared can be computed with the dot of itself:

image

If you instead have two unit vectors, u and v, the cosine of the angle between the vectors can be computed like so:

image

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.

22 Likes

It might be worth mentioning Desmos, an amazing math visualising tool (that you’re using as well). Practicing vector based mathematics in Desmos helped me understand them much quicker.

[sup]and here is something I made a while ago but screwed up bad[/sup]

you forgot magnitude, unit, Dot, and Cross :slight_smile: I use them just as often as I use vectors themselves, and they are the ones that confuse people the most

Edit:
And unless I’m wrong, I was taught in linear that sets are defined with [x,y,z,…], not <x,y,z,…>
Sohcahtoa won’t be a bad idea to post along with this

[quote]

Don’t worry.

I don’t feel so bad about myself now because I actually understood all of that

(even though I only learned about visually adding vectors a day ago)

Though, I don’t know anything about :dot, :cross, etc, so looking forward to that.

[quote] It might be worth mentioning Desmos, an amazing math visualising tool (that you’re using as well). Practicing vector based mathematics in Desmos helped me understand them much quicker.

[sup]and here is something I made a while ago but screwed up bad[/sup] [/quote]

Click on the pictures. :wink:

[quote]
[sup]and here is something I made a while ago but screwed up bad[/sup] [/quote]

I made ur eye thing.
https://www.desmos.com/calculator/qnlldtiday

1 Like

I don’t see why whenever there is a post with graphs and math everyone thumbs ups it, this post hardly taught someone who knows nothing about vectors anything … I have never really learned much about vectors and what not (9th grade, geometry), but once a concept is explained I generally always understand it… that being said this post wasn’t really clear as to what exactly the dot product does in programming or what it even represents relative to vector a and b.

Anyways everything else on the post was pretty cool I just thought it would be better if you had clarified what dot product is more and what use these vector operations had when really working on roblox. Just some criticism on the tutorial, no hate. BTW Thanks for making this because it really pushed me to further understand how vectors work and I thank you for that, and now with the understanding of dot product + vectors I imagine there will be tons of uses for it!

EDIT : Also if you do plan on editing the tutorial and adding more, it would be useful to explain how dot product and triangles relate

2 Likes

[quote]

You’re welcome! Now, if I can just respond to

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:

Finding the closest point to vector p on a ray composed of origin o and unit direction u:

Finding the closest point to vector p on a ray composed of origin o and a direction u of any length:

Finding the intersection of a ray composed of origin o, direction d of any length, with a plane composed of origin p and normal n of any length:

Anyway, as you can see, there’s nothing to do with triangles conceptually or mathematically. These are some of the most common cases where you’ll need dot product. If you can remember these, you can pretty much do anything you want to with vectors without much effort, excluding cross product, which is something for another time.

3 Likes

Matrixes would be great, they are the only concept that I can’t work my head around…

[quote]
And unless I’m wrong, I was taught in linear that sets are defined with [x,y,z,…], not <x,y,z,…>
Sohcahtoa won’t be a bad idea to post along with this [/quote]

I was taught in “Linear Algebra for Engineering and Science” (MATH 1104 at Carleton) that you use square brackets. Those other signs would just be confusing when you’re running comparisons.

Corrected the format of the original post. (whoa Alex’s reply was 523 days ago)

1 Like

that is hypnotizing! This thread has opened my eyes to desmos - thanks!

Awesome article! You explained it better than how my school physic’s book did! :smile: