Video games rely extensively on vectors to calculate and display the virtual game world. But what are vectors exactly? How do they work? In this bonus lesson, we explore some of the basic math behind vectors for game developers.This lesson is here to help you understand the math behind vectors. You don't have to remember all of it to complete the course, as a game engine like Godot provides many functions that handle vector calculations for you. But it can help you understand what the engine does when you write code like direction.normalized() or add Vector2 values.As you'll see in our bonus lessons, we use math terms sparingly. We explain things in a way that makes sense to game developers. And we'll mention the math terms in parentheses when relevant.Finally, this lesson is not a complete introduction to vectors. It's a quick overview of the concepts relevant to this module and the next few in the course. You'll find more guides throughout the course as we introduce new concepts.We'll cover:
What are vectors?
How 2D vector coordinates work
Common vector operations (addition, subtraction, multiplication, etc.)
Normalizing a vector
What are vectors?
In math, a vector is a list of numbers representing a direction and a length (called the vector's magnitude).In math, a vector can be a list of numbers of any length, though in games, we mostly use vectors to represent 2D and 3D coordinates. So, we mostly use vectors with 2 and 3 numbers. You've seen this in the course when we used the Vector2 values to change a sprite's position.A 2D vector has two values representing the x and y coordinates, while a 3D vector has three values for the x, y, and z coordinates.Let's focus on 2D coordinates. We can represent the coordinate system of our game as two perpendicular axes, one horizontal and one vertical. We call the horizontal axis the x-axis and the vertical axis the y-axis.In games, the y-axis is usually inverted compared to math classes, meaning that it points down. If you go to the right on the x-axis, you increase the x value. If you go down on the y-axis, you increase the y-value.
2D vectors are the perfect tool to represent 2D coordinates, like the position of a sprite, the velocity of an object, a force, a direction, and more.But how do they work exactly?
2D vector coordinates
2D vectors are composed of two numbers, typically called x and y.In games, we use these numbers to represent coordinates. The top number, named x, represents a coordinate on the horizontal axis, and the bottom number, named y, represents a coordinate on the vertical axis. In 2D, the coordinates are usually measured in pixels.We use 2D vector coordinates mainly in three ways:
For position: These vectors represent the position of an object in the game world. For example, a position of Vector2(400, 200) represents 400 pixels to the right and 200 pixels down from the origin position, Vector2(0, 0).
For directions: These vectors represent the direction in which something points. For example, the value Vector2(1, 0) represents the direction to the right, while Vector2(0, 1) represents the direction down. Usually, we ensure that these vectors have a maximum length of one to represent only a direction and not a distance.
For velocity: These vectors represent the speed and direction of an object. For example, a velocity of Vector2(400, 0) represents an object moving to the right at a speed of 400 pixels per second, while Vector2(0, 300) represents an object moving down at a speed of 300 pixels per second.
So, when you see a 2D vector, one way to visualize it is as a point on a plane. That's a position.The position where the axes meet is called the origin position. It's the position Vector2(0, 0).
Another way to visualize it is as an arrow. This arrow starts at the origin position, Vector2(0, 0), and points in the direction represented by the vector. The arrow pointing down and right represents the same value as above, Vector2(200, 100). It is just a different way to visualize it.We use this arrow representation for directions, forces, and velocities.
You can manipulate vectors with various operations, including:
Addition and subtraction: You can use this to move a sprite or a character in the game world.
Multiplication and division: You can scale vectors by multiplying or dividing them by a number.
Adding two vectors
Adding two vectors together is like moving step by step in a game world. For instance, if you have a character at position Vector2(200, 200) and you want to move it 50 pixels right and 100 pixels down, you would add Vector2(50, 100). The resulting vector, Vector2(250, 300), is the new position of the character.Mathematically, you add the corresponding components of each vector:Writing Vector2(a, b) + Vector2(c, d) is equivalent to Vector2(a + c, b + d).
Subtracting two vectors
Subtraction is similar to addition but in reverse. We use it to find the difference between two vectors: The distance and direction from one point to another or the difference between two velocities (as seen in the steering ship lesson in this module).Vector2(a, b) - Vector2(c, d) is equivalent to Vector2(a - c, b - d)
Multiplying and dividing a vector by a number
Multiplying or dividing a vector by a number changes its length but not its direction. For example, if you want an object to move twice as fast, you can multiply its velocity vector by 2.Vector2(a, b) * number is equivalent to Vector2(a * number, b * number)Similarly, dividing a vector by a number reduces its magnitude. We use this operation to decrease the speed or scale down a vector.
Normalizing a Vector
Normalizing a vector means removing the length information from the vector and keeping only the direction. The vector ends up with a length of one unit. We use this to ensure that a vector only represents a direction and that when used in multiplication, it doesn't cause objects to move faster or slower (multiplying a value by 1 doesn't change it).To normalize a vector, divide each component (x and y in a 2D vector) by the vector's length. This way, the length of the resulting vector is 1.0.Note: The length of a vector Vector2(a, b) is sqrt(a² + b²).In Godot, you can normalize a vector using the normalized() function. For example, Vector2(100, 0).normalized() returns Vector2(1, 0).
Calculating the length of a vector
Let's break down the math behind calculating the length of a vector. For that, we use the Pythagorean theorem. It states that "the length of the hypotenuse of a right-angled triangle is equal to the square root of the sum of the squares of the other two sides."A vector is like the hypotenuse of a right-angled triangle. The x and y components are the other two sides of the vector.Here's an illustration of the Pythagorean theorem applied to an input vector pointing to the top-right:
We calculate the length of a Vector2 value using the following steps:
We square the x and y components of the vector.
We sum the results.
We take the square root of the sum.
Here's an example with the value Vector2(50.0, -20.0). The calculation is as follows (below, ^ stands for "to the power of" and sqrt means "square root"):
sqrt(50.0^2+(-20.0)^2)=sqrt(2500+400)~=53.85
The vector has a length of approximately 53.85.Here's another common example, with an input vector pointing to the top-right. Its value is Vector2(1.0, -1.0). The calculation is as follows:
Use this space for questions related to what you're learning. For any other type of support (website, learning platform, payments, etc...) please get in touch using the contact form.
What does SGx stands for?KhoHi, am a baguette and not super strong in english but i can guess Mx stands for Module x, Lx stands for Lesson x but what does SGx stands for? It won't go off my head x) Thanks for the answer.46Apr. 06, 2024
Recommended Khan Academy courses for vector math?SingleMom420Hi Nathan, in a previous comment you recommended the Khan Academy app. I downloaded it but being as math-illiterate as I am, I'm not too sure *which* courses within the app I should be studying. For now I picked "basic geometry and measurement", "algebra basics", and "trigonometry" because those sound vaguely vector-ish-related to me, but I'm really not sure. Could you tell me the "categories" of math I should be studying to reinforce my ability to better understand the underlying mechanics of GDscript?33Feb. 21, 2024
Let me get this right...NoneApplicableGamesFirstly I would like to take a minute to appreciate the bigger focus on the underlying theory in this version of the course. It gives it much more value that learn to code with godot 3, especailly if you've already completed it.
Secondily I just want confirm I've understood the common vector operations:
> Addition and Subtraction of a vector will cause that object to be place a number of pixels way from the object's current position.
> Division and Multiplication will scale a vector meaning its direction will not change, but its length will.
> Normalising a vector will strip it of its length, but will keep the direction.13Feb. 20, 2024
How to increase the speed gradually!AJ StudiosSo when I normalize a Vector2 for instance, let's say: **`Vector2(300, 0).normalized()`**
This will return `Vector2(3, 0)` How would I go about moving an object using that vector and then start increasing it's speed gradually?
I imagine that some kind of multiplication will be involved but I can't figure out how the speed will increase over time.72Feb. 07, 2024
useful materialBelinHi all, if you want to know more about vectors I found these amazing course online specifically on math for games. Unfortunately the exercised are on unity, but the lessons are applicable to anything and really good
[https://www.youtube.com/watch?v=fjOdtSu4Lm4&list=PLImQaTpSAdsArRFFj8bIfqMk2X7Vlf3XF](https://www.youtube.com/watch?v=fjOdtSu4Lm4&list=PLImQaTpSAdsArRFFj8bIfqMk2X7Vlf3XF)
Thank you guys for the amazing course btw!21Jan. 04, 2025
Length vs Directionbc likes youHey this is great supplemental material! One question I have is getting a bit lost here:
> Multiplying or dividing a vector by a number changes its length but not its direction.
I'm a bit confused on the difference between length and direction. If we're considering velocity, can we think of it as direction is down if we're using Vector2(0,1) and length is the speed at which we move (1 pixel per second)? So multiplying wouldn't change the direction, just the pixel per second at which we move?61Feb. 28, 2024
Understanding order of operationsMartinBIf I multiply a Float by a Vector2 and assign it to a new variable, does that variable become a Float or a Vector2?
I.E.:
var desired_velocity := 600.0 * direction(1, 0)21Feb. 21, 2024
How godot calculate x and y vector2rifqilubis So godot know if user input Vector2(1,0) it means he go to the right. and
(1,1) is going to down right. and (1,-1) is up right
Now the question is. how godot know how to get up right or down right? Cause godot only understand from length of the vector right??
like, Vector2(1,1) is 1.4 length. so he go a bit faster. down right
but with (1,-1) he go up right with length 1.4
does length mean speed and vector(1,1) means direction only??
i know vector(x,y) means move down and right if positive and up,left if negative.
i only confused with length and how godot know, how to calcute the direction, cause if length is 1.4 the sprite is going faster than usual. 70Dec. 06, 2024
sometimes i forget about mathvariable-skunkhow to understand square root ? sorry my mind didn't get math10Oct. 30, 2024
How important is knowing the fundamentals of math in Game dev?different-turtleDue to personal circumstances I was not able to attend school for several years and thus have only been taught very basic math, like multiplication, addition etc. I find the coding parts of this course to be logical, and I can follow along at a pace I feel comfortable with. I guess, to me it feels like I can follow the logic even without prior knowledge, if I study the course material enough. But when it comes to math, I feel completely lost in a way that is pretty disheartening to be honest. For example most of the mathematical terms used in this bonus lesson mean nothing to me and only serve to make me feel more confused. Maybe if I didn't have that annoying drive to understand everything it would be easier to let go hehe...
So I guess the solution to this would be to study math, but it feels to me like an insurmountable task, and one I'm frankly not that interested in, outside of things that are directly applicable to game dev, I suppose.
My question is, how important is it as a game developer to have a firm grasp on the fundamentals of mathematics? And if there are mathematical concepts or similar that you think are important to game dev, what are they, and do you know of a good way to study them that works for someone without much prior knowledge at all?
I am very much "I just wanna make games" person but if learning something could make me a better game developer, I would at least like to try to learn it! I'll also try asking people around me who are more experienced in math and see if they can help explain things. (For an example from this lesson, I understood pretty much nothing about the length of the vector).
Thanks, in advance!
Ps: Aside from this minor personal hiccup, this is the best course I've ever taken and the fact that it works for someone like me, who struggles a lot with concentrating and exhaustion/mental health issues is something you should be proud of. This course made my dream of making a game seem like an extremely difficult BUT achievable task, when I had previously given up on it.60Oct. 13, 2024
Normalize: We divide x and y by the vector's lenght?PurpleSunriseHello,
I just want to understand if I got this.
I get it that to find the length of a vector we have to elevate x and y, sum them up and calculate the square root of the result.
> To normalize a vector, divide each component (x and y in a 2D vector) by the vector's length. This way, the length of the resulting vector is `1.0`.
If I take the example, the length of a Vector2(1.0,-1.0) is 1.4 something right?
I tired these steps to check if the length if the normalized vector is 1.
**Normalize the vector:**
Vector2(1.0 / 1.4, -1.0 / 1.4)
The new Vector2:
Vector2(0.7, -0.7)
**Find the length of the vector:**
sqrt(0.7^2 + (-0.7)^2)
sqrt(0.49+0.49) *I guess the result is rounded to 0.5 if we take in account more decimals than the first 2?*
So it's sqrt(1) which is 1. Is this the correct process?
Just want to make sure if I got this right.30Oct. 09, 2024
I dont know if I am learning correctly.weighty-raccoonHi, I purchased the course a week or so ago and movement in general is kind of confusing to me. I don't think I am learning but rather remembering, I tried to make a movement system by myself in a seperate project and the movement system worked fine, but , I was just writing the code from memory without really understanding what's going on... Are there any ways I could possibly "learn more effectively"? I understand some things but not others so what would be the best steps to really get a good grip on learning those new things?10Sep. 27, 2024
I understand nothing.Nikita MyshkinFor two full days, I sat with Module M4 trying to understand how it all works. But at the end of the second day, I still don't understand anything, especially L5. I really liked how you visualized the code, but I look at it and just pretend I understand something. I got to L6 and thought I would find a lifeline there. Throughout the article, I thought I almost understood everything and returned to the code, only to realize I misunderstood everything.
It's hard to explain how it happens. It's like you look, think you understand, then look closer and realize you understood everything wrong. I can't even formulate my questions because one overlaps with another, and then another one follows. Even GPT doesn't help.
In general, I saw that you wrote at the beginning of the course that if something is not clear, you can confidently move on. But I feel like I understand so little that in the next modules, I'll just drown in ignorance. Should I continue or is it better to try to figure it out further?
Sorry for my English.50Jun. 21, 2024
Do you plan to cover Transforms in your 3D course?lowenI think they're kind of important and I had a lot of issues in game dev without knowing too much about it.10Feb. 08, 2024
Lesson Q&A
Use this space for questions related to what you're learning. For any other type of support (website, learning platform, payments, etc...) please get in touch using the contact form.