How To Calculate Vector in Math
How To Calculate Vector in Math
How To Calculate Vector in Math
v=br-o4nnqTCA
(00:01) Hello, welcome to another video where I introduce a chapter from my new book Get into
GameDev: Cheat Codes for the GameDev Interview. Today we're going to be talking about the chapter
called "Vectors," specifically Vectors 1, because I have a whole second chapter which is just dedicated to
practice problems, applying a lot of the knowledge and fundamentals that we're going to gain from this
chapter. This chapter is available for free, so if you haven't yet purchased the book, this is a great chapter
to try out.
(00:29) See where your skills are with respect to Vector maths and give yourself maybe some quizzes.
There are a lot of practice questions in this page, so you can actually quiz yourself in a lot of these cases
and test your knowledge. And of course, vectors are going to very naturally lead into our future chapter on
matrices, which are just a collection of vectors basically. And then there's so much else to talk about
matrices, but we'll get to that. First, let's just talk about vectors. A few notes here about notation. I'm going
to go over kind of how you represent the vectors. By the way, click the link in the description to jump to
this chapter. Then we're going to be talking about Rays, Lines, Line Segments, and Vector formulas.
(00:57) So, this is what's called a parametric definition, which is basically you have a parameter here. In this
case, it's usually represented by T, and this very basic equation here (line or whatever it is) the structure
equals a point plus T * a direction vector. That very simple equation is the same exact equation for a line,
for a ray, for a line segment, for a vector. The only difference, or I guess not for a vector (forget the vector
part), but for all these things, the only difference is the range with which T can be, right? If you have a line
segment, right, let's say it's just my finger or something like that and it only goes from here to here, that's a
line segment. That means the range at which we can add to the point in the direction of the vector is a
little bit here and then maybe a little bit backwards, a little bit of a negative parameter. That's the range;
it's a pretty narrow T range. But if we can go anywhere in one direction, that's a ray, and if we can go any
way in both directions, that's a line.
(02:10) So, that's basically the same formula that comes up again and again. This is important to
understand because when we solve a lot of these problems, we're going to want to break it down to kind
of the simplest version. For example, a classic interview problem, they'll ask you to find the intersection
between maybe a ray and a circle. What you need to do is you need to think, what is the underlying kind of
fundamental problem there? And a ray is just a restricted version of a line, and a circle is just a restricted
version of a plane. So if you instead find first the intersection between a line and a plane, then you can
determine whether or not that intersection falls within a valid range of the line segment and whether or
not that intersection falls within a valid range of the circle on the plane. So basically, you solve that basic
problem and then we narrow in with a more narrow phase detection of whether or not it's actually a
proper collision.
(03:21) Okay, so that's basically how that works. Okay, so we're talking about basis vectors, and basically,
basis vectors are non-collinear vectors. I define kind of when a number of vectors is enough to form a
basis. Here's a nice practice question to get those fundamentals down. And here's hopefully a symbol
you've seen before in game development. These are the basis vectors. These are often used to represent a
transform basis because it's kind of giving you the up, the left, and the right in any given direction. Sorry,
the up, the forward, and the right, those are the different directions here. And important is whenever you
see a transform symbol like this, the RGB is going to correspond to XYZ, right? So when we see the red
arrow—I didn't realize this until pretty recently in my career—the red arrow is going to be R, which
corresponds to X. So I know that this arrow here is representing the X axis. The Y is going to correspond to
G, right? XYZ, RGB. So the Y is going to be this axis, and then this axis right here is going to be for Z. Z is up
in this case; Z is not always up. Some game engines will have Y be up because they're a little silly. But also,
the way cameras work, typically the Z plane is kind of going forward, so I think that's really why. But that's
just an important little trick for you to know, I guess, to determine which axis is the red axis. It's the X-axis.
(05:12) Alright, then we can talk about vectors for distance, and then we're getting into Vector Norms,
which is basically a way that you might say like the length of a vector. So here's a very simple example
problem where you have to calculate the magnitude of the vector 2, 3, 4. Alright, the next step, if you can
determine the length of a vector, you can now normalize it, which is to convert the length to length one,
which is to say to convert a vector to unit length, sometimes referred to as unitizing a vector. Okay, so this
is a chapter on that. This is all important fundamentals because we're going to be using these techniques,
these operations like normalizing a vector and finding the vector norm throughout all of the more
complicated problems that we're going to be getting into.
(05:47) So let's get into our first vector product, excuse me, our first vector operation, which in this case is
the dot product. So, the dot product is probably the most important topic for you to study in this entire
book. The dot product is going to come up in almost every gameplay engineering interview because it is
such a great way to determine whether or not candidates just understand the basics of 3D mathematics.
The dot product is a basic vector operation, but it's one that a lot of people aren't going to know if, like,
they're just a coder, right? Somebody who's just a coder at, like, Facebook probably doesn't know the dot
product. Okay, to be real with you, and I know that as a former person who worked at Facebook as a
coder. Like, I probably didn't know the dot product when I was there, but when you get into the game
development industry, you're going to need to know things like dot product and vector maths. And the dot
product is a great place to start because there are so many problems that they might ask you in an
interview that are directly related to the dot product.
(07:21) So there's a lot to know about the dot product, and this whole section of this chapter goes into
detail about the dot product, in addition to the many ways that it can be written down. There's actually a
few, and then we talk about some of the different characteristics of a dot product, such as understanding
when it will reach certain edge cases. For example, one of the most common uses of a dot product is using
it to determine if two vectors are perpendicular, and that will be the case when the dot product is equal to
zero. I want to underscore here that the dot product is a vector operation, so it takes in two vectors, but it
returns a scalar, right? That's a number; it's not a vector that's coming out. It's a number that's coming out.
And if that number is zero, not the zero vector but literally zero, then that means they're going to be
perpendicular. And if it's greater than zero or less than zero, that can tell us whether or not there's an
acute or an obtuse angle between the two vectors.
(08:29) Okay, so a little bit more about here talking about Cauchy-Schwarz inequality. I probably
pronounced that wrong; sorry if I did. And then here I have a little bit of a practice question here. Okay,
pretty common question where they're going to ask you, "Is something facing something else?" And you'll
have to take just the transforms of those two objects and use the dot product to determine the angle
between them. That is, of course, going to give you the unsigned angle. Okay, the dot product returns an
unsigned angle. So, like what that might mean is, if this is 30 degrees, I need to do a 30-degree turn to look
at my hand here. That is the same as a 30-degree turn here. Obviously, I'm looking in different directions
here, right? One of those is a positive turn, one of those is a negative turn. Well, the dot product can't tell
the difference. Okay, so the dot product is an unsigned angle. We're going to need something a little bit
different called the cross product to determine which direction we're actually turning in. And we'll get to
that in just a moment.
(09:08) But first, another application of the dot product is going to be vector projections. The way that I
learned it in grade school was this idea of it sort of like casting a shadow onto an object. So here's an
example here with a diagram.
(09:41) The idea would be if A casts a shadow onto B , then it would create the X vector. So that's one way
to think about it, but it only really works in a diagram that looks like this where the sun is from the sky
because really you could project anything onto anything, and that's where things get a little bit
complicated. But the basic idea with a vector projection, the way I like to think about it, is that any vector
can be thought of as a composite of different vectors—different basis vectors that add together to make
the vector in question. For example, you can take this vector right here, right? Vector A . Let me zoom in
here a bit if I can. Oh boy, we zoomed in way too much. All right.
(10:18) Vector A is basically the sum of vector Y plus vector X . Can you see that? If we started at the
beginning of A and we add X and then we add Y , we get to the same place that we would have gotten to if
we just went from the start of A along A . Okay? And that's because X and Y can be thought of as
components of A . And in this case, if we can think about the x-axis running horizontally and the y-axis
running vertically, then they are literally the components that correspond to the x and the y-axis, right?
These are the x and the y components of a vector. For example, if you had the vector (1, 1), then the
vectors (0, 1) and (1, 0) when added together are going to give you the same delta that would be applied if
you had just applied (1, 1).
(11:02) Okay, so that's kind of the basic way that I would introduce these components. However, it doesn't
need to be components with respect to just the x and the y-axis. It can be with respect to any axis. So
here's an example where you can see that vector A is the same as the vector Z plus the vector Y . And if it's
the case here that when we're projecting onto B , the result of A projected onto B is going to be vector Z,
because it's vector A 's component in the direction of B , just like in the previous example, vector X was
vector A 's component in the direction of the x-axis. In this case, also in the direction of vector B , but that's
the important thing.
(12:28) The important thing to think about is that we can take components of a vector with respect to any
axis, and that's why we can project a vector onto any axis. Okay, so let's zoom out a bit. There's a lot to talk
about there. I've got a lot of example questions for you, little practice questions that will help you out. The
perpendicular projection I won't go into detail now, but it's basically just finding the line that's
perpendicular to the normal projection that we've done. And then here we have the dot product
inequality, which is sort of like a variant of the Cauchy-Schwarz inequality, but again for dot products.
(13:27) Then I have a nice little video which I recently made that kind of goes into detail with a Unity
example. You know, I was having a hard time thinking about this stuff, so I just kind of went into Unity. I
got some test values and I just practiced it, right? And I totally recommend you do that if you're having
struggles with any of these different vector math product topics. Go on a Unity, test it out yourself. That's
the best way for you to gain a bit of an intuition.
(13:27) Okay, now let's talk about the cross product. The cross product is kind of like the big brother of the
dot product. It's another vector operation where you're taking in two vectors as inputs, but instead of a
scalar result, we're getting a vector out of it. The vector is going to come with an angle, but this time,
instead of an unsigned angle, it's an angle with which we can actually get some sense of the sign. And
that's because the resulting vector that comes out of the cross product is going to be oriented in a way that
is perpendicular to our two input vectors. And if it's perpendicular in one direction—sorry, let me show you
an example. Maybe I'll just use my fingers here. If you take my two arms, right, these are two vectors that
we might put in as input, and you can see how these vectors maybe are perpendicular, and the cross
product is going to find a vector that's mutually perpendicular to these two input vectors.
(14:34) So in R3, there's going to be only one answer, right? Which is going to be the vector that goes like
this, but that vector can be oriented up or it can be oriented down. And that up or down is essentially what
we do to determine the sign of an angle. That way, when we need to turn 30° right this way or that way,
we can differentiate between which direction we're actually trying to turn. So that's one of the basic
applications of the cross product. But there's a lot of other use cases as well. One of the interesting
applications of the cross product is that the magnitude of the cross product, which is to say sort of like the
length of the vector that results from the cross product, is actually equal to the area of the parallelogram
that is spanned by the two input vectors.
(15:41) And that's illustrated in this example here where basically vectors A and B form a parallelogram,
and the area of that parallelogram is actually A X B. This would be an important little trick to know about
because it'll come up later when we start doing some vector math problems in the future chapter. You're
going to need to refer to some of these basic tricks and things like that, so it's important to know things
like that.
(16:16) Okay, some other notes here on the vector—or sorry, on the cross product magnitude. Then
there's a lot of cross product properties like the fact that it is anti-commutative, and what that means. The
right-hand rule, we talk about that a bit too. And then we talk about cross product computation. So when it
comes to actually computing the cross product, it can be a little bit longer than the dot product. It is
definitely a more expensive vector operation, and I go through in detail here on how you would solve the
cross product for the 2D and 3D case—or sorry, just for the 3D case, excuse me. The 2D cross product is
something else.
(16:53) Okay, we're talking about the 3D cross product. The 2D cross product you might think is just the
cross product in 2D, but it's a little bit of a misnomer. It's kind of something different. And so in this
section, I detail what is the 2D cross product, and what does it do, and how do we use it. And I've given you
three practice questions here to help to hopefully help test your knowledge of that so that you get a firm
understanding of what the 2D cross product is, because I do understand it's a topic that is not always
taught in class.
(17:26) Okay, and that's it. That's the end of all of the vector fundamentals, I guess I would say, when it
comes to 3D math. The next chapter is completely dedicated to helping you through going through a lot of
these practice problems. There are so many practice problems here. I know it doesn't seem like a lot; I
scrolled by it pretty quick. But when you click "Reveal Solution," you'll see—well, that one's not that long of
a solution. When you chop open this one, you're like, "Oh my gosh, I'm going to be scrolling for days."
There's so much knowledge packed into this little text, into this little button right here because there is a
lot to talk about here. There's a lot.
(17:53) And I do think if I can bet that any question is going to be on your interview, I'm anticipating at a
top-tier AAA studio, they're probably going to ask you about the vector dot product. So I really want you to
feel prepared going into the interview. This is definitely one of the ones that I want you to practice and go
through. This chapter will be available if you purchase the book. If not, you can go through just the practice
questions that are in the previous chapter. So if you're a free user, then we'll see you
(18:22) I guess in the practice test chapter. That is the other one that's going to be a free chapter.
Otherwise, everybody else, jump on the train. We're heading on over to matrices, which are so important
to understand, transformation matrices really being the basis of how we represent all object movement
within 3D game engines. So I'll catch you in the next one. Join me in the matrices chapter.