Why Do We Need Scalars and Vectors in Game Programming?
In game programming, it is important that we are able to describe what we want to do to our game objects.
We can implement and use math equations with scalars and vectors. For example, creating custom gravity onto
game objects, applying speed, and even applying acceleration to game objects.
What is a scalar?
A scalar is nothing more than a magnitude representation of a vector.
Some examples of a scalar:
Distance
Speed
Time
Mass
Length
Area
Volume
What is a vector?
A vector is a value or quantity that has a direction as well as a magnitude.
Some examples of a vector:
Velocity
Acceleration
Force
Momentum
Vector Data Type
Many game engines have some form of andling vectors, and the Godot Game Engine is no different
In Godot we have the Vector2 and Vector3 data type.
# An example of creating a variable with a vector2 data typevar vectorExample =Vector2(5,5)
var basicVector =Vector2() #(0,0)
basicVector.x =1
basicVector.y =2
print(basicVector) #(1,2)
Transcript
Hello and welcome to another episode in the Godot basics tutorial series. In this episode we will be taking a quick look at Scalars and vectors. So why exactly do we need to know about scalars and vectors. Well it's important that we can describe what we would like our game objects to do. We can use and implement math equations with scalars and vectors. Now scalars are nothing more than a magnitude representation of a vector. Some examples of scalar would include distance speed time mass length area and value.
Notice how these are just numbers and that they don't actually tell you a direction. So that's one thing to note about Scalars. They do not tell you direction. They only give you a value vectors. On the other hand or a quantity that has a direction as well as a magnitude. So basically it's not only a value it's a value with direction. So some examples of vectors would include velocity acceleration FORCE MOMENTUM AND QUITE more.
However you may find yourself only using the following when starting out in your game programming journey.
Now let's go ahead and take a look again at the Flappy Bird video example as you can see we have several different vectors acting upon our game objects.
And so we're gonna go ahead and take a look at that.
Now just by looking at the video we have a few things going on for our bird. We have a force going upwards and a force going downwards. And for our pipes you'll notice that they move at constant speed to the left and that there is a distance between pipes. So if we were to draw this out on a piece of paper it would look something like this. We would have an arrow pointing up and that would be our force going upwards. And this is a vector. And of course towards the bottom we have gravity going downwards and an arrow pointing down another vector.
And as a matter of fact we even added a position in x y coordinates and for the pipes we have an arrow going left and we have velocity going left towards and the same thing for the bottom pipes velocity going left. And notice we also have a distance. This could be considered a link and that would be a scalar value. And as you can see here in our drawing we're basically representing that there is a distance between the two pipes as they move leftward. So already you can see the value of understanding vectors and scalar is we can use them to describe our objects in the game world. And
And also note that our pipes have a position value as well. And these are changed over time based on our velocity. So many game engines have some form of handling vectors which are again quantities with a magnitude and the direction on top of that. We can also use vectors to help us with the position of our game objects in good dough. We have the vector 2 and vector 3 data type and this data type will help us out with vector equations in good. We can write our vectors in the following in the first example we have a variable a and we're sending it a vector data type in the 2D space with the values of 5 and the x coordinate system and 5 in the y coordinate system we also have another way of adding a vector.
In this example we just declare our variable A to B of A VECTOR two data type and it is empty. And by calling the x coordinate system so a dot X we can assign it the value 5 and on the y coordinate system a dot y we can assign it the value 5. And now our variable a is the same vector value as the first example of assigning a vector value to variable a.
Let's take a look at vectors and direction all vectors have a toe and ahead when we have an arrow on the arrow head is considered the head of our vector. And of course at the opposite end that does not have the arrow head is considered the tail end of our vector. When you see a vector like this you can think of it as a vector that has magnitude of X indirection e and of course if you wanted to be very technical you could say zero degrees. This is why we use vectors because we want to explain magnitude and direction.
Now if we had a vector pointing up we would say this vector has a magnitude of x in direction north or ninety degrees. And basically our direction is wherever our arrows pointing towards. Now to understand the degrees when we have a vector pointing in some direction we do use polar coordinates to our east. We have zero degrees north 90 West 180 and South 270. And of course when we complete our rotation even though it says zero we actually say 360 degrees because it lets us know that we did one full rotation our vectors at least at this point in time we are not worried about its rotation but rather the direction it is pointing to.
Now two things I want to introduce in this episode is addition and subtraction. First we'll go over additions now to vectors can be added together to get a resultant. In this example we have a vector with a magnitude pointing east at 5. And now we are adding another magnitude pointing east at 5 and our resultant is a magnitude of 10 pointing east. And these can represent different things and as an example think of this as speed of player plus wind equals new player speeds. When this case our player speed is being affected by the wind and now our new player is running at a speed of 10 instead of their default fight we can do quite a lot with adding vectors and of course we could have the opposite as well.
We could have our player speed at 5 and the wind is pointing against us and now our new speed is 2.
Now our game objects in the last episode we described it as a coordinate system in the Cartesian graph as the following. We have a value in the x axis and a value on the y axis. However when we want to represent this in mathematical terms we use the square brackets and it would look like the following we would have the x value at the top and the y value at the bottom. And when we're dealing with 3D or three dimensional systems we would have the z axis at the bottom instead. Now we can use vectors to describe our game object's position and it would be the following.
So we have our game object and it's given a value of x and y. Now one thing to note is that we describe our game objects with vectors. What we're saying is that our positional value is relative to the origin and it would look something like this. So for example if we have a vector of five and five. What we're really saying is that our vector at position 5 and 5 is relative to the origin of our game world and our origin is at position 0 0. And really what we're saying is that we have a direction and magnitude which is 5 5 and it leads us to our game object.
So what we're saying is we have a direction from the origin and it has a magnitude of 5 and 5. So that's one way to think about vectors when dealing with position in the game world. Now let's go ahead and take a look at vectors as direction. So let's say we have an enemy game object. And it's at a position of x1 and y one. And. We would like our enemy to go to the hero. Which is that a position of x 2 and White 2. Now before we can actually move our enemy we must first find out. The direction we need to go towards to
In order to hit our hero.
And that's where subtraction comes into play. We need to subtract our vectors in order to find that direction. So as you can see here in order to find that direction we must first get the values of our hero. In this case x twin y 2 and then we must subtract it. From where our enemy position is that. Which is x1 and Y what. By doing this we are able to get the direction and magnitude. Of our hero from the perspective of our
Enemy Game Object. One thing to note is if we want to find the direction and magnitude of the enemy from the perspective of our hero game object we would need to flip the value. So notice how instead of X two and y two we have X1 and y one and then we subtract those values from our heroes vector which is x 2 and Y too. And that would give us the direction and magnitude of the enemy from our hero game objects perspective. Over in this example we won't be looking at the hero we're looking at the direction from the enemy.
So let's give them values or enemies at Vector position 5 5 and our heroes at Vector position 10 5. So basically we're going in a straight line. Now we plug in our hero values 10 and 5 and we subtract it from our enemy vector values which is 5 and 5 and that gives us 5 and 0. Now one thing to note is that this value doesn't mean anything it's just a direction all this vector does is explain a direction in our game world. As a matter of fact it's completely useless by itself.
I'll give you an example. As you can see here our vector can be applied anywhere in the game world. And so because of that the value is useless. This vector by itself doesn't mean anything. It's not until we apply it onto another object that we can get some use out of it. So now we actually have to use our direction and magnitude in order to get some value to get some value from our direction and magnitude we need to apply it to some thing in the game world. And that's what we're going to do. As a matter of fact we're going to take our direction and magnitude that we solved in our last problem which was finding the direction and magnitude of the enemy and the hero.
And we're going to apply that direction and magnitude to the enemy. Game object in order to change its position to that of the hero game object because we want our enemy to move towards the hero. Now to do that we need to use addition we need to add our vectors so to enter vectors we need to get the vector value of our enemy. Game Object and we need to get the direction and magnitude that we got from our subtraction example. And so what we do is we do the following R enemy. Game Objects vector values X1 and y one and we will add the direction and magnitude we want our enemy.
Game Object to go to in this case or direction and magnitude will be the vector value of the following x 3 and Y 3 and we just plug that in here and through addition we can change our enemies position through vectors. And so if we plug in the values from our last example we have our enemy. Game object and its vector value is 5 5 and we have our direction which by itself again is useless. But when we add it to a game object we're going to tell our game object to move towards a direction and a magnitude and that's the value of addition with vectors.
And so we have our direction and magnitude which is 5 and 0 and when we add it what we're doing is we're changing our position into the new position which is 10 and 5 which is also the same vector values our hero player has or rather had and so not only can we use mathematical equation such as subtraction to find direction and magnitude between two objects we can use addition to move game object and so let's go ahead and take a quick look at some code and how to do this in Godot.
Now we're gonna do the same thing I showed you in the slideshows in Godot. And as a matter of fact it's really simple because Godot gives us a way to not only have vector values but to also use mathematical equations on our vector values. So I went ahead and in this script we have three variables an enemy a hero and a direction and they all have a vector to data type and just like our slide show our enemy is at position 5 5. Our hero is at position 10 5 and direction. We need to get that value because we want to apply the direction value on to our enemy value in order to change its position into the hero value.
So on the first line we need to figure out direction and we use subtraction subtraction allows us to find the direction and magnitude between two objects. In this case we take our hero vector value and we subtract it from the enemy because we want to know the direction and magnitude. We need to take from our enemy to the hero. If we wanted to do the reverse and find out what magnitude and direction we need our hero to go through in order to reach the enemy Game object we will reverse the values so we'd be enemy minus hero.
But we're not doing that and so we're gonna keep it hero minus enemy because we want to change the enemy's position to the hero's position next. Once we get our direction we're going to apply that direction to our enemy. Game object. And so our enemy will add the direction. So they plus. Equal. Symbol is the same as saying enemy equals. Enemy plus direction. Just in case you skip that episode in the GV script fundamental tutorial series now because we're taking the vector value of our
Enemy game object and we're applying a direction and magnitude we are going to move our. Enemy into the position of our hero value. And so when we. Print to console you're gonna see that our enemy object actually has the same value. As our hero game object. And so through vectors. And the power of subtraction and addition you do have the ability to do quite. A lot when game programming and as a matter of fact. Vectors. Are used in other game engines. And so just by knowing how to use vectors when to use vectors and how to apply vectors we can do quite a lot.
And in this series we're gonna go over them, Not yet, I do not want to overwhelm you because you're just beginners and you're just starting on your game programming journey. However. On top of vectors just know that. Any formula you've learned in your math class or in college can be applied in game programming. And we can do quite a lot. And in this example. We showed you. Or rather I showed you. How to find a direction and magnitude between two objects and how to change position. Of one object into another. As a matter of fact you don't even need to change the position to the hero position.
You could add any vector value to the enemy hero value in order to move it. Well. That's all I have for you in this episode. Thank you so much for joining me. If you have any questions please feel free to leave a question or comment in the comments section down below. Have An amazing. Day.