Vectors & Dot Product • Math for Game Devs [Part 1]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
my name is freya i have been in the industry professionally for about like nine years at this point maybe 10 years i also used to be part of future games i was in the class of 2010. i've been teaching at future games since like after two years after that i started teaching at future games so well i did mostly like c-sharp courses and math courses um as well as some guest lectures otherwise like professionally i made a plug-in a long time ago for unity called shaderforge um so it was basically a node-based trader shader editor for unity which didn't exist at the time there was one that was like outdated but it wasn't maintained and the person who worked on it was hired by unity and then they kind of started working on you know shady graph internally which took a very long time uh luckily for me um because then i could make shade of forage um so i sold shader forge on the asset store which made like enough money to be two full-time salaries pretty much so me and another student at future games we started a studio uh so we started an indie game studio um called neat corporation so at knee corporation we kind of started out wanting to make our own like very specific indie game but then at gdc we were sort of we sort of like bumped into valve right as they were releasing their um htc vive headset so we got kind of inspired by vr as soon as that kind of happened so as that exploded the whole vr scene we got to be there like at the very very beginning when everything started like growing which was really cool um and also made us realize how like going to gdc and just talking to industry people can lead you down so many pads and like just that connection with people is so important if you want to like make connections in the industry uh start new opportunities and whatnot and all it takes is kind of like you have to try to be available for all of these things anyway that was the story of nikkor um how we sort of started nicor and started working on the game called budget cuts so that was our first kind of flagship release title one thing that i just need to mention before we get started in terms of like my approach to teaching like my goal is that i'm here to help you learn right that's kind of that's kind of it um so so usually get a lot of questions along the lines of like you know do i have to do this assignment and so forth um the short answer is yes um because you're gonna have christopher do all the grading and all that stuff um but the long answer is that i personally um i don't care what assignments you do or don't do or anything like that my goal is that you should learn the things that you want to learn right and i'm here to help you do that but i do i do want to stress that it is important though like what kind of like what kind of standard you're setting not only for yourself in terms of like what goals you have and what you want to learn and how you learn but also how that reflects on you in the eyes of your your fellow students right or even in the eyes of me right if i um if people around you notice that you are someone who uh is like doesn't do any assignments or like show up late i've heard every lecture then i think a lot of people like are going to think of you as that person that's always late and doesn't like deliver or whatever um and you're all going to be in the industry soon right like if you think about my classmates pretty much all of them are in the industry now so you know i will remember people who were kind of like not great so like it's important to kind of like be a good person and um be like kind of project the image of you that you want others to see once you're in the industry right um so i think it's important for like from that from that point of view to actually do um do assignments and whatnot to at least be the person who did the assignments right uh but then again personally i don't care that much um i just want you to be able to learn the things you want to learn right um if you hate math you don't think it's going to be useful then then sure feel free to not do anything right um but i think it would be detrimental for you um especially if you're if you want to do a lot of coding what will the assignments look like uh the assignments will be uh mostly to do some mathematical thing in unity uh it's not going to be very unity centric unity is mostly going to be there as are like platform and rendering engine um so it could be things like i would do the math for the spread of bullets in a shotgun or whatever like that kind of stuff so they're going to be pretty short i intend to do like more than like one assignment per day so you can have like like three tier assignments like one easy one medium and one harder task and then you can sort of do however many you want i think depending on how christie wants to grade this uh i'm just gonna send the like answers to christopher and he's gonna work on the grading and stuff i usually like having a tiered like assignment thing because it tends to be more flexible and you can sort of do things step by step which i like all right um another very important thing please ask questions there are no stupid questions i i think it's like one of the most detrimental things for you to do is to like you have a question in your head or something i said was unclear please ask that question that you have in your mind it's so important um because like usually when people like almost every time someone says this might be a stupid question but they ask a really good question so please keep that in mind ask questions don't be afraid to ask questions um asking questions doesn't mean that you're stupid it means that you're actually smart because you actually want to learn the thing and want to understand the thing that you you are observing and trying to pick up brain but of course try to stay on topic like don't go like too far off um we're gonna have a lot of breaks so we're probably we can do like you know random q a soft during breaks if you want okay oh obviously sometimes like some people can get very stuck on one thing in that case if like one person gets very stuck on something then we can probably like talk about that after the lectures to like clarify any anything so in case it like takes too long to like uh try to hash that out all right any questions so far sorry we haven't like started the actual lecture i've just been rambling about random stuff uh will it bring in linear algebra i believe so yes um i mean yes we're going to talk a lot about vectors dot products cross products all that stuff um and matrices too all right so um let's see shuffling around my notes oh if you want to we could look over some of the things you're going to see in math papers um i don't know if you want to like have some tool sets for like being able to parse papers i could plan to plan that in probably not for today but some of the other days because we do have i do have a bunch of like time that i haven't like planned in because usually things take longer than you expect sometimes they're way shorter and so forth all right so i first want to talk a little bit about like why i think math is cool uh i think math sort of has this bad reputation because i think a lot of in a lot of cases it's taught as kind of just the boring thing you have to do in school that has to do with numbers and you have to like um i don't know it seems very esoteric and the most common question that students ask is like when am i ever gonna use this in my life right uh like when do i need the cosine you know um like that that is extremely common for people to to feel like that and when it comes to math but i feel like game development is sort of one of the like one of these strongest cases where math is just like extremely useful like absolutely everywhere in uh in doing uh game development not only like in terms of gameplay code but also like a lot of it is in like rendering and all that stuff right um but i think the the problem with teaching math in general is that i feel like um people who teach math are sometimes like excited about math but they don't really they kind of presume that other people are already excited about math and then they don't really like show it in an exciting or interesting or visual way so then it just becomes this esoteric thing where you have to trudge through a bunch of numbers and it's just boring right um so i think that's a that's a shame i feel that like mathematics is incredibly fundamental it's like one of the most fundamental fields of study that we have and like they're like it kind of doesn't matter where you are in space like you could be an alien species somewhere um you could be like absolutely anywhere it doesn't matter it doesn't have to be on earth doesn't have to be this society you can have other numbers of dimensions but math is still going to exist right it's going to be called something else numbers are going to be presented in a different way people are going to use different symbols but math in and of itself is going to exist because math is like it's the like the study of quantity uh it's a study of space it's study of structure it's the study of change and it's kind of hard to imagine a universe where any of these things just don't exist um like what does it even mean to have a like what does it even mean to have a universe that doesn't have quantity as a concept right um and and i think that's really cool it's kind of this like field of study where you can sort of co-discover things like you know the the circle constants uh like pi and tau e uh the golden ratio like all of these things are probably discovered in other societies presuming they exist um even though we're entirely disconnected from each other just because it's this study of the very fundamentals of kind of structure and change and that type of stuff um yeah and so so for me i feel like the um it kind of underpins like physics that underprints chemistry biology none of these things would really exist without math working as well as it does and being applied to these fields right it's similar to philosophy i feel like philosophy underpins everything in like social sciences it underpins uh morality ethics politics philosophy sort of the groundwork for all of those things and that's kind of why i also like philosophy but i think it's really cool that this is so fundamental and that's why i think math is cool um they're also like you of course can't like apply it to absolutely everything um because usually people say stuff like math is universal language uh but you know it's kind of not universally applicable to every situation out there uh anyway so that's all well and good but i'm already i'm already interested in math and you might not be um so i feel like we should go into the like practicalities of what it actually means to use math in in games um and how everything works i also think people overestimate how much math you need to know to do cool you can get so far with a few basic principles and sometimes yes um i i keep saying that you don't need to know that much math to do game development if you want to like make an engine you need more but if you just want to do gameplay coding or even shader coding to some extent you don't need that much math so i'm going to go through most of the like the very essentials of game development math um like even things like derivatives or integrals they are kind of rare in game development even though they're very fundamental in teaching math in game dev you don't have them very often and like mostly if you're doing some very like um very math heavy rendering stuff that's when you start running into um especially integrals uh but otherwise like generally like linear algebra is like most of uh most of game dev math understanding derivatives and integrals is really useful for anything related to implementing physics yeah so there's the idea of what a derivative is is really important but you don't really need to know how to derive equations right um so like generally in game dev unless again you're coding a game engine uh you're you're going to be using existing physics packages you're generally going to do things on a per frame basis so like the understanding of it is really important but to actually derive equations isn't that important sometimes you will run into it but i don't think i think i might have run into it like once or twice throughout my career but yeah understanding it in terms of like yeah like you mentioned like time dot delta time that we're gonna go through because that's really important um but in terms of like actually deriving equations you don't really do that much so i feel like there there is a concept that is talked about very early on in your math course and usually it's referred to as the number line so you can imagine this like one dimensional space we can just move along this one line right and in the center we can put the number zero right and then you can sort of imagine a space where these numbers are um kind of put along this whole line right all right so we have one two three so all integers are on these little notches all right um cool so this is a number line it's it's like i think it was like usually this is mentioned very early on in your math courses and then it's kind of like forgotten uh but i think it's an extremely useful place to be because it very like beautifully illustrates um kind of the continuity of numbers and like where the integers are and so forth right um i hope i got these numbers right i feel like i'm gonna make so many weird mistakes and so all right this is this is correct right okay cool so we have zero in the center and then one two three four five so we could we have our positive numbers on this side and negative numbers on this side and this continues to positive infinity and this continues to negative infinity right uh so this is a useful way of thinking about it because you can have numbers that are between the integers right so so these whole numbers are integers and then between those uh you can have decimal numbers right so you have 0.5 here so that's halfway between 0 and 1 and so forth right okay so so now one thing that's very important whenever you're working with math especially in game dev is not to really think about numbers in terms of just being numbers and because numbers um is only there to represent something else right um so so then you can ask like okay what is this number what what is it like what is it what is it a substitute for right and what is it representing um so usually if we have a number let's say we have number two and we can mark it with a dot there so so what does two mean here well we could interpret it in many different ways um two could be uh it could be a position right maybe this is the position along this line in which case this is going to be a coordinate system right where every number says the position on this one dimension right um so we could call this the the one x axis right so this could be interpreted as a position um we could also interpret it as a vector right uh it could be an arrow usually going from zero because vectors don't really have a root of quote-unquote the arrow they only have a single point right so this is a vector uh it just has the value too right and because it's one-dimensional we can only we can only have vectors that point either to the right or to the left right um so we can interpret two as a vector uh we can can do another number let's put three here um and then this is another vector uh we can do another vector for negative one right um so this is sort of a one-dimensional interpretation of vectors um but then we can ask like lots of questions about these right um we can ask how um let's do another vector actually let's do negative three okay so so usually when you work with vectors you work with a lot of concepts um in terms of like length and direction and that kind of stuff and usually you kind of start doing that once you go to two dimensions but all of these things exist in one dimension too this is not exclusive to like two dimensions and above right so if you look at these vectors right here what is the length of these vectors um well um this one that's pointing to two um this one has a length of two right uh the this one has a length of three but if you go to these uh these also have a positive length because the lengths are always positive right this has a length of one and this one has a length of three as well um so yeah so so this is basically a just a one-dimensional interpretation of what vector length is right also i didn't draw these exactly the same size but you know what that's okay you get the idea i hope all right so what about our direction what is the direction of of these vectors um well if you think about direction in one dimension usually directions have a length of one and so in one dimension you can only have directions that are either negative one or one um sometimes that's usually called a sine uh when you're working in one dimension usually it's called a sine sine of x so usually this is the like one-dimensional direction right basically it's only positive one or negative one uh there are some like exceptions if x is zero uh if x is zero uh then it kind of depends on the implementation um in some cases it's going to throw an error uh in some cases it's just gonna return one and so forth but usually it'll either return zero one or an error um when you pass into zero anyway so so the sign of these numbers uh is the uh the direction it's pointing pretty much and if you want to get the length of these actually maybe i should keep them sine of x is basically the direction um so this is either negative 1 or one mostly random rambles in the in the student chat no questions yet so zero vector has no direction yes uh it sort of like depends on your implement or like implementation and interpretation of like what it's used for um like in some cases it is useful to interpret the sine of x to be one uh because quite often the you use the direction to multiply something to like sort of change the direction and then scale it by some value and sometimes when the value you're checking the sign of is zero sometimes you want it to be one but generally it depends on um generally it depends on like the case right but again the like purely mathematically a zero vector doesn't have a direction as far as i'm concerned all right so so this is kind of the direction of a one-dimensional vector it's usually called sine again for one dimensions these are sometimes called scalar scalar values and so forth then again the the length we talked about like the length of these vectors because the the actual values of the vectors like the value of this one is negative three right uh but the length of it is three so so to get the length of a number you basically just um you get what's called the absolute value uh which is just a fancy way of saying if it's negative make it positive like that's it um so so the absolute value usually abs and math libraries um that's the that's the length uh sometimes called magnitude but i feel like i'm the only person in the world that calls abs for scalar's magnitude but i think it's funny if sine x can only return one number when it uses a float it does return a float we are not talking about two-dimensional vectors yet we're only talking about one-dimensional values which is floats or a scalar or a single value um so we're not we haven't gone into two-dimensional or three-dimensional vectors yet can you sign a 2d vector what if the vector has one negative value and one positive um so so generally the the general case of a sine is that it gets the direction of the vector um the the sine of a two-dimensional vector would would be itself a vector right because it's a it's a two-dimensional direction um but we're going to get into 2d vectors i just want to start out with one dimensional stuff but i think it's kind of neat that you can have a one-dimensional interpretation um that is still vector based so so we're still just on the number line we don't have any two-dimensional vectors here all right so then we could ask a lot of questions like what does it mean to um let's say you ask some question like um what is the what is the distance between uh what's the distance between one and three i'd like like what what does that question mean well uh generally a distance would be we want to get this length here between these two numbers uh so that would be a way to get the distance right um so so if you want to do if you want to get the distance between um these two numbers then generally you would you would have the two numbers represented as like well we could represent them as just letters right so in this case we want to do three minus one um and then that gives us two right so then we get the distance between these two numbers so usually um doing uh like subtracting is usually it can be interpreted as getting the difference between two values right uh so so if we want to get the the distance between these two values generally you do one minus the other but if you like flip the order of these if you do one minus three you're gonna get negative two right um so so in this case we actually want to uh we wanna make sure that we get the length and the magnitude of this right because if we do 3 minus 1 what that gives us is that we basically take this vector this is 3 right because it lines up here this is the the one-dimensional vector representing the value three and then we want to subtract the vector one and the vector one is this vector right um and the way to kind of interpret subtraction is that you can sort of take this arrow and you can subtract it as then you flip the direction of the arrow and then you get a new value that stops here right and that's going to be an arrow that goes here and this is the value two right all right so but then again this is order dependent this presumes we have the three before the one otherwise we're going to get negative two uh so if you ask the question what is the distance between these two values if you say negative two that's going to be a bit of a weird answer um depending on the implementation there are some cases where you want to get what's called a signed distance um and again signed distance is referring to the fact that it can be either negative or positive but generally distance values are always positive um same thing with area if you want to get like the the area of some polygon then the area is considered to also always be positive except if you want a signed area so assigned area just means that it can be either negative or positive um so you're probably going to going to hear a lot about like stuff like signed distance fields which is a term that has kind of exploded for the past few um months it's just a fancy way of saying you have a field of distance values that can also be negative um all right so if we want to get the the actual true distance between these two things uh as in the positive distance uh we would need to get the magnitude of this right so that would be the absolute value right so so generally this is the way to get the distance between two values or more generally if we want to generalize this we can say that the um let's see the distance between let's say we have values a and b we're still only talking about float values or scalars one-dimensional numbers um so we want to get the distance between a and b all we need to do is do the absolute value as in the length of the difference between a and b um so now we have the difference between a and b and then we get the length of that um and then we get the distance value um okay everything clear so far also realize i haven't talked about edition that might be a good thing addition is probably good too no so um so if we want to do three plus one um let me just erase things it's a little messy um let's say we want to do three plus one then in a one-dimensional interpretation of vectors then this is three right uh and then we can have a another vector this is one so if you want to do three plus one one way of interpreting that is that it's kind of like taking the vector and kind of just adding it on top of the other vector so now we have three and then plus one and the final vector we get out of this as sort of the result of this equation is this vector going all the way here which is the value of 4. so that's kind of like how addition works uh previously we did subtraction when we wanted to know the difference between these two in which case instead of adding this to the top we negate it and then and then add it right or just subtract it right um yeah so then doing that we ended up with a vector at 2 right because 3 minus 1 is 2. as far as i know i'm bad at doing math like i mentioned before i might be wrong um okay cool any questions so far sorry for the like weird one-dimensional vectors but right now we're only talking about scalars we haven't gone two-dimensional yet yeah so so far we've learned that three minus one is two that's how that's how well this math course is going what's the difference between length and magnitude nothing it's the same thing 3 plus 1 equals 4. that's true we did learn that too this is story the void owl now buddy you don't have to use your claws oh i missed a question uh will we go deeper into quaternions later on in this course no we're i'm going to talk about how to use rotations uh but i'm not going to talk about how quaternions work internally because that's not relevant you don't need to know that in order to make games um because we're not going to be engine coders we're going to make games right so i'm not going to go into like how to write a quaternion class but i'm going to talk about how to use unity's quaternion class uh how to apply rotation how to think about rotations what they are and so forth what's the difference between quaternions and rotors mostly an interpretation difference as far as i know so uh let's see there are a few things we haven't talked about yet so we've talked about some of the operators you can do uh you can do the sign we'll get you the quote-unquote direction of a one-dimensional vector or a value which is either just negative one or one abs will get you the magnitude of a vector which means that if you have a vector or value of negative four abs of the abs of negative four is four right it just makes negative values positive in terms of uh one-dimensional vectors or floating point values or scalars right it's a little different for two-dimensional vectors and we're going to get to that and then we have the the distance uh is you get the difference between two values it could be like we had before three minus one or yeah three minus one so that would be two if it's negative two or whatever then abs will make sure that it's positive and that's the way to get the distance between two values so if we want to get the distance between negative two and two like we want to get this distance um then we can just do that formula up there right and then it will work out uh we talked a little bit about adding and subtracting as well um but the next thing that we should talk about is like what is it what does it mean to multiply a value right um so usually the way that i like to think about it is that um when you're adding something addition is an offset addition is to move something or increase something by some amount right um so i usually think of addition as an offset and multiplying as a scale so we can scale something by multiplying something by some other value right uh so so let's say we we have the the number two right uh we have the number two and then we wanna multiply it by some other value uh let's say we we multiply it by by two so if we multiply that by two um what this means is that it's going to be twice as long um so that's gonna go from being on two to being a four so the result of that is 4 right so what that means is that because we have multiplied by 2 the length of this vector is going to be twice as long right because again if you um if you do abs of this one and like check the length or the magnitude um it's now going to be twice as long as it was before and this relationship holds for any other vectors too it's not just for scalar values or one-dimensional values okay so and then we can do we can do another example so we can do two uh multiplied by 0.5 um so so this is basically uh half a length right so 0.5 is 50 so what we're going to do if we multiply 2 by 0.5 is that it's going to be half as long right so that's going to have a value of 1. because 2 multiplied by 0.5 gives us 1. and and the the same thing is um it's the same thing when it comes to dividing right so this is the same thing as doing two uh two divided by two um so so multiply and divide are kind of the two sides of the same coin just like um so for instance let's say we have um a plus b um and then we have a minus b so if you think about these these two situations um like having a um like subtraction all that is is kind of like you're adding a negative number uh so so another way of interpreting this is that instead of looking at it like this we could just say that this is the same thing as a plus uh a negated version of b right and negation is is not the same thing as subtraction right so this uh flips the sign of it a negation means that if it's positive it's going to be negative if it's negative it's going to be positive and so so like addition is is generally kind of like or subtraction is sort of a special case of addition right actually let's do no let's do multiply so a multiplied by b and then we have a divided by b right uh so so in this case uh this is sort of also a special case of multiply right so in this case it's actually a multiplied by the reciprocal of b as the reciprocal is basically one divided by um by the value so these two are the same thing right um so so you can sort of think of addition and multiplication as the core things that you do where when you're doing subtraction or division it's kind of like a special case uh where you're kind of multiplying by the reciprocal of something uh or in in this case you are adding the uh the inverse of something right anyway sorry that was a bit of a tangent anyway i just think that's kind of cool probably not super relevant to know but usually the point is you can kind of convert between these two you can you can translate a multiply to a divide if you want to or divide into a multiply um yeah okay that's a big word what's a big word well all this meowing in the background i had to bless our chat with them with thor emoji that's a great emote thank you so much marcus um oh reciprocal yeah oh sometimes it's called one over as well but yeah like sometimes there are actually cases where instead of dividing multiplying can be like slightly faster in terms of like computationally but usually that goes into the category of like unnecessary micro optimization i guess it depends on what you're doing but yeah um okay um cool let's see are we i think we think we might be ready to move into two dimensions um so let's erase a bunch of things don't need that these are kind of weird why are these numbers marked why do those numbers matter it's a little unfair actually can you explain quake engines inverse square at some point um what part of it i'm not super familiar with the actual maths of it um but as far as i know it was like um it was an approximation of doing the inverse query uh of something and is we used like kind of weird bit shenanigans that sort of only works with certain representations of numbers in binary um but yeah i don't i don't know i don't know too much of the history behind it except what i just said basically um but i'm sure there's a wikipedia article or something you can cut in okay we've talked about one-dimensional numbers uh one-dimensional or one-dimensional vectors are usually not called one-dimensional vectors usually people just call them scalars people call them floating point values in programming or decimal values so i've sort of approached this in a non-conventional way so don't call things one-dimensional vectors because you're going to confuse people but the only reason i approached it this way is because all of these functions have direct analogies to the higher dimension vectors so let's go to two dimensions let's let's just enter another world or something like that um so usually when we talk about um when we talk about dimensions usually we have axes right uh currently we've only had one axis like it's the x-axis usually this is the number line we only have the one axis where we have all the numbers right and if we want to represent a position here all we need is one number right um and we also need to not lock alpha on the layers anyway okay so so basically if you want to represent a position on the number line all you need is one number because there's only one dimension uh so what we're now going to do is that we are going to add a dimension to this um so um first off when we work in multiple dimensions it's really useful to use colors so usually when when working in uh 2d or 3d space usually we use the colors um red green and blue so the x-axis is uh i'm using the eraser there we go x-axis is usually red um and there's a neat little little shorthand uh if you want to like remember which one is which so usually you have x and y and z so these directly correspond to you know rgb right so if you want to remember like what axis is what color uh then they follow rgb like rgb is usually the color color space we use and they directly correspond to the colors of the uh axes that we have in our or the each dimension axes basis vectors whatever like term you want to use um they correspond to those um so so like if you open up unity the colors of the gizmo is going to match this right if you see a red axis you know that that's going to be the x axis right uh so it'll look something like this um there we go um it's something that like i don't know it's weird how many people have not realized that it's it's like it's it's one of those things that's obvious once you learn it but before that it's like really not obvious for some reason um but yeah someone in twitch chat said left-handed the reason it's left-handed is because we are going to talk about unity and unity is left-handed and unreal is also left-handed although unreal is z up um but yeah anyway so that's why it's left-handed so i'm sorry for any like actual professional mathematicians because math is usually right-handed um which is kind of kind of not great um but whatever it's okay i'm also left-handed neat i'm glad you figured out your chirality right all right so we're gonna add another dimension so so far we've just had one dimension um and we also need to close this document because photoshop decided to lock the document which it sometimes does when i'm using the tablet um all right so we're just gonna we're just gonna take this axis and copy it and rotate it there we go oh if you're like very familiar with math this is probably painfully slow and i apologize but i do want to make sure that everybody feels very covered and know what we're talking about throughout this entire course so sorry if it's a little a little too basic but that's i hope that's okay all right so let's see uh the number line still works the same way we still have not like it's uh it's still a number line we just have two of them right so now we have one two three four five negative one negative two okay so now instead of a one-dimensional space which was the number line we now have a two-dimensional space uh so generally when you add a dimension to something uh the dimension is always perpendicular uh to the the other dimensions um so or the other axes uh so so whenever you're looking at something like this then actually it's not always perpendicular but if you want to have like an orthographic space which you usually work in um then all of the axes will be orthographic to each other uh so they always meet at a right angle um again there are exceptions if you're working with like projection matrices or whatever then but whatever that's irrelevant now sorry i'm going into unnecessarily weird details um all right so now we have a two-dimensional space so you know how we previously uh marked a point uh where you know we would say that some place is three right in terms of position right when we are working in two dimensions instead of every point on the number line being a single number a point in two-dimensional space is two numbers uh so there are a lot of like different ways you can like write vector notation um usually in mathematics their notation is really garbage and terrible so i'm just going to sort of do whatever because math hasn't hasn't worked this out yet um so usually i just do parentheses when i want to like mark a vector so so let's say we have we want to get a position in this 2d space let's say we mark this point right here just like we did before we could draw an arrow representing the position and if you want to draw a position or a vector uh vectors vectors kind of always originate from zero um because vectors don't have a root and a tip they only have a tip um so like where you put the vector is only for like visualization purposes or interpretation purposes um this is really important if you want to like know the distinction between like a vector and a point and a direction which we're going to get into so now we have a point so this is a point we can represent it as a vector too um and now if we want to like type the the coordinates of this one uh it would be two on the x-axis um and it would be one on the y-axis uh so so basically what this means is that each of these are called components and these components of this vector correspond to each of these two number lines so this is the x component and this is the y component of this vector right um in unity this type would be called a vector 2 because it's a two-dimensional vector [Music] um there we go vector two um so so this is how you would sort of write it in code you would do new vector two and then you have a two dimensional factor right okay so this um these are the coordinates for this point right all right so so now if we look back at everything we talked about before uh we can start doing a lot of operations with these that works exactly the same way uh in two dimensions so so let's start with addition let's say you want to add something right um so let's say we want to lock the photoshop document again and then reopen it um all right so let's say we want to add something to this um let's call it something um let's call it a all right maybe i don't know if we should do a lowercase let's do lowercase why not oh discord just said 170 new messages in the student chat hope not no we're fine okay thanks discord um all right so we have the vector a the vector a has these values right uh and then we want to add to this so again when we were adding numbers we we sort of had one um a one-dimensional vector plus another one-dimensional vector right if we want to add two-dimensional vectors um you would kind of get some number here some number here plus some number here some number here right because you have x and y coordinates right so you would have x y x y if you want to add two um two dimensional vectors together right um okay so let's uh let's pick another vector um let's say we want to do the vector um here so what are the coordinates of this vector there we go a question for class how would we write the coordinates of this vector it's always really awkward when you ask the class for like questions because it's usually obvious and then like some people don't really want to answer anyway and it's like it's weird um i'm not gonna do this again this was a mistake um okay negative one one exactly uh because it's it's if we look at the x-axis here um this position on the x-axis lands at negative one uh just like this position on the x-axis landed at two right so now we're uh the coordinates of this one is gonna be uh negative one on the x-axis uh and then on the the y-axis um it's gonna land at one just like the other one this one was also at one on the y-axis right uh cool so this is the vector that we're working with so let's say we want to add these together uh adding vectors together is you kind of do it exactly the same way as you would do with uh scalars or one-dimensional vectors but you do it for each component separately um and jesus christ photoshop does not want to cooperate today um so one way to think about this uh visually is that uh you know how we sort of imagine taking an arrow and putting it on the tip of the other arrow when adding um well we can do the same thing with this one we can kind of take this arrow and put it on the tip over here so that would give us uh this arrow right um i i don't know how to draw straight lines uh there we go so now we've moved this one over here right and what we end up with is that oh this is called uh b by the way there we go vector b uh so we have a here and then we're adding b here so where do we end up well this is the point where we end up right because we add this vector add this vector this is where we end up so what we what we get in the end is this vector right here let's do it in a different color because i got a little confusing um we end up with this vector so this vector the yellow one is a plus b right and you can sort of one thing that's nice about looking at it this way is that if you have some value like a plus b um this is actually equal to b plus a this might seem like it's kind of trivial that well of course that's the case right well why would it not be um but remember that that's not the case for like if you have a minus b uh that's not equal to b minus a right so so it's not always the case that if you flip the uh the values you get the same result right um so um so it's really important to to like keep in mind when things are i always forget if it's commutative or which one is which um there are terms for this um anyway so so if we look at this if we think about what we did here we have the vector a at the bottom and then we added b to a right because we added b here um so if we just look at the geometric interpretation of this we should get the same point if we do b plus a right um so if we try that then we have the vector b so now instead of moving b over here we can move a over here right so if we put a here we end up at the same point it's the it's the same result regardless of what order we do this in right commutative okay people are confirming that it's called commutative good okay uh anyway sometimes that's a useful term to know um like it's the same thing for for scalars a multiplied by b is the same thing as b multiplied by a but that doesn't always apply in some cases um multiplication is not commutative so for instance um if you're working with matrices or quaternions then multiplying those um is different depending on the order that you do it in i think matrices are non-commutative all right any questions so far is this clear how things work with something a little weird wonky then i guess just ask questions at any time i'm always up for answering questions um so that's sort of how addition works right uh if we talk about let's say subtraction um let's see i should probably undo a bunch of arrows all right so as steve is saying in the chat that i'm not supposed to read um subtraction is addition in the wrong direction so just like we took one arrow put it at the tip of the other arrow subtraction is again like we talked about before subtraction is the same thing as addition but we negate one of them right and negate in this case um well we were thinking about like one dimensional numbers before right we have a value of two and then we negate it we're going to get negative two right and if we have negative 2 and negate that we're going to get 2. so negation is basically we flip the arrow to the other side around the origin so in two dimensions it's exactly the same thing um we could do it like per component we could think about like what does it mean to negate the x-axis uh well if we negate the the um or we would negate the y-component of this one uh we're gonna get uh this vector and then we negate the x component which used to be two and then it's gonna flip to negative two right but an easier way of thinking it think about it is you just flip uh the arrow to point in the opposite direction right um so if we negate this vector then we're gonna get this vector right which is the same thing as just negating these individually so we could just copy those move them over and then um negative 1 and negative 2. right uh so now if we want to if we want to subtract something if we want to do b minus a then then we add these two vectors or we do this one minus this one right so if we do b minus a then we basically take this vector and we add this vector and then that's gonna land us over here right uh yeah and then if we did this in the the opposite direction if we flipped it because remember when we did addition uh flipping them had the gave the same result right um but in this case it won't right if we do so this point right here uh this is b minus a right if we do a minus b then we would we can imagine a and then we subtract this vector and that would get us over here right so over here we have uh a minus b uh so here we can see that uh subtracting uh is not commutative if we flip the order we're gonna get different results right uh okay so if we do b minus a in this case we're going to get this vector right here so this yellow line uh going all the way here so one thing that is that is really really important and crucial to know about doing subtraction between vectors is that this vector right here is the difference between uh between the two points right so so this yellow vector is the same vector as this yellow vector right we've just moved it to a different place um so if we want to know like what is the again the difference between the points uh a and the point b then that gives us this result which in this case um this would be a 2d vector still um but the y component happens to be zero right so on the x axis we have uh negative three and on the y-axis we have zero uh so that's what that's the that's the difference between um that's b minus a right uh and if you want you can still do do this component wise if you wanna do b minus a um you could do negative one minus two gives us three right uh you can do one minus one uh gives us zero uh so you could do it component wise as well okay uh so with subtraction they become each other's inverse values then um not really i guess it depends on what you mean by the inverse values um and they also don't become the distance from one to the other the distance is a separate concept because the distance is a scalar but if you subtract two vectors uh you're going to get another vector right uh so the vector is a difference between a and b right uh so so this is uh b minus a and this again is a vector right uh we're gonna talk about uh the distance soon all right so so all we're doing here is we're doing b minus a right better describe it as an offset technically it's called a displacement or usually displacement is used when it's like over time i think but yeah anyway so so it's important to remember that like this vector um the the way we want to interpret this if this is the difference between b and a is that um this is not a position right um now vectors don't inherently have a property that makes them a position or makes them a direction or makes them uh you know a quantum code vector but we need to interpret things right and that is like super important to know what each vector represents right so if we have this vector negative three and zero it's more helpful to visualize it as a as an arrow between a and b uh but the actual values is negative three and zero on the y axis right so so it's really important to like interpret this in the correct way um and also when you're writing code make sure that you're actually um clear about what this is right like if you want to if you want to keep this vector for something um call it b to a uh or call it the the difference between something or the delta or whatever um but if you just have a vector then you're not entirely sure like should we interpret this as a position should we interpret it as a position relative to a which we are in this case um and so forth right yeah so this is like a very crucial concept uh it's very important uh once we're getting into uh using different spaces um so when you're doing a lot of things in game dev um the difference between local space world space and all of that stuff is really important um so so you need to keep track of like you know if you have a vector like this on its own this one doesn't say if this is in local space if it's in world space um if it's relative to some other point or like how this is used right so always be like very careful like how you uh name your variables when it comes to this um so like the um so this is something that i hate math for math is so bad at this math notation generally is really garbage uh because they like there's so much stuff that's very implicit and like notationally ambiguous uh but in code we can actually name variables we can make functions that are very clear what they're doing um so in code you can be very explicit and i think that's really good because math is unreadable sometimes um yeah in math you would have things like well if you have v um then you need to restart document again in photoshop thanks photoshop um so sometimes you will have papers this is an actual case by the way where like v means one thing uh this is bold but then you have a non-bold v that means a different thing and then you have a cursive v that means something else like this is bad like don't do this i i hate it that like math papers actually do this kind of garbage um so like you will run into this if especially if you're like reading shader papers or whatever that kind of stuff is really frustrating um yeah so so math notation usually rely on on these types of things and it's really annoying um but in code be explicit be very like clear with what everything is have variable names that make sense and so forth right um yeah and then you would have things like oh if it's bold um this is a vector but we don't draw the vector arrow because we're just going to presume that people are no know that this is a vector now and if it's cursive it's a scalar value this is a component of the vector or whatever it's it's frustrating anyway stuff like this is why i don't call myself a mathematician because i kind of hate math notation and i also don't know much math outside of game dev oh speaking of which if there's ever something where you are like when is this going to be useful or what is the point of this ask just ask everything i'm going to talk about is useful for game development none of this is going to be like esoteric unuseful they're just bad things right so so don't be afraid to ask like when am i going to use this uh or can i order fries with trigonometry or that type of stuff right just feel free to ask questions in my experience vector math is probably 50 of gameplay programming yes pretty much um like it's like again people ask me many times like do i need to know a lot of math to do game development i'm just like nah not really you need like vector math trigonometry matrices help uh and then you need to know how to manipulate values like remapping ranges uh doing oscillation that kind of stuff um but usually it's it's you don't need that much anyway i'm gonna take a picture of this and send it to the students in case people want to keep this beautiful piece of art what does orthogonal mean uh that they are 90 degrees off of each other have you ever worked in non-euclidean geometry or altered space the game so you're made um not really no i guess there are like there are matters of like interpretation right i mean if you want to call polar coordinates non euclidean then yes um but usually outside of that no i haven't done a lot of that mostly because i like i don't know it's like seems like it might be fun like experimentally but i don't think i'm gonna actually like make a game out of it but it seems like it might be fun to like try those things most interesting math problem i had in game dev is trying to forecast the path of certain projectis that is a thing we're going to talk about like uh ballistic trajectories free us game to have math cards this is a vector it's like two numbers stuck together twitch at lauren's transform yeah yeah last time i did a math course on twitch there was someone who's like an actual mathematician and every time i brought up a concept they would like bring up the incredibly esoteric like really complicated generalized concept in math with like really complicated words and i'm like this is not the time like i don't even know what those things are chill i'm not going to be able to explain i don't know lie groups and algebraic rings to students geez it's amazing that people can understand esoteric concepts in math but not regular social norms kind of yeah well but they they are separate skills so you know that's how it goes okay so we talked about these things right we talked about the sign of a value we talked about the how the length or the magnitude of a value talked about the distance between two values all of these concepts also generalized to 2d and any other numbers of dimensions you want to use so it works for 2d 3d uh 45d however many dimensions your game is in right okay so so let's let's let's see what these would mean in 2d right so let's bring back uh some vector let's say uh say we do this one you know drawing straight lines is one of the hardest things in art there we go nailed it all right so now we have a vector cool so the components of this vector again we can look at the the axis here for the x and the y components uh and then we can see that the x component uh is three uh so this would be uh three um and the y component is two cool so now we have our vector i also didn't really want to draw all of this uh right so now we have a two dimensional vector again two values one for the x-axis and one for the y-axis uh right so we talked about the um the the sign of a of the value right so if we have if we have a negative 5 for instance the sign of this is going to be a negative 1. the sign of a value like 2 is going to be 1. so so basically what this kind of represents is because again we are on the number line right and we have zero somewhere uh so what the sign kind of is is that it can only be either one or negative one um so trying to think of if i should call these these unit vectors one dimensional unit vectors which is a little bit of a cursed term um but if you talk about unit vectors that means that you have a vector that has a length of one and both of these have a length of one right um so as so what this means is that the uh when we do the sine of something we get the direction right uh we get the direction for if it's um if it's going on the negative direction on the axis uh or we get the direction pointing in the positive direction uh of this axis right um so what what the sign kind of what it really does is that it gives us a direction um usually direction vector okay so what does that mean in this case well uh we talked about i mentioned unit vectors um so a unit vector is a vector that has a length of one which both of these have right even though they're one-dimensional vectors they do both have a length of one because this uh distance right here this one um this distance right here is also one right uh so one way of representing something that has the same distance um is usually using circles but in one dimension we can't really do circles so let's go to two dimensions uh so now that we have uh two dimensions um basically oh god i need to draw a circle um drawing circles is hard you know what there are tools for this let's let's do a let's cheat [Music] there we go what a beautiful circle okay is that visible on stream or is it too faint i think it's it's good right okay so so all the um all the vectors that are pointing towards the boundary of the circle so if you have a vector pointing here then this has a length of one right uh this vector also has a length of one if you just measure the length along this arrow it's one um so what this means is that all the vectors that end up on the boundary of the circles are all unit vectors right so when we talk about unit vectors um quite often they're referred to as directions so directions are always unit vectors so so if we want to know the uh direction of this one all we we're doing is that we're kind of making it the length of one uh so so this vector right here is the direction of uh this vector right and what what this is called uh the process of making a vector the length of one is called normalization uh so nor ma li eyes were inconsistent there we go so the process of making a vector normalized or the length of one is normalization uh so so this vector right here is the normalized version of this vector and this concept is also very important uh like this is super super useful in so many different ways yeah this is this is like ubiquitous whenever you're dealing with um you know dealing with vector math in terms of positions of objects uh the relation to each other uh the direction to some other object and so forth right um all right so let's erase this confusing arrow uh so so this is a normalized vector right so so that is the direction right in one dimension uh the vector could only have two states right uh it could either be negative one or it could be one right uh these are the only two states that are valid for a normalized one-dimensional vector right but for a two-dimensional vector we have an infinite number of points right uh this circle can can fit so many points the slaps roof of circle or something so like any of these vectors would be a normalized vector and all of them have a length of one right yeah so so normalized vectors means that it has a length of one but that's kind of it uh so when you talk about normalizing vectors so let's say this whole vector is called v um if you want to normalize it in math notation you will usually see it with two vertical bars like this um so this usually means the um well actually this could be the length of it sorry if you have v and then a little hat on top of it this is a normalized vector if you have bars on the side that's usually the length of it sometimes you'll see two bars on the side because nothing is consistent and everything is garbage um but yeah so usually uh this would be the uh direction and this would be the length or magnitude same word all right so this is just the math notation garbage i'm pretty sure this is correct i don't i feel like i have to relearn it every time i read a math paper so so we might wonder like how do you get a normalized vector right like how do you how do you go from v to the to the normalized version of v or the direction of v um kind of use those like interchangeably right so this little vector down here would be at the normalized version of v with a little hat on top um but basically the direction of v okay so we're going to talk about how to normalize a vector um so i didn't quite get the normalization so so basically if you have any vector uh pick up any point on over here then you're that's going to be a line from zero right um if you want to normalize this vector what that means is that you make the length of one so what you get out of it is a vector pointing in the same direction but it's got a length of one right so it used to be this whole vector but if you normalize it it's going to shrink down into a length of one or if you pick a vector that is uh that is a length that's smaller than one uh this vector is going to normalize into being longer all the way out here so so basically you can sort of consider it to be away from going from a point to an arrow that represents the direction to that point um yeah when do you usually need to normalize a vector so the concept of a direction is really important so so say you have say you have an object that is over here let's say maybe this is an enemy like at this location and then you want to know that maybe the player is at zero and you want to know what is the direction that you need to move in order to move towards the enemy so if you are dealing with like physics or something and you want to add the vector to move toward the enemy then usually you want to move at it like a fixed rate in terms of speed right so what you usually do then is you normalize the vector between the player and the enemy and then you have a normalized vector which again that's a direction usually in terms of terminology and then once you have a normalized vector if you multiply that by your speed or some value that represents how much you want to move in that direction you can guarantee that that's going to be in you know meters per second or that that distance is going to be in meters because the length is one right uh so that means that um you get consistent movement in in space and what's the difference between unity's normalized method and normalized um yeah the dot normalize will modify the uh original one uh whereas the other one returns a a vector okay did that make sense uh so basically pick any point normalizing it means you get the direction to that point um and a direction in this case i'm using the word direction to be interchangeable with a vector with a length of one right but it's still pointing towards that point right like i mentioned before one of one of the powers the powerful things about having normalized vectors uh is that let's see if i can undo a bunch of things um so so if you have a direction like this you can multiply a vector with a scalar as in a single value so if you have a vector again usually when people say vector they mean multiple components rather than one but if you have a two-dimensional vector you can multiply that by two for instance uh or like some some number uh but if you have the normalized vector uh so let's say we have our our vector that's normalized it's the same thing as this one um and then we we multiply that by a value of two let's pick two what that means is that the new vector we are getting out of this uh that is going to be the direction along v right here uh but the length of it is going to be 2. so this is kind of what is abs like extremely useful about normalized vectors um because if you want to move like a uni like two units uh towards something uh then having a normalized vector means that you can get a position that's exactly like some number away from some point um so if you normalize a vector multiply it by a distance uh then you're going to make the length of that vector the value you're multiplying it with um and it might seem like that's not that's kind of trivial but that's not the case with this vector right if you multiply this by two then at the length of it it's going to be like all the way over here right so the length of this is definitely not two right it's way longer um so so this is why normalized vectors are extremely useful because of the fact that the length is one if we multiply it by some scalar the length is going to be that scalar right um yeah so that's something that's useful when it comes to this so like i mentioned before it's useful in physics for instance so if you have the normalized vector then if you want to move something along this vector then well what you can do then is that you could have a time multiplied by your speed for instance um so time multiplied by speed so now we're just multiplying all of these together so what that means that if time is a value that is ticking in seconds when you start the game time goes from zero to whatever the current second is um then you're gonna have an object that moves along this vector uh at the speed you give here uh we're gonna go into more physics stuff later uh but so this is just one of the really really powerful things about having normalized vectors right can you show an example in unity uh yeah sure do people want to see examples of this um show examples in unreal nope i think showing it in practice is really really good because you're gonna work in unity so why not all right let's nuke some objects there we go let's go 2d because we're not into 3d yet [Music] um that's correct uh i think will the game crash if it runs for more seconds on the float 32-bit limit you're gonna have to run the game a very long time like it's not gonna crash it's just that your equations are gonna get a hitch and a weird thing that's gonna happen once it goes over the limit um i don't think it's gonna crash you're just gonna have very weird behavior as soon as you hit that point um okay let's see all right um i probably should have done code examples earlier actually because again i like keeping things very pragmatic and practical um okay uh let's just let's just do let's just do some of the things we've done here uh just to show how it works right um so i usually really like using android as most because you can see it in the editor you can very easily like draw lines and whatnot um so android gizmos is a special function in unity uh that is called in the editor and you could optionally enable it in game in the game view as well uh where you can like draw lines or whatever uh so you can do stuff like um gizmos dot draw a line and then you can supply some positions right um so we do um something like that then we go back to unity um we now draw a beautiful tiny little white line right okay oh wow i wonder if there's a vector graphics package you can get um okay so all right so draw a line just a very simple one um but we probably want to be able to like supply some other points um i do have to wrap android gizmos and if unity editor i'm pretty sure that's going to get stripped for builds but i could be wrong sometimes you use methods in there that are only in the editor namespaces and whatnot but like if you use handles for instance but gizmos it kind of ignores that i'm pretty sure yeah i'm pretty sure you don't need to strip it it's not gonna like maybe the code in here will be part of the assembly um i'm not sure but at the very least it's not gonna get called um regardless um um okay uh all right so we probably want to be able to pass a vector into this or a point or whatever right uh so instead of instead of just hard coding it here we can add a property or in my case i'm just gonna i'm just gonna pass in a transform because it's easier to move it around let's call it um points transform let's make a new game object uh actually because since we're drawing all of this in world space we can just use this transform um all right let's pass point into there all right uh so now we're drawing a line between zero and this transform right uh so now we're basically just taking the the position of this transform and we're drawing a line to it right um so again we can think of this as the the two number lines we had where this is the origin and then we have the x-axis and the y-axis right here um oh you couldn't hear the discard [Music] you should have been it might have been a lower volume so uh all right so now we have the the points right um so now let's say we want to get the direction to this point because we have been talking a lot about directions right uh so so let's get that so direction to points um so in this case because the it's originating from zero and we just want the direction to this arrow um then all we need to do is normalize that point right and unity has a bunch of built-in functions uh you can do point.normalized and that's going to return a normalized version of this point right um so um yeah so now we have the direction to the point and again i'm using the word direction to mean a vector of length one which is sometimes also called a unit vector a normalized vector like it has a bunch of terms but usually when i use the word direction i mean a normalized vector uh just to like clarify um okay so if we draw that instead and then go back to unity uh now this line always has a length of one right doesn't matter how far away the the transform is but the but the length of it is also one even if it's shorter or longer it's going to normalize it to always be unit length so it now it's sort of tracing the outline of what's called the unit circle so the unit circle is just the all the points of the vectors that have a length of one so it's the this circle right here right uh so now we have a direction right what if you set the transform to zero uh so probably uh now it gets very sad um because the i'm pretty sure normalized it looks like it just returning a zero vector uh so now the the quantum code direction that this returns is going to be zero uh so it's trying to draw a point from third line from zero to zero um so speaking about that this is a very good case where um when you have something like this um this is actually division by zero like technically we're going to get into how to calculate the length or how to how to calculate the term like normalized vectors later uh but this is actually division by zero and something that is kind of beautiful in mathematics is that any time you are dividing by zero i think this is like so mind-blowing but like every time you're dividing by zero in some equation that is kind of the math talking back to you saying that like hey this is a weird case you need to handle this case something is happening here and you need to think about what do you actually want to happen in this case because it's kind of telling you that there is a degenerate case or a case that you cannot calculate because division by zero is undefined um so so whenever you have an equation where there is a divide line think about if the denominator can ever be zero if it can be zero figure out what that means right like when would this this be zero um anyway okay so so that now we're just drawing the normalized version of of this vector do you have a course that you to meet no but i have my youtube channel which is free so you don't have to pay for it uh right um cool okay so let's talk about let's talk about the the length because we've sort of mentioned the length a lot when it comes to normalizing things we probably should have talked about length before normalization but you know what that's okay all right so so let's talk about length how do you how do you get the length of the vector right um i might be skipping ahead but when you say the vector two that normally divides by zero where does it do that um you are sort of skipping ahead i'll get to that uh but it technically uh unity's vector2.normalize handles that case and just returns a zero vector instead of you know crapping out and not knowing what to do um you could sort of make a normalized function that just throws an exception which might be useful but unity's normalized just kind of just returns a zero vector oh a zero vector is just a vector with components at zero so so all of the components are zero sometimes called a zero vector um yeah okay so so we we talked about normalization but not how to calculate it like how how do you normalize a vector like how do you go from this to whatever the heck these coordinates are right um all right so if there is i don't know if we should use this vector let's do another vector um let's do this one what a vector it's a little curved but pretend it's not all right so so let's see uh what is this what is this vector um so we have x coordinate we have negative two uh y coordinate we have three right so this is our vector so now we want to know the length of this like how do we do that uh so now this is gonna get into uh the the hacking pythagorean theorem right um because if you think about it if you wanna get the length of this uh you can sort of imagine a triangle here and this is a right angle triangle and this is something that is taught in math class so if you remember how to get the length of the hypotenuse then you know how to get the the length of a vector right um so so basically what we need to do then is um we take the x component square that and then we add the y component and square that and then we take the square root of all of this there we go uh that's the pythagorean theorem that's how you get the the hypotenuse of a right angle triangle right but this is this is how you get the length of the vector right is that clear by the way we haven't talked about like exponents or what it means for something to be squared or talked about square roots but i'm guessing we don't need to talk about that or um but if not let me know we can talk about it okay so this is basically how you get the length of a vector all right that's it i think so now we've been talking about uh we talked about for one-dimensional numbers we have abs instead of something else uh but the generalized concepts uh you usually write it like this for any other vectors you can add more components inside of this query um you can add you know z cube or z squared and then you have the length of a three-dimensional vector so so it's the same thing like regardless of how many dimensions we have right um and this actually holds even for one dimensional vector um so you know we talked about one-dimensional vectors where you can use the abs function which is basically just if it's negative make it positive uh but you can do this for one dimensional vectors too uh so if you if you have the square root and this let's say we have the uh the the one component of negative five for instance um so um all right so let's just do the equation for that right well we do the square root of uh five squared so that's five five times five right um which is uh 25 i think i don't know pretty sure so that's 25 right um oh actually sorry not 5 times 5 a negative 5 times negative 5. if you multiply two negative numbers um then it's going to be positive in the end because the signs sort of cancel out right so we get positive 25 and then when we do the square root of that by convention square roots technically have two results but we're not going to get into that so technically this then returns 5 because the square root of a 25 is 5. the equal sign is incorrect yes because this is the length of that sorry length of negative 5 then just ends up being 5 because that's what the square root then returns right um so yeah um so it's just kind of like that this same equation works for any vectors now in practice when you want to get the the length of um you know want to get the length of oh wait are you getting notification sounds i think i'm just getting it locally so when you want to get the length of a vector for one-dimensional vectors or just scalar values usually you use abs you don't actually do the full equation but for anything like any dimensions about that you generally use the pythagorean theorem okay so so this works for for two dimensional vectors too um oh actually didn't sorry legs you know i actually have a cold right now so this is like canonically kind of more accurate but um sorry uh sometimes my backwards writing doesn't work out okay cool so now we have that right and now we can actually do that math um so so if we want to get the length of this one uh we can just just do that right so that's negative 2 times negative 2 plus 3 times 3 and then we do the square root of that um this is not a good looking square where's my good color there we go so then we can just run these numbers right uh so all right we do square root of negative 2 times negative two pretty sure that's four uh so it's gonna be four plus uh three times three that would be nine um so four plus nine um which is oh god uh that's 13 i think look again i'm really bad at doing math in my head uh pretty sure that's 13. all right so it's no longer really tied to the x or y axis i guess we can make it blue or something um so that becomes 13. um and then we want to calculate that so that's when you pull out the calculator and you type 13 and then you do the square root of that and it turns out it's 3.6 something right um so this is approximately equal to uh 3.6 oh sorry maybe we will do it in blue why not cool so that's how you get the length so now what this value represents um is the uh the length of this right here right the the full length of this vector turns out to be 3.6 something something right um so now we know the length of it right uh cool uh that's how we do it uh if you want to get the um like if you want to do this in practice you almost never write this formula yourself and so if you go to unity if you want to get the length of the point or the vector um again length is a scalar it's just a single value so the length of this one would be point dot magnitude because again magnitude and length same thing um yeah so this is how you get the length sometimes it can be useful to calculate the length manually because sometimes you want both the length length and the normalized vector and in that case calculating things manually can be really useful cool everything clear so far uh how to get the length of a vector so now we've just talked about how to get the length but not how to get the direction or sign and not how to get the distance okay let's do the distance so the distance uh now that we know how to get the length of something um now if we get the the difference vector or the the displacement between two vectors uh let's say we want to get uh this vector between uh these two right here um then this vector this would be uh we don't have names for these i'll call it a and v there we go um so so in this case this vector right here uh would be a minus v right a minus v so now we have the difference between uh these two points right so if we want to get the length or the the the distance between these two vectors all we need to do is get the length of this vector because that like if we know how to get the length of this then we now know the distance between these two points um and in this case it also doesn't actually matter if we do v minus a or a minus v because if we just if we're just interested in the distance uh the distance is always positive so it doesn't matter at which order we we do this in so if you want to get the distance between uh two points uh previously we did the absolute value because that's how we did the distance in uh one dimension actually we didn't we never call it length um anyway so this is how you do the length uh so we can basically just replace this by by calling it length instead right um so now we have the now we can get the length of this one and the way to get the length is is this one right here right there we go cool um all right so so basically this is how we get the length uh if you want to get the uh the the distance between two points then you subtract the points one subtracted by the other and then you get the length of that vector and then you get a distance um so there is a slight difference in term in terms of interpretation between distance and length as a distance usually is between two points um and length is usually the the length of one point or the length of the vector that represents that one point um so so when we want to get the distance between this point and this point um we first subtract them from each other to get the difference vector and then we check the length of that difference vector all right so so now uh we have a way of getting the uh length we have a way of getting the distance have we clarified that the resulting blue vector is still centered on the origin uh we talked about that yeah like sen like vectors don't have a root and a tip it's only the vector data itself right um so we talked about that when we talked about subtraction that uh if you want to draw it at its origin it would be this vector right length magnitude is of the vector distance is between two vectors generally yeah so when you talk about distance that's a special case where you want to know um how long is the vector that is the displacement between these two points basically uh so when you talk about distance again distance has an input of two points usually um and then you get the the difference of those two points and get the length of that um if you want to know the length of length of something it's just one input right so that's just the length of one vector um yeah of course there are built-in helpers for this you don't have to do it manually um so if you want to know the distance between two points you can use vector2 dot distance from a to b or db null i guess so this would give you the distance uh but you can you can you can do it the the other way around too you could do um a minus b dot uh magnitude for instance uh this would also give you the distance it's the same thing right all right or you could do the the manual way if you want to of like doing like a minus b like a dot x minus b dot x um and then you then you square that uh and then you add the same thing but for the y component um and then you do the square root of all of that um so so this is the this is the same thing like this is also a way you can get the um the magnitude or the the distance between two points right yeah just different levels of abstraction i guess um how to apply it in practice um well we could make two points all right let's make a green b red now we have two points right what a good pair of points um okay let's say you want to know the distance between these two right uh first we need the actual points because we only have transforms here so it would be uh oh i need to probably assign them in the inspector dude there we go uh so now we have um let's see we just call them a so that's gonna be a transform.position and then we'll do the same thing for uh b so now we have the two points a and b and again we can we can draw lines to make sure that this is uh working right i'm not using my library um gizmus draw line from a to b there we go cool so now there should be a line between these two points uh so we can move them around and we now have yeah this what a good line right but we want to know the distance of that so if we don't want to know the distance i guess we can print it in the inspector or something um so if we want to know the distance um then again we can use everything we typed here right so we can do vector2.distance uh and now we should set this value right here uh it might be hard to see it in the inspector uh but there's a little value there there's updating now it's 0.45 now it's 1.9 and so forth so now we actually have the distance right there um yeah so that's how you use it in practice um and again the the distance value is going to be exactly the same thing if we do a minus b dot magnitude so we can compile this it's going to work um and yeah still have the same distance any any other questions before lunch any thoughts how to make a game like angry birds uh i can't answer that that's a very long question and a whole production pipeline start start doing things that's how you make games generally speaking no one's going to get mad at you for doing a minus b dot magnitude instead of vector 2. distance uh i don't think so i mean the the first one might be slightly more esoteric because you sort of need to know the underlying math for that to be readable but i think the i mean most people should know about that it's a very important concept um like vector2.distance the nice thing about using that one is that you're being very explicit that you literally want the distance between two points uh if you use the a minus b dot magnitude it's a little unclear like you get the length of the difference between these two but like are we gonna is that interpreted as a distance uh maybe these aren't points maybe this is something else so you know sometimes it's useful to like use the one that literally says distance because it's very clear that you kind of want a distance right and conversely if you don't want if you want to do the same math as this but it's not a distance value then using vector2.distance is likely going to be more confusing than useful right so it kind of depends on the context i personally really like doing um like being explicit and clear with my code hugo you're spoiling it i haven't gotten to that yet but um yeah actually let's just let's use we can just talk about that um but that's the last thing before before lunch okay so we talked about the uh we have this line right here right uh the the one that's pointing in the direction of the this transform right uh so what this one is that is the normalized version of that point like this is this is the only thing we've done there um so so the way to calculate a normalized point um so instead of just doing dot normalized we can do the point and i would divide it by the length of that point right so point dot magnitude so this is how you normalize a vector so by dividing it by the length the um the length becomes one right um yeah so so that's how you normalize a vector i previously talked about the fact that there is a degenerate case uh where you're dividing by zero and if you look at this equation you can probably tell what the uh what is what the degenerate case is right because points.magnitude can be zero uh so what that means is that uh point is at zero uh so if you have a point that's at zero and your goal is that you want to get the direction that this point has from the origin uh but if the magnitude is zero then you're dividing by zero so now again math is telling you that something is messed up and you need to think about what you actually want to do here so what this means is that um you're trying to figure out a direction of a vector that doesn't have a direction it's zero it doesn't point anywhere right um so so usually in these cases you need to figure out okay what do i do to handle this special case uh if it's at zero do we want to fall back to just using zero do you want to check if the length is zero and then not draw it or not execute some piece of code um usually it's it's really like every time you see a division operator um it's really useful to think about if the denominator can ever be zero if it can then that means that you have an interesting case to solve um yeah all right i think that's it uh for lunch yeah we're gonna talk about the square of magnitude stuff that you're talking about um within the uh like after the break and yes doing square magnitude is faster that you're yes um i think there are some intricacies of like the property call itself has some weird overhead but if you manually do the square magnitude it's like way faster than actually doing uh magnitude um so i've heard but that's not for mathematical reasons that's for silly overhead reasons actually we should do a we should do a lunch break okay i i'm just there's no audio in the lunch break room so so yeah i'm just gonna place an image here see you soon see you after lunch look at that little boy sleeping in the goblet he's a peaceful little cat now uh where were we oh we just covered uh how to actually normalize vectors how to do that right not not just in terms of what it is how to do it using unity's helper but how to actually do it yourself right okay um what's the state of things so we have our code and we were just showing directions right okay all right so so here's here's a short little exercise that we can think about together um so so for instance now we have these two points right previously when we looked at direction here um this is the direction of the point itself in world space right like this one is um it's just a line drawn from zero in the world and then out to one distance away from the center in the direction of this point that we move around right um so uh but yeah so so let's say we wanted to draw the direction between these two points uh let's say you want to do direction from uh from a to b um so so let's do that let's just figure out how to do that right so uh we're going to ignore the the center points let's just hide those uh let's forget all about this um let's let's forget about this too forget about everything you have learned nothing so far um okay so now we are drawing a line between a and b right uh so i should just keep this line uh so now the goal is that we want the direction from a the green point to be the red point so so we want that arrow right so so how do we get a direction between two points well the first thing we need to do is to get the difference between these two points right so if we take one point and subtract it by the other we get the vector going from one to the other right um so that's something we talked about earlier um like the we have the two points here both of these um sorry my desk layout is not in tablet mode so we have this point and we have this point and then we want to get the direction between these two points but first in order to get the direction we need this blue vector right here right so all we need to do is subtract one from the other the order in which we do the subtraction uh will change the direction that the arrow is pointing right so usually you can go either way depending on how you do the subtraction um so so generally it's kind of annoying that if you have um if you have two points like uh you have a and you have b and you want to get the direction from a to b and the way you do that is b minus a um and it's kind of annoying that this is the order that you do it because it feels like you kind of want to have the a first um but i kind of just have this mental model of you always have to like flip them if you want to get from a to b um so yeah so it's always like the the two points uh minus the uh front points uh so then from this to this in terms of like direction uh okay so that's all we need to do to get that vector right uh so let's let's do that vector so that's going to be called a to b um and that's going to be b minus a so now we have the vector going from a to b right um all right so now that we have that vector what happens if we draw that one uh well we can try it out right draw it from zero to that vector so now we're just going to visualize the raw vector right so if we do that it's now going to draw at the origin right because the vector itself doesn't have a root of the arrow right so it doesn't really care about the location where we did this calculation but it is getting the correct vector like this is the vector going from um from a to b right so so what we need to do now is that we just need the direction right uh so we don't want the actual full vector we want this to have a length of one so if we want a direction vector we would need to normalize this right so so let's make the direction so a to b direction uh so then we can do a to b dot normalized and then we can draw the direction so now this one is always going to have a length of one so it doesn't matter how far apart they are but we do have the direction right we can move either of these points and the direction is still going to be correct okay so far so good but now we we probably want to draw this one at the correct location because it's kind of confusing that it's down here like the direction is correct but the visualization of this vector is kind of offset we kind of want to see it here right like we want to see it go from the green point to the red point okay so so we we need to move this vector we also need to hide this line because that line is going to be covering up a bunch of stuff that we want to see so let's first hide that line and then we want to move this vector so draw a line is you supply two points in it right and the first point is a because we want to originate this line from a uh so we can just pass the point a into that one and then in order to make the this uh be relative to a all we have to do is add a because then we're going to move it up by this vector going from 0 to a right because that's what a is the coordinate of a is a vector going from 0 to a so so then we can do a plus that direction uh and then if we recompile um it's not going to point toward the red point right and so now you can start to see like you can imagine these being units in a game right and you need to point towards some target or whatever and you need that direction somehow then this is uh this is a way you can do that right um yeah although the um this part is only for drawing purposes uh you already have the direction here uh the only reason we had to do the a and a plus the direction is to draw it at that location right but the direction itself is already done because directions um in terms of interpretation direct directions should not be like um they don't really have a position it's just a direction right um yeah okay uh did that make sense so far if it didn't have a plus it would point to the location based on zero yeah if this is just zero um or the uh sorry a zero vector oh it didn't it was sad about that um if we just set this to uh vector2.0 uh we remove that one uh then it's going to be positioned at the origin but it's going to have the correct direction right uh so so the direction is correct here um yeah so so kind of usually the you only need this direction you usually don't have to move it there we're only doing that for visualizing uh what's going on um okay why are you doing a plus uh because we want to draw at that location right um so so if we don't do a plus it's gonna draw the vector going from zero in the origin of the world to the direction that the vector is pointing right but in our case we want to draw it at the location where it's relevant right so again if we just do this vector3.0 recompile uh so now um it's going to be at the origin of the world just at zero so now we're kind of showing the raw data of this vector uh but in terms of like visualizing it it's kind of like annoying that it's at the origin because if these things are very far away uh then we don't even see the origin even though the direction here is correct this is the direction uh going from a to b right um so so the direction is correct but just visualizing it we need to supply two points to draw it right with when we're using draw a line um so yeah oh yeah there is also a draw ray one if you want to use it uh although i tend to not use that one but yeah so so the uh the a here is just to offset everything so that it's positioned here instead right uh so yeah so so all we need to do to make this one positioned at a uh is that we add a so zero plus a and a plus a to b direction um and this is unnecessary so we can just remove that and then we have a um to a plus a to b direction uh and then we get the uh the two points right so what a plus a to b direction is is the point here it's the end of this line right here so that's what the second point is and this is where the first point is so what this gives us is this point which again goes from the origin all the way out to this tip because uh depending on if we want to interpret it as a vector or as a point we can visualize it in different ways right but that's what that returns it gives us that point right there uh what happens if you draw a line a atp direction uh i mean it's just going to be confusing there's no like interpretation that that makes it that makes it useful sort of um yeah so there's not really much point of doing that although it is very common to sometimes forget to do things correctly so sometimes you will see issues like this uh where you're kind of just confused about what's happening right because this doesn't this doesn't look like a direction at all this is this does not have a length of one right um yeah um because then what we're doing is essentially this point is originating at the correct location but this one would still be if we didn't have a plus it would still draw the end point at the origin so we need to move both the origin of the line and the end point of the line um by a right so that it's relative to a yeah you're offsetting both endpoints or the starting point and the endpoint with a right so now we have a way of getting the direction between these two right um let's see there have been a few more things we talked about uh so one of the more useful things about directions is that we can very explicitly position things a fixed distance along that line that is very easy to write um so so let's say you have some some float value um let's make it a range actually um from 0 to 4. um let's call it offset it's fine um all right so now we have a value called offset don't need a b distance anymore um okay so let's say we want to draw a point between these two points we're going to want to draw it along the line that goes here right and we want to set the distance away from the player so now we're going to call that offset and we can do gizmos dot draw sphere so just drawing little points so we want to draw that one at the location so just to make sure that this works um we can try let's draw at the midpoint between these two so if you want to get the the midpoint or the average of two points um as you would do with any other numbers you do the same thing with vectors you add them together and then you divide by the number of entries you have so a plus b divided by two gives you the average point um of um of these two coordinates right uh all right uh so then we also need a radius i can just make something pretty small um actually that's being a bit more explicit so midpoints equals that there we go so now we want to draw a sphere at the midpoint between these two points all right so we've got this gray little sphere and it is right between these two points okay so now we got this thing that we can draw um but we don't want to draw it at the midpoint uh so we want to draw it a fixed distance from the starting point toward the end point so if we want to do that like i mentioned before the fact that this direction is normalized means that if we multiply it by some value we know that the length of the resulting vector is going to be that value that's not true if it's not normalized so this is kind of the really really powerful things about direction vectors or unit vectors normalized vectors same thing it's the same word uh wait different words same concept um so we have the direction here and then we want to get a point along that right so we can do a to b direction multiply that by our offset so offset is the the parameter that we can tweak for the distance along this line um so let's call it um offset vector uh so now there we go sorry so now we have the vector that is offset that it's going to move from this one toward the red one now same thing as before if we just draw this one it's again this offset vector is relative to the starting point so if we just draw this vector it's going to be drawn at 0 in world space right so we probably want to move that one so we want to make sure that it's relative to a just for drawing purposes right so we add a to offset vector and then we can go back to unity uh and now we can see that we have this the sphere and it's exactly one uh one unit away from the starting point and we can change this offset so we set it to zero it's zero units and we set it to two it's exactly two units away um and unlike the midpoint that we got before this one doesn't like squash and stretch it's always the exact same distance away from the player right so what is the point of this what's the use of this well if you imagine this offset being a time value then we just created a projectile right this one this thing is just moving um at a fixed velocity right um so now all of a sudden we just have a very simple formula where if we increase offset over time um we have the flight path of you know an object in motion um and all we've done is basically take the direction multiply it by the distance and that's it if you increase the distance over time then you have a moving object that's moving along that direction vector right okay did that make sense anything that any questions or things that were not very clear such a good way of explaining it i wish i had this lecture three years ago you should be a teacher or something i should be a teacher you know if only i was a teacher um yeah okay so so yeah so so now that this is like very much a like this is very very central and very core to working with vectors in in games and the concept of a normalized vector or a direction is super crucial to have a have a good understanding of um i'm probably going to make that a very central part of all of the um like all of the assignments that you're getting um because this is extremely core um okay let's see where are we in the schedule um oh right um okay maybe we should make more than one script just to make sure that we're not like cluttering this whole thing um i forgot my hotkey for disabling objects there we go all right let's make let's make another script just to like show more examples of like why is this useful uh so let's say we want to make a um a radial trigger like we have a trigger and we want something to happen by the distance to that thing right uh so let's create it there um and then some objects we can test the code maybe this is a player maybe it's an enemy who knows all right so we have an object now and we have our trigger that is just nothing right now um so let's go back to android gizmos again uh because it's useful for just testing things very quickly uh there we go okay so let's see what do we want to do in this trigger uh for instance we might want to set a radius on this one right like what is the threshold at which we want to want this trigger to activate right so let's make another range um from zero uh to four uh and then this is gonna be the radius and we default to one all right and then we probably want to draw this so that we can actually like see what the radius is um this is by the way something i talk about a lot when i have my tool dev courses uh on draw gizmos to just visualize things like radius and whatnot super important and really good for level designers so that they can actually see what the range is of everything right otherwise everything is kind of like difficult to parse um all right so let's draw this uh we can do that with uh gizmos dot uh draw uh let's see there are many functions here we can do a draw wired sphere for instance so wire is um wireframe uh okay i don't know i think the i think handles has a wire disk uh maybe we should use that one instead uh although okay one thing to note though whenever you're using handles uh handles is in the editor namespace so if you want to ship a game and you're using handles and android gizmos you have to make sure that you um compile that out right uh so in this case if you use as soon as you're using handles always make sure that you you um compile out the um um the code right otherwise you're gonna get builders and that makes people sad okay so we're doing handles uh let's draw a disk uh we want to draw this at at the location of this trigger right um that's the wrong using that is true uh there we go unity editor is the one that should be wrapped uh all right so we want to draw this wire disk uh and let's see uh we need a center point so the center point is gonna be the location of this trigger right so it's going to be transformed opposition um actually let's abstract this a little bit more so we have the origin right of the trigger so that is just transform that position uh so we're gonna draw that there um normal okay we need to supply a normal for the disk uh we haven't really talked about 3d vectors yet we're going to get to that later and what normals are in general but quickly if you imagine you have a plane um like not a not an airplane just the physical plane um then the normal is the direction that is pointing out from that plane so my finger right now is the normal of the the plane of my hand right so that's the normal direction um or it's there we go now now with a better better normal direction although usually z is the normal uh for surfaces so so the blue vector there is the normal of my hand so in order to draw a disc what that means is that the normal of a disc makes the disc lie in the plane of my hand if we supply the blue vector as the normal to this right but we're in 2d right so so in our case we our coordinate system heck kind of looks like this right uh so we just need to supply the blue vector which is the z-axis in world space so in this case the um we're in 2d we can just um tip over our water water bottles um so uh we can just do vector3 dot uh forward uh this is equivalent to uh doing new vector three zero zero one uh so this is just a direction vector that is pointing in the z axis right um right and then then we have the radius so we want this radius to be the same as the radius of this trigger right um jesus christ all of you want to have that my gizmo i only have two and i hand made them so i don't know how to like sell them um yeah anyway so now we've supplied a normal through this disk we have a radius and an origin so now we should be able to see a wireframe ring around this trigger uh cool um so now we um we can now tweak the radius right uh we can change the radius and the ring will change size we can move this around and um everything is working as expected right um okay i 3d printed my gizmos um and then i painted them that that was my process that's about it um yeah nice i have two they're very useful especially when you get to talking about cross products it's it's super good do you have the schematics um i just made the 3d model at some point i don't know it might actually be at my work computer at nicor but i could just remake the model or whatever it's not a very complicated model to make all right so now the question is how do we actually get the like how do we know if we are inside of this trigger or outside of this trigger right so so now all of a sudden we have a math problem that goes through all the concepts we've been talking about right so we have uh we have a point this is going to be a player or an enemy or anything else right and then we have the point that is the center of the circular trigger all right so how do we solve this um does anyone have any ideas in chat about how to how to do this what are we going to do move it all we need to all we want to know is whether or not this point is inside or outside the trigger how do we know that it's a boolean state something we can put in an in an if statement right check the distance between the two objects yes okay we have the distance what do we then do uh if it's greater than the radius it's outside yeah exactly physics.overlap that works for physics objects but right now we're not using any physics objects or colliders um all right cool so let's do this again how do we check the distance between these two points uh well we have the origin and then we have the object position uh let's call it object position uh that's object transform dot position uh okay so now we need to know the distance between these two so distance equals we can do vector2.distance between uh object position and the origin so now we have a value for how far away is this object from the trigger right but now we also need to know the state of whether or not it's inside or outside the radius of the trigger right that's kind of what we're interested in um okay so we have the distance and the radius so all we need to do then uh is to uh we can make a boolean state so uh let's call it is inside uh and the only case where this is inside is if the distance is less than the radius of the trigger right so if the distance is less than the radius uh then it's inside uh you could do less than or equal to implementation dependence depends on how you want to do this um yeah so now we have a state for whether or not it's inside and let's uh let's set the color of the ring to be red if it's outside and green if it's inside so handles.color is inside and if it's inside we want to do color dot green not green um otherwise call it a red so red outside green inside um and transform has not been assigned so now we have a bunch of null refs uh because this is not serialized so let's serialize it all right still null there we go cool uh right so it's green move it outside it's red and it seems to be able to check this distance right uh so as usual you need to test things make sure you can change the radius and it still works um seems like this this is working uh was everything clear how how this was set up why it works and everything oh what's the vector01 needed for uh but yeah that's just because the disk function is meant to be able to uh draw in 3d space um so so this is just our way of telling it to we just want to draw on the x y plane right because of the we set the normal of the disc to be on the z axis which means that the disc is gonna um traverse on the um x y plane right why not use vector three dot forward um i i mean if i were to write this code when i'm working on something i would write vector3 dot format um just for like educational purposes talking about why like what this is what it means uh vector3.forward is a little bit unclear when i'm teaching what vector threes are so in this case i just type vector three um because it's it made things a bit more clear in terms of what the underlying data is right um but yeah so that's that was the only reason yeah but otherwise uh this is equivalent to vector3.4 it's the same value right uh the only reason that i that i sometimes don't do it is because um transform.forward is very much not the same thing as vector3.4 would um so you need to like keep in mind what you mean when you type the dot forward and sometimes you can like confuse the two um so yeah so sometimes i think it's good to just you know write the whole vector out to clarify that this is just a just a vector on its own one more thing so we talked about um we talked about how to get the length of a vector right and if you recall this is how you get the length of a vector right so you have an x component of a vector you square that and then you add the the y component squared and you take the square root out of that um it was the pythagorean theorem that we talked about earlier that whenever you have a right angle triangle you can get the length of the hypotenuse using that formula right which is equivalent to asking what the length of a vector is right um okay so we got this this formula and we can use this to calculate the exact length of a vector uh if you are doing like if this is in some sort of for loop um and also this person twitch chat is really annoying can we just time out this person there we go great um so okay so if we can um we can use this distance value to check a threshold just like we did here right uh so if we we can check if it's less than some value or greater than some value right um so if you are doing some code that is like iterating over the 600 objects or whatever um so in that case then you might want to optimize this because if you're doing a distance check like every frame across 600 objects or maybe even more um so so in that case you might want to um you might want to optimize it right and there is a very nice way you can optimize distance checks when you're doing stuff like this so so one thing that is probably good to know is that square roots are not like super expensive but they're a little bit more expensive than like multiplying uh they're a little bit more expensive than adding or subtracting so it's sometimes good to keep in mind that the square root itself has a cost to it so again when you're doing four loops over many objects in an update loop or something that's when it really matters right otherwise don't micro optimize this all the time because it's going to make your code unreadable so don't do this all the time i'm just like bringing this up as an optimization tip when you have like very heavy mini distance checks and whatnot all right so um so if we want to make this cheaper we could actually get rid of this square root we could just nuke it just remove it and then we can see what happens right okay so previously we were checking the distance right this distance in and of itself contains the square root uh so this one is doing the whole square root check right but we can do this manually uh so um so so let's first get the uh this displacement uh difference vector um we can call it um this trigger actually let's call it displacement there we go now we're going to use esoteric terms uh so the displacement here is that we subtract one from the other right uh so so this is the vector going from the origin of the trigger uh to the um to the object that we're checking the distance to right so that's the vector going from the center to this one right um all right so that gives us the the displacement vector now uh now we want to calculate the length of this right so i want to get the length of this uh we could do uh displacement.magnitude um but if you want to do this manually just to like clarify the the math involved in this um so so the length or this is technically the distance maybe we call it this distance uh there we go comment that out or just dist there we go all right so we're going to get the distance um between these two points right and the way we do that is get the length of the displacement vector right um so we do the x component and we square that as in multiply it by itself um usually if you want to square something multiplying it by itself like this is usually faster uh than using like a floating point pile function um so if you do like displacement dot x to the power of two in some cases um this is gonna this is gonna be way slower than just multiplying it by itself right so that's a useful thing to know so um so displacement.x times displacement.x what did we just type well we just did this part right the the x squared all right so now we do the same thing for y squared so we add um displacement dot y multiplied by displacement dot y so now we did this part right here with the y components um and then finally we do the square root of the whole thing um all right so math dot square root of all of this uh so we got the distance and now i'm pretty sure everything should work the same way it didn't before unless all of my math is wrong and all right seems to work so nothing's changed so far so now all we've done is basically replace um unity's distance function with the math underlying all of that stuff right um okay so what we're now gonna do is the uh the optimization part so we talked about how you can actually remove the square root and still make the math work out so that we can do a threshold check because again we're only checking a threshold all we're interested in is is it less than or greater than some value right um so in that case we can remove the square root so what does this mean then well now this is not the actual distance uh but this is the distance squared as in the distance multiplied by itself uh so usually it's good to like uh note that with like sq is usually what i use whenever something is squared right um but now the the distance squared is not the actual distance right um oh if you want the like actual algebra involved in this um sorry i should probably should probably write that down um so x squared if you want to get x out of this then if you take the square root if you take square root of x squared these are going to cancel out and you just get x in the end um so like the the square root and uh raising something to the power of two uh they're kind of like um opposites of each other right uh so technically uh square roots are it's just x to the power of 0.5 uh you do we just usually write it out as the square root symbol um anyway so basically they can cancel out and whatnot um so if we take the square root of x and raise that to the power of two uh then we get x so same thing between these two okay uh so so basically when we get the distance here um where we remove this square root we actually have the uh the squared distance not the actual distance because if we want to get the actual distance we need to do the square root on it first uh all right so so now we have this weird uh weird squared distance value so now this one is going to be incorrect right because we just removed a piece of the math um where um we just removed a piece of the path uh that happens to work in this case and this is again important to check for or to test your code right the reason it works here is because our radius is one if we set radius to 0.5 then now you can see that the threshold is incorrect it marks it as inside here um and the same thing if we make the um if we make this larger than one then it's it's counts as outside already here but it really shouldn't do that right um so the way to do this is that we're using the squared value here but the radius we're comparing to is the actual distance but what we can do is that we can take the radius and square that so now both of these are squared so if we square the radius we're comparing the square distance with the squared radius and now if we go back to unity and recompile this is now correct and everything is like working exactly the way we want to but we managed to optimize away a square root like we're not actually using a square root anymore all we're doing is one more multiply instead of the square root which is much faster in many many cases so if you again if you're doing very performance sensitive code you can get rid of that square root now it's a little annoying to have to type all of this so there are built-in functions for this so instead of like typing all of this manually you can get the displacement vector and do squared magnitude um so this is built into unity so you can just do square magnitude and now you have the well the squared magnitude of the distance between these two um and um there is also actually is there is there a um there is no square distance okay well anyway this is sort of the square distance then we're just doing the displacement vector manually and then we get the square magnitude of that displacement vector right um anyway so this is a neat little optimization you can do um but one thing that's very important this only works if you're only checking a threshold if you need the exact distance you have to do the square root because we are no longer calculating the distance all we're working with is this wonky space that is not linear right um actually let's i can show you the quick little example oh desmos.com calculator this is an incredibly useful tool we're going to use this a few times throughout the course um it's good you should use it um anytime you want to graph something um all right so so let's say we we have our distance let's say that's x so if we interpret this along the x-axis so now we have a value representing the distance as we go further away right and obviously it's just going to be it's just going to be one it's a diagonal that just goes up there um but if you do x squared then you can see that um but wait are you what had oh no oh i'm sorry okay um all right so like i mentioned this only works for thresholds if you want to know the exact distance then you want the red value here not the blue value because this is not a distance value um but if you take any point um like if you if you say at here we have one coordinate and then we have another one here um we want to know if you're to the left or to the right of this value that's going to be true regardless of which of these two curves you're using and this is a little esoteric if you're if you're not like accustomed to you reading curves um but basically if you want to check if something is less than or greater than something else which is exactly what we're doing here we're only checking if it's less than or greater than something um in that case um you can just use the squared versions uh because the the comparison is true regardless of if you have the square root or not um that's why we can do this optimization but only when checking thresholds because the blue line is not an actual distance it's a squared distance that is a long thing i need any questions so far how do you calculate the normal of the plane is it the rotation then or um so the it depends on how the plane is defined uh it really depends uh if it's a two-dimensional plane uh then if you want to calculate the normal in that plane like say the say your plane is defined by the the red arrow here uh and this is 2d then the normal is just going to be um it's just going to be the red arrow rotated 90 degrees right and that's it and then if you imagine the plane extending it to infinity along the red axis right then you can just rotate the direction of that plane to get the normal but then again the the thing is whenever you have a mathematical plane the normal is usually built in to the plane itself so you can usually get that normal out of just the representation of the plane that you have but it really depends on like what is your plane defined by like a mathematical plane works in one way uh if you have an actual 3d mesh then things work in different ways right because now you have triangles and maybe colliders that you need to like i don't know raycast against or whatever now so that works differently than like purely mathematical planes okay so the last thing we're going to talk about now um before we end today's thing uh we are going to talk about the dot product so uh swoosh i think we might need to clean up this whole thing okay there we go what a good good circle um let's make a new layer let's draw some lines now okay so this is um this is going to get very close to trigonometry but we're not going to have time to talk about trigonometry today um so instead we're just going to talk about the dot product so the dot products um when you're talking about multiplying vectors together um generally that is an ambiguous statement um so when you talk about multiplying two vectors uh there are many many different ways that we can like approach that like what do you mean by multiply kind of the most straightforward one is that let's say you have two vectors you have vector a and again the the vectors are the components you have the x components and then you have the y component of a vector right or if it's a 3d vector you also have a z component all right so what does it mean to multiply two vectors so if we do a multiplied by b uh if you just write this sign here just this little dot um usually that means that you are doing the dot product between uh vector a and vector b and the dot product is actually a very specific type of multiplication um when you talk about multiplying vectors there are like um there's a dot product uh there is the cross product um and i do believe the wedge product but the wedge product is very close to what the cross product does um but anyway so so there are different ways of multiplying them and knowing which one you want to do is really important uh there's also component-wise multiplication uh where uh you kind of take uh so so there is there's the component-wise multiplication where you kind of multiply these numbers together and then you get a third vector out of that that is the you know this x multiplied by this x gives you some value here and the same thing with the y component then it gets some value here um this is actually one of the least common uh multiplication methods um this is almost only used when you want to scale things on like a non-uniform scale or whatever um so so this is usually called component-wise multiplication um unity has it in the form of vector2.scale or vector3.scale yeah so so this is usually a very specialized form of multiplication that's that's not always very this is not used very much actually um except for scaling okay anyway so let's talk about the dot products um now the way that i usually like to approach the dot product like there are many approaches some people like to go through trigonometry to talk about it uh but i i like to think about things geometrically because that makes sense to me in many many ways um all right so we have our unit circle uh so by unit circle i mean that the radius of the circle is one so any vectors that is pointing to to the edge here has a length of one right so if you have uh let's see wow let's let's put two vectors here we have one vector there and another vector here actually let's use separate colors because that's going to be useful so we have vector here let's call it a and another vector actually let's do let's do this one there we go perfect and b um so now if you have um now we have two vectors right we have vector a and we have vector b um and then we can ask what happens if we take the dot product of these two vectors because the dot product has two inputs it has two vectors as input um usually in math libraries and whatnot it's just called dot um so let's write that down the dot product um between a and b there we go okay so the dot product what is the dot product geometrically um you can think of the dot products as projecting one vector onto another vector so if you look at if you look at b here for instance if we do the dot product between a and b what we're going to get is actually if you can think of b flattening onto a perpendicularly um what the dot product gives you is this length right here so so sometimes sometimes a dot product is referred to as a scalar projection because of this reason it's called scalar because the result of the dot product is not a vector the result is just a numerical value so the dot product between b and a here would be well the whole thing is one so this is less than half so i guess this is like 0.4 or something um so it's approximately 0.4 in this case okay so so you can sort of think of it as projecting one vector onto another now there are some caveats um for this to work um a has to be normalized this one has to be a length of one uh you can you can change the length of b um so if you say make b uh this vector uh now the length of b is not one uh but the scalar projection is still going to be the projected distance along here right so in this case it's going to be like 0.8 or whatever okay so that's what the that's a very like simple uh way of like visualizing what the dot product does it projects vectors onto each other and in this case if you reverse the order of a and b you actually get the same result um so this one it doesn't matter what direction you do this in because again if you uh do this the other way around then we're gonna do a 90 degree projection onto b and that gives us this distance right here and that is also 0.4 right so it doesn't matter what direction we do this in all right so you can swap those doesn't matter uh but you still get like a valid value out of that right um all right so that's what the that's what the dot product does um one thing to note though is that uh the dot product can be negative so it's a little bit misleading to call it a distance um so for instance if we have uh this vector as our b uh then what's going to happen is that it's going to project against the kind of the infinite line here right so in this case uh it would be it would be the same thing it would project against the the other vector like this 90 degree angle and then you would get again you would get this distance but in this case it's going to be negative so you can sort of think of this as a signed distance in this case right so this is a negative 0.4 so here's a very useful way of thinking about this now if you look at a here and you look at the values we have we have 0.4 we have negative 0.4 it's one over here and if we were to do the dot product where uh they're completely opposite of each other uh we would get a value of negative one right uh and you know how we've talked about uh the a number line before right if you think about it b projected onto a is kind of a way of getting a number on the number line of a right so you can sort of extend a and consider that to be a number line right sort of like this so when you project it onto that you kind of get the coordinates along the direction of the axis of a so this is kind of a very nice way of just converting something to some other coordinate system and this is actually used if you want to do space transformation which we're going to talk about next time this is used uh for that for instance um okay um where do you use dot i'll get into that very soon uh first i'm just gonna talk about how how you do the dot product like how do you calculate the dot product between two vectors it is very very simple uh it's like surprisingly simple for something that is incredibly useful uh so the dot product between vector a and vector b um this looks like it's not equals um so the way to get the dot products well the actual practical way is that you use the dot product function in your vector library um but if you want to do it manually you would do a dot x as in the x component of the a vector and then you multiply that by b dot x and then you add a dot y and then multiply that by b dot y that's it it's very straightforward multiply the x components and then add the multiplied y components there's not much more to it um so so that's how you get the dot product between two vectors uh now again this projection example is only valid if uh one or both of your vectors are normalized um it doesn't quite work out if none of them are normalized um because then it doesn't really project because then it projects and scales that distance so it's good to keep in mind that the projection interpretation mostly holds if you're if you're doing if one of them is normalized um so so again the number line example kind of presumes that a is normalized because then you're kind of getting the b coordinate along the vector of a okay another useful way of looking at it um is that see let me erase some things i'm raising too much why did i draw these on the same layer it's fine everything's fine oh yeah like someone is mentioning um the square magnitude uh this also happens to be the way that you get the squared magnitude of something um so if you um so if you do the the dot product between a and a and then you do the square root of this you have a distance function this is mathematically equivalent to getting the distance or the length of vector a so it's kind of a shorthand to do that and if you use the same vectors for both inputs for the dot product okay uh so so here's another way to interpret the dot product um so the dot product can also be used to kind of figure out um how close are these vectors to each other uh so if you think about a here um and you have your vector b when you project this one uh you're going to get a value that is very close to 1. this is going to be like 0.999 whatever um if they are exactly equal as in the vectors are the same and they're both normalized the dot product is going to give you one um so if we can kind of think about what happens when you rotate this vector around if b is pointing here we're going to get a lower value in the projection right because it's moving toward the center now so this is going to be 0.6 or something and then when you're kind of imagining imagine keep rotating this one if it's perfectly perpendicular like this as in it's 90 degrees off of a then the dot product is going to be zero and this is really really crucial so this is important to know so if we track what the values are then it's going to be one over here let's do one uh somewhere here it's going to be 0.5 right uh somewhere here um it's gonna be like 0.8 or whatever and so forth and because it's um because we're projecting this onto a there's also symmetry in this where we would have 0.8 over here as well and if we go perpendicular in the other direction this would also be zero this also projects to zero it's 90 degrees off it's going to project towards this added zero again so we get back to zero and then we can continue to the other side if they're completely opposite then you get a value of negative one and then you have values in between here so you know you would have 0.5 here and 0.5 somewhere here right so what does this mean uh this means that if you have a two normalized vectors and these are and you're comparing two vectors what you are getting is kind of an approximation for how close they are to pointing in the same direction right uh so in this case i'm just talking about normalized vectors both of these are normalized so so if one of them is not normalized then these values are not going to be interpreted exactly the same way but the sign of this still holds um so what you can notice now is that all of the values on this side are negative all of the values on this side are positive right so one thing that the dot product is very useful for is is this vector pointing towards something or is it pointing away from something right because now if we consider the green arrow to be the direction a player is looking and the red arrow is the direction to an enemy or something uh then we can tell if the player is looking away from the enemy by doing the dot product right if the if the player is looking in this direction well oh this should be negative sorry uh then this is a negative uh value right because all of these are negative sorry i forgot to do negative for these um yeah so all of these are negative values all of these are positive values so now if we do the dot product between the player direction and the enemy direction it's going to be negative here because it's projecting here get the distance here and that's negative so this is super super useful um i do have an animation for this too um [Music] not that animation it's the wrong one so here's an example this is basically what i've just been talking about now uh you have two vectors if they point in the same direction you have a value of one if they point directly perpendicular to each other it's a value of zero if they point completely in opposite direction um it's a value of negative one and that that's kind of it so again this is only when you have two normalized vectors um if one of them is not normalized the positive and negative thing those rules still apply uh in that case they don't have to be normalized but for these values to be exactly one and negative one when pointing in opposite directions um they have to be um one of them has been they have to be normalized right can i calculate the second vector if i only have the first vector and a dot product um well it would be ambiguous right because you would have two results not one but i believe it's possible yeah is there any point having one vector larger yes if you don't want to check the if you if you're not interested in checking like are they facing the same direction or some other direction um sometimes you want or very often actually the vector is shorter or longer uh than a normalized vector so this vector projected onto this one sometimes this distance is still interesting and it's very often interesting as well um so let me be a bit more practical because we've been talking about like esoteric things and let's let's think about um what this can be useful for right let's see maybe i should move do i move that or do i keep it angle is implied by the dot product now angle is we're not talking about angles angles is spoilers we're going to get into angles as soon as we talk about trigonometry but right now we're just doing vectors okay here's an actual example from the game that i i'm working on so let's say you you happen to have a game that has a surface of all things um so let's see i guess we can give it some some direction there we go it's very soft and good um i don't know how to make this continuous there we go cool okay so we have a surface we have a point on the surface uh it's solid underneath here so you can just pretend that this is a solid and you have air here uh now we we quickly mentioned or we briefly mentioned that the normals or what a normal is of a surface and again a normal is a something that is pointing directly out from some surface um so like the the blue vector here is normal to the palm of my hand right the other two vectors are tangent to my hand they are not normal right uh so pointing directly out from some surface that's the normal so let's um let's visualize some normal so if you have this surface again the normals point directly out from the surface um so the normals along this surface would like kind of continue like this right and then you get a vector like that and so forth you can imagine these have a length of one uh so these are the normals of the surface so let's think about this point in particular um and let's draw the normal the normal is going to be very large because our circle is very large um so this is the normal let's call that um and now we have the normal of a surface so here was a problem that i had um when i was working on a game back in the days um if you have a surface and then you have an object for some simplicity's sake let's say we have a sphere or a ball or whatever and this object is moving at some speed and it's gonna hit the uh this ground and then bounce off of it right uh okay cool uh it can bounce off of this surface now you want to play an audio clip um so let's say you you're gonna play some audio clip there we go that's my audio clip symbol because you have an impact sound you want to make a clonk sound when the ball hits the ground or something um and now the question is um what is the volume of this sound like how loud should this be um how would you solve this problem um so so now we're getting into like trying to figure out okay if we are moving very quickly uh then obviously we want it to be louder than if it's not moving quickly right if it's moving very slowly hits the surface uh then we might not even want to play a sound right okay so let's just think about this some more so so maybe volume uh is just the speed of the object right cool all right we got the volume equal speed but consider this case if you erase these all right so we have this ball let's say we throw this one down straight down at some speed let's say it's uh 10 meters per second and then we have another ball that's at a very low angle and it's going to hit the ground at the same speed so we want this one to be louder than this one right because obviously this one is going to make a very hard impact on the surface whereas this one is not going to make a very hard impact like it's going to mostly gonna grace the ground and then do a little bounce right um so now we have this issue of like well it's not just based on speed we have to do something else right okay so what can we do so what we can do is that we can use the normal of the surface to figure out how quickly is it moving toward the surface because that's the key thing that we need to know right so so let's let's bring out the normal again so the normal represents the direction of the surface right and what can we then do well we can think of these arrows here as our velocity vector so we're going to get into more physics later but you can sort of represent velocity as a vector um so basically each component like x says how quickly you're moving along the x-axis and y is how quickly you're moving along the y-axis so okay so how do so how does the dot product come into this well what we can do then is that um if you imagine this normal here we have the velocity vector here and then we can project this onto this line right and again the dot product gives us the projected distance here um and then we can do the same thing here right uh we take this one do the dot product and now we get this distance right here um so even though i guess we pretend this arrow is as long as this one uh but the the point is that now using this normal we can get speed in this direction rather than speed um overall right like getting the getting the length of the velocity vector is your overall speed but it doesn't say what the speed is in the direction of the surface right but that's what we're interested in if you're moving directly toward the surface it should be really loud if you're moving really fast but you're not really hitting the surface fast you're kind of moving fast just tangentially then we don't want it to be loud so in this case every time you have some object um that is hitting some surface you know this object is flying onto the surface then we can just do the math if the if this is the velocity we project this onto um onto the normal vector and then we get a magnitude here for how loud this should be right okay so let's try a different direction um another ball that's flying in this direction um well if we do that then we're going to get a vector like this and when we project this one it's not as loud right because now we're only using this span right here and this is much shorter and a much smaller volume um so so what we're getting is the direct or the the the speed along a specific axis so we get the speed along the normal that's what we're getting when we do the dot product between you know the velocity vector of the object and the normal of the surface um yeah so long story short you need the dot product uh basically yeah so so that's that's one use case so that's what i use stock product for for instance there are millions of use cases for the dot product but this is just one of them um yeah um do both of these vectors need to be normalized before using the dot product no so specifically here it's very important that the normal is normalized and that the velocity vector is not normalized uh because we do want to uh the longer the velocity vector is the faster this object is moving right so if you imagine the one that's moving downwards that's this velocity vector if it's moving faster we want it to be louder right so that would still project onto this line here and then we get this value here so we still want it to be louder so we don't want to normalize the velocity vector we do want to normalize the the normal vector though um otherwise we're not going to get things that are in meters per second anymore and it's not going to be a correctly projected um speed along an axis right um can you post that image here yes all right let me just um complete this because i kind of left this incomplete um okay so so better way is something along the lines of volume equals uh the dot product between the velocity and the normal there are some caveats here uh so if you remember how the dot product works uh these values are going to be negative uh so you have to like make sure that they're positive uh you might want to multiply them to change the overall loudness or whatever um yeah but if but but again the dot product product is a scalar um if it's negative and you don't want it to be negative then you can just do the do the absolute value of that right um so getting the absolute value of this would get you um get you some volume um but the core concept is that with the um with the dot product you get a projected um projected velocity along some axis in this case the axis was the normal of the surface right um was that clear by the way uh yes i can post that image in the discard actually let me write some notes to make sure um it's a little misleading to say speed because speed is generally not signed it's pretty much always positive so i'm gonna put that in quotation marks oh people are talking about the cosine uh yes the dot product has a very intimate relationship with the cosine but we haven't done trigonometry yet so we're going to do trigonometry later um okay yeah let's let's post this in your discord geez when people are skipping ahead of your math class and spoiling the what's going to happen in the next chapter yeah i can just show you another example of the dot product um if you want to see in-game this time um so here's another use case for the dot product so i'm working on this game this is called flowstorm um so right now the another thing that i used the dot product for was the pitch of the thruster of this rocket right so if you listen to the rocket here you can hear that the pitch is changing depending on what's happening right you can hear that it increases in pitch um okay so so far no dot products right uh if you move fast we increase the pitch if you move slowly the pitch is lower um it's sort of the same thing with uh sliding like you can hear the pitch change depending on if you're moving quickly or slowly all right um so the thing that i'm using the dot product for is that if you are accelerating and you're turning the rocket left and right actually let me zoom in uh then the the pitch is going to be different it's not just based on speed so if you listen to this while watching the rocket like if even if i'm moving sometimes when i turn the rocket the acceleration or the pitch goes down again even though i'm moving fast right um yeah so so basically what i have done is that um actually let's go to the level editor instead because there's more space um no level editor is currently broken that's okay we can have no reference little refs in in the console um so if you listen to the rocket now we're moving faster and faster sideways but then i look up you can hear that every time i turn where the rocket is now perpendicular to the velocity vector the audio will now change pitch because what what i'm really interested in is how quickly is the rocket moving in the direction you're pointing that's the crucial thing here so in this case uh what i'm doing is that instead of a normal direction um i have the direction of my rocket right uh so so i have this vector right here uh this is the direction uh and then i have the velocity right the the actual velocity vector for um how quickly you're moving in that direction right uh so so the actual velocity could go in either direction right it could be in this direction it could be along this um it could be opposite towards it right um yeah so so that way i can get the speed in the direction of the rocket and then i can use that to change the pitch of the rocket so that's kind of it that's basically like just depends on how you're moving and then project that along the direction of the rocket um and then what i get is uh what is the um how much are you moving along this direction and that's it that's how i uh got the pitch there sounds like it's the absolute value of the dot product yes so so that's that's kind of it do i use the dot product for something else i do use it for the impact sounds uh just like i mentioned before this is the game where i did the impact sounds so when you hit the surface there are little sounds that play that one is based on the dot product just like we talked about um yeah otherwise there are many like placeholder sounds like when you crash that's a placeholder sound so there's a lot a lot of stuff that i haven't done in this game but but yeah otherwise that's how it works peter oh someone asked about the lighting in this uh no the the lighting is not the unity's built-in one i wrote my own custom lighting system um here's some lighting buffers if you're curious about how my lighting system works the uh the first lighting buffer is kind of self-explanatory the um the other three are a little esoteric oh i also use the dot product for the water physics uh so under water i kind of want you to be able to turn the rocket and that would also change the direction of the rocket again you do stuff with the dot product um and the direction you're moving versus the direction you're pointing and that way you can make the rocket sort of behave as if you have drag underwater right um yeah otherwise i think that's it unless you have more questions um now i'm gonna try to figure out what assignments to give you yeah i'm just gonna i'm just gonna have a slow q a thing uh while i figure out the assignments um all right i got two out of maybe three four assignments i haven't decided how many yet be gentle i mean we've mostly gone through a lot of like fundamentals right now um so i'm not gonna do something like super advanced um one of these assignments do i have no idea uh i think uh christopher is going to do all of the all the assignment stuff he's also going to be correcting them so i'm just going to do the i'm just going to make assignments and then christopher can decide everything else um and also christopher is going to be doing the he's going to do the grading and whatnot okay there we go assignment one is now ready i'm going to reveal all the assignments as soon as i'm done writing them oh i just assignment 2 has a thing we didn't talk about today isn't that spicy i presume all of you know what transform.forward means or transform.right um because those are going to be relevant but i presume you've done that already because you've been using unity for a while uh you don't have to be a student to see them i will show them as soon as i'm done writing them i'm just jotting down notes now for the the assignment i'm going to draw more specifically i know you can't see right now um i'm gonna draw what the the assignments are gonna be a lot of these concepts are probably gonna be very easy if you like uh re-watch the stream and whatnot um so like it's mostly about i want you to get some practice in actually doing the assignments and practically writing code that does the things we've been talking about because that usually helps you like solidify what we've been talking about so like try not to copy code from each other unless you really really have to um so like try to see if you can do it on your own at first because that's kind of a better way to make sure that you've learned the things you need to learn otherwise of course look things up if you need to but it's always good to make sure that you you actually know what you're typing if you're not sure what you are doing when you type vector2 dot dot like you should know what is happening when you do that which you might not if you just copy someone else's code right because again my goal is for you to learn what these things are um i don't care if you do the assignment or don't do the assignment or copy code i don't care but i want you to learn i want you to learn the things that are important to you um but then again i'm not going to do the grades christopher probably cares if you copy or cheat or whatever um and if you do them at all um okay all right i got the assignments and here here we go um i'm i'm gonna draw these so that it's like very clear what they're doing and i'm gonna post both the pictures and the the the the text for it assignment number one yeah i don't i think the the assigned the date for these assignments is probably gonna be much later as in you don't have to do them tomorrow if you do then tomorrow that's going to be good because then i'm going to cover these assignments at some point and so it's probably good if you've done them before that um if you want to like maximize your learning but if you're stressed then i mean don't don't stress yourself out okay um cool first assignment re create the radial trigger uh so this is the thing we made on screen um we we had a trigger uh that had some radius right um there we go it's got a radius and then we want to detect uh whether or not a point is inside or outside right based on this trigger and the trigger should be able to be anywhere um you need to be able to move the trigger around you need to be able to change the radius and it should still be able to detect whether or not this point is inside or outside right um so so that's all you need to do really like it's um inside versus outside uh and you can do all of this on drug in androgyzmas uh you don't have to make like an actual update loop or whatever just just do that do it in android gizmos the point is i want you to like try this out yourself and make sure that you've actually like um you can do the concepts that we've been talking about right uh so recreate the radial trigger is the the first assignment can be moved in scene only yes you don't have to animate anything it doesn't have to be in play mode just the same way we did it on stream uh just like make make sure that you know the mathematical concepts but like you don't have to involve like physics or rigid bodies or gameplay or anything like that just to make sure that like um you have the idea of detecting whether or not something is closer to something or not right um yeah so that's one way of doing it um or that's the first assignment so assignment number two um uh so so previously we the radial trigger uh kind of checked like you know are you inside of a trigger or outside of a trigger right and that is based on the radius but now i want you to make another trigger um i feel like i had a name for this um actually i just renamed it there we go that's how rebellious i am um so the next one is that i want you to make a um look at trigger okay so basically you have a similar thing here you have a um actually let's name this player there we go player objects um another player cool uh so then we want a a look at trigger so in this case um if you have the the trigger at some point uh and then the player is looking in some direction we want to be able to detect if you are looking toward this trigger or not right um so that's what what this one is for so if you are looking at this trigger you want it to be true if you're looking away from it you want it to be false right so this one should also you know this one has a radius that you can modify to set the threshold of this um the look at trigger should also have a threshold from zero to one so let's see actually let me move this down because i need to clarify a few things all right so threshold from zero to one um and if um if the threshold is one uh that means that it's going to be very strict um probably impossibly so because you're probably never in practice because of like floating point in precision all right so one is super strict um and this is a this is a floating point value you can set it to 0.5 and everything um all right and then we if it's set to zero uh that means that um so perpendicular or closer means your and it there we go so so this is the threshold so you should be able to if this is set to one uh you have to look like exactly at the thing and it's going to count as triggered only if you look exactly at it because of floating point precision that's going to be like pretty much impossible never going to happen um but if you set it to like 0.5 it should allow you to look a little bit away and still count as looking at this thing but then as soon as you are looking away from it like this that should not count as looking at it right so basically this threshold should set how sensitive it is if it's at one it's hypersensitive you need to look exactly at the point if it's zero it's not very sensitive at all if you look in this direction it's gonna count as looking at the thing right um yeah that's about that's about it for for number two oh and to clarify we haven't talked about angles yet so we're this doesn't involve angles none of this is angle breaks this is still just vectors um all right and let's do the third one still just the vectors yeah angles don't exist you've never heard of angles this one is smaller okay heck i i i'm trying to figure out how to draw this one without spoiling the solution because this is kind of difficult so this is the one we're going sort of toward concepts we haven't talked about yet uh but that's a little teaser thing or something so let's say this is the origin of the world like this is uh zero zero and then you have an object in unity uh that has some some orientation right so usually unity you would have this gizma to look something like this and then you would have the z axis pointing sorry toward the camera and then let's say you have a point there we go okay and then you have this object let's call it object so this one is i want you to make a transform function to transform um world to local and local to world cool okay so the goal is i want you to have a transform in the scene that has some rotation and a position and i want you to write a function that can transform this point uh either into local space of this object or from local space of this object to world space um we haven't talked about spaces yet so that's why this one is a little bit of a curveball um but i'm sure you sort of understand the concept of what it means for something to be local space and world space um so that's what i want you to do and you're not allowed to use unity's transform functions in order to transform this point um you're not allowed to use matrices you're not allowed to use transform dot transform points not allowed to use those those don't exist anymore um so so you need to do this using vector math and dot products um that's what you need to do right yeah you can still use transform dot write transform.up to just get vectors and whatnot uh but you're you're not allowed to use the transform functions uh no you don't need to use quaternions this is possible using only the things that we have talked about oh yeah this is 2d only we don't need to care about 3d yet so yeah 2d only i don't know where to fit that in um i don't think i get the goal of this you need to be able to transform a point from world space to local space which means that you need to figure out where a point is um so if you look at this point for instance i'm this is really hard to explain without spoiling the whole thing if you look at this point here um if you consider the the coordinate of this one in world space well it's this vector right and then you have an x coordinate you have a y coordinate and so forth but making a transform function means that you should be able to take this point in world space and transform it to this space as in i want it to be relative to this object so now i want the x coordinate and the vector and everything to be based on uh the local space of this thing right here right um yeah and it should take rotation into account so it's not just position you can ignore scale scale doesn't matter um but it has to take rotation into account and you can do this using only the concepts that we've been talking about you don't have to do anything outside of that in fact you're not allowed to use matrices or the transform dot transform point functions you need to do this with vector math and dot products and from your point to the position it would be under the new appearance facility move point being world space um you you mostly just need to get the coordinates uh how you like make it in the scene or how you move them around doesn't really matter you just need to be able to get those coordinates um but how you visualize that or whatever is a separate thing you need to be able to get a point relative to a different space um yeah so what i mean by that is that if you ignore these lines that i used to draw you just need the coordinates of this one if you like pretend that whoops pretend that the this right here is world space or whatever because that's kind of what what you do right but yeah basically you want to get this point relative to this space that's it and also the other way around oh and just parenting objects and getting their location kind of like defeats the purpose of it um and the reason i call this a curveball is because we haven't talked about space transformation yet that's the only reason otherwise the the ways to do this only talks about everything we've been um that you only need stuff we've been talking about earlier um yeah um you you can use transform.forward transform.forward transform.right transform.up you're allowed to use those you're just not allowed to use the space transformation functions because the whole idea is that you need to like do the space transformation yourself okay all right i think that's it um those are the three assignments uh oh yeah if you want to join like my discard for my streaming shenanigans uh feel free to hop into discord um we're like a bunch of game developers a lot of us are under the lgbt umbrella so it's sort of like a venn diagram overlapping those two things um so feel free to feel free to join discard let's see i'll post this in the math channel that seems appropriate for the transform from world to local is it okay to use a child object for output um oh if you just wanted to like test your code to see if it works sure uh but like you could do it um if you want just like using gizmos so you don't have to like use actual objects or whatever okay my voice is dead i need to stop talking um and i need to stop streaming but thank you all so much for joining i hope this was useful and that this has helped you learn something or get a refresher on things or i don't know topi is useful
Info
Channel: Freya Holmér
Views: 803,275
Rating: undefined out of 5
Keywords: Acegikmo, Freya Holmér, Freya, Holmér, Twitch, Unity, Unity3d
Id: MOYiVLEnhrw
Channel Id: undefined
Length: 196min 27sec (11787 seconds)
Published: Mon Nov 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.