this video is brought to you by our own Metta thank you so much for donating if you want to support breakage yourself you can go to patreon.
com/scishow so we want to welcome to the first video in the patreon funded game math theory a series that covers essential mathematical concepts that you will definitely run into during your game development career today we will be looking at vectors before we get into how a vector is defined let's have a look at why they're useful vectors are often used when we want to move a character around the scene let's say that we want to move our character two units to the right and one unit up what we essentially do is create a vector which we represent with an arrow that points in that direction we can then add this vector on to the position of our character to make him move if we were to write down this vector mathematically we would write to one because the vector moves to one the x + 1 on the Y we say that a vector is defined by direction and a length in our case the direction of the vector is where we want our character to point and the length of the vector is how far our character should walk in that direction if we wanted our character to then move one unit to the left we would make a new vector with the coordinates minus one zero because the vector goes minus 1 on the X and zero on the Y however you will quickly notice that the coordinates only show what the vector looks like and not where we should place it in the scene that is because vectors don't have any inherent position that allows us to draw it anywhere we want it is only when we combine it with the position of our character that the vector adds anything meaningful to our example now let's look at another example where victors come in handy say we have a scene with two players player a and player B and we want to find the distance between the two well both players have a position which means where they are in relationship to the center of our level in this example player a has a position of two one because he's two units from the center on the X and one unit from the center on the Y likewise player B has a position of 1/4 because he's one unit from the center on the X and four units from the center on the Y we can now create a vector that points from player a to player B to do this we subtract the position of player a from the position of player B this is done in the easiest way possible by first subtracting the X components 1 minus 2 equals -1 then we subtract the Y components 4 minus 1 equals three and the result is a vector with the coordinates minus one three let's try to draw this in our scene we can see that the vector correct it goes from player a to player B but how do we calculate the length of this vector that is in fact really easy the length of a vector can be written as the square root of x squared plus y squared note that the length of a vector is referred to as the vectors magnitude if you wonder why the length of a vector is defined using this particular formula you can try to think of the vector as the hypotenuse of the triangle created by the vector coordinates and then use Pythagoras theorem to calculate the length of that side but you could of course just use the formula as is so if we want to calculate the distance between our players we simply insert the x and y coordinates of the vector into the formula and voila the distance from player a to player B is three point one six units the same thing is extremely easy to do in three dimensions as well all the vectors now simply have a third component the z axis which is also added into the formula at this point you might be thinking why do I need to know all of this my game engine has an easy function for getting the distance between two points in space well an example of why this knowledge might be useful to you is the fact that it has just allowed us to make a very important optimization in our code say that we are making a racing game and we have two players a and B who are competing to reach the goal and in our game we want to display who is currently ahead in that case we might subtract the position of each player from the position of the goal to get two vectors from our players to the goal we could then get the length of each vector using the form left from before and compare them to find out which car is closest however using the square root of Raider can be pretty taxing on the computer and doing this calculation a lot of times during a game might lead to performance issues since that we could simply remove the square root and only compare the remaining x squared plus y squared this doesn't give us the actual length of the two vectors but it still shows what vectors greater than the other which makes it perfect for doing a comparison between the two so that is pretty much all I have to show for this video if you're interested in learning more about vectors that's the link for that in the description hope you enjoyed the first part of game math Theory next I hope to cover the magic of sine waves if you want to support the series you can do so on my patreon page thanks for watching and I'll see you in the next thank you so much to all of the awesome patreon supporters who donated in August and the special thanks to our and Metta Robert Rogers Calhoun Vixen P and Andrew K.