Hello, there's no two ways about it.
If you want to get started in games programming, you're going to need to know some mathematics.
But it's not all doom and gloom.
As I have demonstrated many times on this channel, you can achieve some really cool stuff
by just understanding a core set of mathematical functions and operations.
The recent videos on the channel this year have been a little bit more complicated than
usual, and this is prompted quite a few of you to ask the question, what maths do I need
to know in order to be a games programmer?
Well I hope this video goes some way to answering your question.
So I'm going to start with something that's really fundamental and that's Pythagoras theorem.
And as games developers, we'll be using this all over the place.
And it works with right-angled triangles.
Now a right-angled triangle means that one of the angles is 90 degrees.
And that's really important and we'll see this a lot in this video.
If I label the sides of the triangle in the following way, so here we've got A and B and
C, Pythagoras theorem links together the lengths of these three sides.
And there's an easy way to remember this relationship.
It's as follows.
We have C squared equals A squared plus B squared, which means if we know the length of A and
we know the length of B, we can calculate the length of C, which is known as the hypotenuse.
Some simple algebra, we can rearrange this to be C is equal to the square root of A squared
plus B squared, calculating the length of C is quite an important thing in game development.
Let's for example assume we're working on a two-dimensional game and we represent our
screen as a per of x and y axes, so here I've got x and here I've got y.
This of course then is our origin 00.
Now let's say I have two points on my screen and I wanted to calculate the distance
between the two points.
One of these points for example could be the player and the other point could be the
enemy.
Perhaps when the player is close enough to the enemy, the enemy's response changes.
So knowing this distance becomes a critical form of gameplay manipulation, but all we have
are the two points, so here we've got p1x and p1y and here we've got p2x and p2y.
If we form a right-angled triangle out of those two points then we could say that B
is equal to p2y take p1y, it gives us the difference between the two points and likewise
we could say that A is equal to p2x take p1x which gives us the distance in the x axis.
Therefore we can substitute into our formula to determine the length C. C equals the square
root of, it's going to be a long one, p2x take p1x squared plus p2y take p1y also squared.
In principle it doesn't matter whether we do p2 take p1 or p1 take p2, all C is the distance
between the two points and that distance, well it's just a fixed scale of value.
So whether we look at it from the player's perspective or from the enemy's perspective
doesn't make any difference.
Once we know this distance we can make assumptions, so for example if C is less than
or equal to 50, that might be some unit in our game space, then attack.
Whenever we're working with two-dimensional games we'll always be working with an x and y axis
like this and it's important that the x and y axis are at 90 degrees to each other.
And this is enforced by the layout of the screen and the layout of our arrays in memory
so it's very natural to think of things in a two-dimensional system such as this.
And even though we didn't specify a triangle explicitly we could derive a triangle just
from these coordinates in the 2D space and this is a principle which we'll keep coming
back to time and time again throughout this video.
If you're going straight into three-dimensional games or four-dimensional games or 27th
dimensional games you can simply add more terms to this as required.
Where each term represents a distance along one of your world's axes.
The next area of essential mathematics that you'll need to know about for game development
is vectors and I do not mean standard vector.
This is not what I'm talking about, they are not the same thing.
Let's take an axis again and somewhere on my axis I have a point.
We know that the coordinates of that point is defined by its location along both of those
axes.
So here will be its x coordinate and here will be its y coordinate and we can happily
represent this position, let's say it's p as being x, y.
On the one hand this point is just a point in 2D space but we can also consider it as
being a distance travelled from the origin.
So starting at 0, 0 and going to our point p.
If we always assume it starts from the origin we never need to include that anywhere.
So the only information we need is the location of the point.
When we think about things in this way what we have described is a vector and vectors
are quite important because not only do they describe a point in space but they also
describe a direction.
Our vector can point in any direction and be any length long.
When we want to describe a vector we usually see a little arrow above the letter we're
using to describe it and it's convention to use a column of parameters to specify the
point part of the vector.
In this case we know that our point was at x and y so in the literature if you start
to see this notation you know you're going to be working with vectors.
The key thing to remember is a vector has a length and a direction.
Now how do you think we calculate the length of a vector?
Well hopefully it's already obvious that what we have created here is a right angle triangle.
And if we know these two sides and we do this one is y and this one is x then we can
use Pythagoras theorem to calculate the length of this vector.
The length of a vector is also known as its magnitude so here is our vector and usually
you would put two bars either side of the symbol to represent the magnitude or the length
of the vector.
And we know now that that is simply Pythagoras' theorem.
Vectors can be added and subtracted.
So let's say I get to this point and I then use another vector to define another point.
And let's say I do it again this time we have a vector quite a long one going this way
and one more time why not?
Big long vector going over here.
Now label my vectors b1, b2, b3 and b4.
We always assume that a vector in isolation starts at the position 0 0 and when we add
vectors together we assume there's a little 0 0 just here and then there'll be another
little 0 0 just here and up here.
Adding vectors is very simple.
If we wanted to work out the location of this final point over here we can add up all
of the vectors on this path to give us that point and therefore we can say that p is equal
to v1 plus v2 plus v3 plus v4.
At the vectors, don't forget these will each have their own sets of x and y's.
We add together the common components of each vector.
So we come as x plus x plus x and y plus y plus y plus y which will give us the position
of our point as a vector and this holds true because this is still it's a point in 2D space
but it is still a vector starting from the origin.
So let's assume we have a player character which is walking along a path and it visits
all of these points.
We can describe in 2 dimensions that path quite nicely with vectors.
We can also sum the lengths of each vector to see how far the player is walked by working
out the magnitude of each vector as we go along and after we've walked along that path
because we've got a resultant vector p we can work out the length of that vector which
will tell us how far we are from our starting position.
The important thing to remember about vectors is they are a length and a direction but what
do we do if we don't care about the length of the vector and really all we're interested
in is the direction.
So we're going to lose this scalar value of the vector.
Well there's no such thing as removing the length, the best we can do is assume that
the length is 1 and vectors that have a length of 1 are a unique type of vector that
are called a unit vector and we'll see later in this video that there's quite a few
properties of unit vectors we can exploit to our advantage.
The nice thing is any normal vector can be converted to a unit vector through a process
called normalizing, fundamentally our vector is a right angle triangle and I've already
shown that the magnitude or length of a vector is calculated by Pythagoras' theorem.
If we assume the original length of our vector was 10 but we want to represent it as
a unit vector its length must become 1.
The direction remains the same but this is of course changed our x and y values.
They've become smaller and due to the proportionality effects of right angled triangles
this is in fact become x divided by 10 and this has become y divided by 10.
They've become 1 tenth of their original size.
The hypotenuse will have become 1 tenth of the original size and therefore the sides
of the triangle must also reduce by a factor of 10.
This means if we can calculate the length of our original vector and divide that vector's
components by the length we can establish the unit vector which has a length of 1.
The notation for a unit vector has a little hat over the top or a shepron and therefore
we can also imply now although you won't see this written out very often that the unit
vector consists of components x divided by the square root of x squared plus y squared
and y divided by exactly the same thing and therefore by taking our vector dividing
its individual components by the length of that vector we end up with our unit vector.
This has a magnitude or a length of 1 and of course it still has direction and it's
this direction that we're interested in.
If we always assume the length is 1 all that's left is the direction the only interesting
feature of this vector.
Unit vectors have many uses but perhaps the most obvious one is that they can be scaled
very simply.
Let's say I wanted to find a point along the direction of my vector but that point must
be and let's just pick a number for the sake of argument, 8.3 in length away from the
origin.
Instead of doing any complicated calculations we can simply multiply our unit vector by 8.3
because multiplying 1 by 8.3 is equal to well 8.3 and the direction is still the same.
So this point here we could define as being p equals our unit vector v multiplied or scaled
by 8.3.
Unit vectors in games are great for specifying direction.
So here I've got a crude car and if I assume that the centre of the car is lying at
the origin I can represent the direction the car is travelling in as a unit vector.
The distance that the car travels I can just represent as well the distance.
A scalar value I don't need to know the direction.
The direction is included in this unit vector the distance is what will scale the unit
vector by to work out where the car is in the future.
So we know that the distance will lie anywhere along that direction vector and to work
out where the car is in the future we just scale our unit direction vector by the
distance that we're interested in.
I think fundamentally vectors are a great way of navigating space so in 2d we'll use 2d vectors
like I have here but the same applies to 3d as well.
The next area I want to cover is angles.
This is very basic trigonometry and when working with angles it's useful to just memorize
a few formulae.
We're already now quite familiar with a right angle triangle it can represent.
The distance between an enemy and a player and it can represent a vector.
The important thing is this angle here is 90 degrees but what if we wanted to know what
this angle was?
This is the symbol theta commonly used to represent angle and as usual we've got our x-axis
and our y-axis.
When we look at the triangle from the perspective of this angle here we can label the sides.
The longest side of a right angle triangle is always called the hypotenuse.
In this configuration over here the y-axis will be called the opposite side and the
x is called the adjacent.
So we're looking at the triangle from this corner and the rule I want us to learn here
is that the tangent function you'll see that on your scientific calculators of theta
is equal to opposite divided by adjacent.
It's a ratio and therefore we can also assume that theta is equal to the inverse tangent
with tan with a little minus one of the same thing and just to keep the writing down
I'm going to use y over x and so that's simply not.
A hypotenuse could be a vector we know how to calculate the length of a vector now and
we know its direction and we know how to break it into its x and y components we can work
out the angle of that vector relative to the x-axis.
Now just a little side note in a lot of programming languages we don't see tan to the
minus one instead what you'll see is a tan y divided by x but whenever you see a divide
over potentially a variable you don't have control of we need to be sure that this x
isn't equal to zero and well why is this we know that the internal angles of the triangle
must add up to 180 degrees and if we draw a triangle where the length x is equal to zero
well we don't have a triangle at all we have just a line going up and this means
this division can start to yield problems with our program it could crash it could make
the numbers invalid it's better if we can perform this calculation with a degree of safety
and so again a lot of languages provide a secondary tangent function often called a tan
two which takes as arguments y and x and this function performs the necessary safety
checks to make sure we get an answer that we might expect so if we were looking for the
angle of a point away from the origin in this instance we would get theta and as the angle
moves around towards y we would hope to get here 90 degrees and the a tan two function
will make sure that we get the result that we expect I should also point out at this
time that most programming languages don't work directly in degrees instead they work
in radians and convention is to always specify these angles in radians or degrees relative
to the x axis and as we go around we start to form a circle and a circle starting here
is zero degrees or 360 degrees they're the same thing and over here we would have 180 degrees
90 and 270 while degrees are great for humans to understand they're a bit enough for
mathematics to understand and instead maths prefers pi don't we all so in radians zero
degrees is zero radians 180 degrees is pi and a full circle is 2 pi 2 times 180 our 90 degrees
is pi over 2 or if we look at that in relation to the entire circle it's 2 pi over 4 it's
the first quarter of the whole 2 pi 180 would be 2 pi over 2 it's the first 4 half 3 quarters
of the way around would therefore give us 2 pi times 3 quarters which is 6 pi over 4 which
is 3 pi over 2 it's always a bit awkward that one but what we can see is there is a linear
relationship between degrees and radians and we can define that as being radians equals
degrees divided by 180 times pi and naturally it follows that degrees therefore our radians
divided by pi times 180 it takes some getting used to as humans we like to think in terms
of degrees but the functions in our computer programs they like to think in terms of radians
with a bit of practice and experience eventually humans start to think in radians 2 but
I wanted to highlight this in case you start using some of the trigonometric functions
and you're not quite getting the results you expect now I started this section by saying
there are things you just need to memorize and one of those things was that the tangent
of theta is equal to the opposite over adjacent and I like to remember that as toa tangent
of theta is equal to the opposite divided by the adjacent toa is one third of this phrase
so care toa and depending on what information we have to hand decides which part of this
phrase we use for calculation so when we knew the opposite side and the adjacent side we
can work out the missing angle with the tangent function if we know the adjacent and the
hypotenuse then we can work out theta with the cosine function and if all that we have
is the length of the opposite side and our length of the hypotenuse then we can work
out theta with the sine function and writing it out like this makes it a very easy thing
to remember so care toa now I've just introduced the sine and cosine functions so let's
have a think about those sine and cosine are periodic functions why they do what they do
is well beyond the scope of this video but they do have some useful properties here I have
an axis and I've got y in the vertical direction and I'm going to put theta the angle along the x I'm
going to do my very best to accurately plot y equals sine of theta it goes up it comes down and it goes
up again and it'll keep doing that forever and ever and ever and ever and it goes between
plus 1 and minus 1 do you know what just for clarity I'll write these in in the colours so red was
sine theta in green on the same axis I'm going to plot cos theta and cos theta looks like this
it's not bad and again carries on doing its thing forever and ever and ever now you're thinking why
sine and cosine important let's plot a second axis y and x at this point here we've done a full
revolution so this would be where the angle is 360 degrees or of course 2 pi in radians so let's
have a look where x and y lie when we calculate x and y using sine and cos and I'm going to use
y is equal to sine and x is equal to cosine starting with the theta of 0 and looking at the green line
which is our cosine in x we can see that our x coordinate is plus 1 and if we look at the red line
for sine our y coordinate is 0 so I'm going to plot that here it's 1 and 0 I'm now going to look at
other locations on this plot of sine and cosine so let's look here where it's crossed 0 in the y axis
this also happens to coincide with the peak of the sine wave here my x is now 0 but my y
is plus 1 0 comma 1 look at the next interesting feature which is here where the sine wave crosses
the x axis also happens to be the lower peak of the cosine wave which gives me an x of minus 1
and a y of 0 and no prizes for guessing if we look at this last crossing point we see that x which
is cosine is 0 and our y is negative 1 now I've picked out points here that are easy to draw and
represent on this axis but if I picked every single point along this waveform absolutely every
single one what we would find is it plots a perfect circle as we increased theta and this circle
always has a radius equal to 1 it's a unit circle and if we think back to our vectors where we
had a unit vector it looked very similar it was a vector pointing in a direction with a length of
1 so given a specific theta we can calculate a point in x and y which also happens to be a unit
vector and this is quite important because just having a theta value alone we're able to describe
a unit vector and we know that we can scale a unit vector which will give us any point in our 2d
space this is also known as a polar coordinate we're given any theta and any radial length we can
describe any point in 2d space so why is this important to game developers well in a similar way
to our car example before let's assume this time we have a spaceship but all we know about this
spaceship is what angle it is relative to the x axis we now know how to turn this angle
into a direction vector for the spaceship to travel along and it's also now very easy to rotate
the spaceship because all we need to do is increase or decrease the angle so knowing about
sine cosine and tangent we can easily swap between using vectors and using angles and it depends
on the gameplay scenario you are trying to invent as to which approach you might prefer to use
rotating things is certainly easier to think about when working with angles whereas moving things
along paths and in certain directions is much easier to think about using vectors so being able
to swap between the two when necessary is a requirement for most aspects of game development
however there are some things to be aware of sine cosine tangent and square roots
can all be considered quite computationally intensive i.e. they require a lot of CPU cycles
in order to give you an accurate result however what we've seen so far with vectors other than
the square root function vectors are trivial for a computer to compute so we're possible
we prefer to work in the domain of vectors instead of angles and this is where another important
mathematical concept which is essential for game devs is the concept of the dot product which
amongst many things allows us to think about angles in terms of vectors here i have a space
with two vectors i'm going to call this one v1 and this one v2 and i'm interested in trying
to work out the angle theta between these two vectors well let's look at doing this the long way
round first i can break my vector down into a right angle triangle to give me an x and a y
component and i can use inverse tangent to give me an angle relative to the x axis i can do exactly
the same for my other vector give me a big theta this time and it should be intuitively obvious
that the red theta is equal to the orange theta minus the green theta so we can express that
the angle between the two vectors is going to be equal to the inverse tangent of v2 y over v2
x minus the inverse tangent of v1 y over v1 x but here we've got lots of things to worry about
firstly we've got this divide condition as come back that's okay you can use the a tan two
function then we've got these computationally expensive functions to use anyway these inverse
tangents but we'll get the result that we want eventually there's a slightly different way
to think about solving this problem recall that vectors contain direction and the length but the
length is largely irrelevant in this situation it doesn't matter how long these vectors are
the angle between them isn't going to change so let's represent these vectors as unit vectors
the dot product is defined as being the sum of the product of the individual components of
the vectors so in this instance here we might see v1 x multiplied by v2 x plus v1 y multiplied by v2
y this is a scalar result it doesn't give a vector as a result just a single number and let's
have a look why for the time being I'm going to assume that our x axis is also a unit vector
so here we've got one zero so if I were to take the dot product between v1 and my x axis
I would have v1 x multiplied by one plus v1 y multiplied by zero therefore the dot product in this
instance is simply the same as the x component of the vector what we've calculated is how much
does our v1 vector project onto the x axis and by project what you can think of
is if there was a light source casting down onto our x axis this line here would be where
the shadow stops and this line will always be at 90 degrees to this vector which represents our
x axis notice and not uncow incidentally we've created a right angle triangle and in this right
angle triangle we happen to know the adjacent we happen to know the hypotenuse and what we're
trying to calculate is the theta so if we go back to the cat part of soccer toa we can see that
cos theta is equal to our dot product result divided by our hypotenuse but our hypotenuse
was a unit vector so we know it's length it's one therefore our theta is simply the inverse
cosine of our dot product we got this optimization because we moved to unit vectors
if we didn't move to unit vectors we could still get the correct result but we would have to
use the proper length of the hypotenuse so we would have to use Pythagoras' theorem to get that
value but this is one of the reasons I wanted to emphasize the importance of unit vectors
they can save us a lot of computation so now we've worked out the angle of one of our vectors
relative to the x axis we could go and do the same for our original v2 calculate how much it
projects onto the x axis and perform a similar equation to help here where we look at the
difference between those two results however there is no reason at all for us to project directly
onto an axis our dot product will allow us to project one vector onto another so assuming we want
to project v2 onto v1 we want to see at what point does the shadow extend to a long v1
providing we've got some light source that is perpendicular to v1 and this is precisely
what the dot product calculation calculates doesn't matter whether it's the x axis or another vector
and so assuming we're working with unit vectors we can calculate that angle as simply being
to the minus one of the dot product between the two vectors and that's usually represented literally
by a dot but I emphasize this is for unit vectors if you don't have unit vectors you're going to
have to calculate the magnitude's first calculating the angle is all well and good but we've got
this nasty inverse cosine function to use now if you do need the angle there's no two ways
you've got to do this function to convert this ratio into a meaningful angle there are intuitively
other uses for the dot product scalar value that we get as a result of this calculation consider
the following here I've got my x axis which I'm going to describe as a unit vector and here
I've got another vector which is also a unit vector I'm going to calculate the dot product
between these two unit vectors so we've got v dot large x in this case which are both unit
vectors we'll calculate the dot product so that's 1 times 1 plus 0 times 0 equals 1 now let's
consider our unit vector traveling along this y axis I'm still going to take the dot product
with the x axis unit vector dot x is now 0 multiplied by 1 plus 1 multiplied by 0 equals 0 let's
take a look at a final vector again a unit vector in this instance I've got minus 1 times 1 plus
0 times 0 equals minus 1 what we've seen is as we travel around like this we've gone from 1
through to 0 through to minus 1 I like to think of this result as how similar are two unit
vectors when they are the same we get the result 1 when they are perpendicular to each other at
90 degrees we get 0 and when they are in opposite directions we get minus 1 and so if I were to have
two vectors like this they're quite similar I'd expect my dot product between those two vectors
to be somewhere near 1 if I have two vectors like this well they're not very similar
so I'd be expecting the dp to be somewhere around 0 and if I have two vectors like this well they're
not similar at all in fact they're quite opposite so I'd be expecting the dp to tend towards
minus 1 and I find identifying the similarity between vectors to be a very important thing indeed
let's consider a small section of racing track and on that racing track we've got some sort
of finish line marker and I have a car driving along the track when the car passes this marker
I want to increase the number of laps I know the direction vector of my car and for this region
of the track I've specified a line that we want to cross but I've also specified a direction
that that line must be crossed at in fact it's 90 degrees as the game is being played
at some point the car crosses that line and we detect it and at that point of detection I can take
the dot product between the direction of the track of the direction of my car and I know that if my
dot product result between those two vectors is greater than 0 then I have crossed the finish line
the right way round so I can increase my lap counter however if the direction vector of my car
dot product with the track direction is less than 0 I know that they're not similar at all
and in fact I must have crossed the finish line in completely the wrong direction so maybe I want
to decrease the number of laps let's consider a slightly different scenario here I have some terrain
for a small simple platform game the character has a direction vector and these segments of terrain
have what's known as a normal vector it's at 90 degrees as my character is traveling along the
terrain I can take the dot product between the two vectors here we can see that they are 90 degrees
to each other so the dot product is going to be 0 and the same applies down here but as the
character gets onto the hill the direction vectors become far more similar they tend towards one
so I could use this number to influence the speed of my character as it's going down the hill
it's traveling faster let's look at it from the other side this time my character is trying
to go up the hill well now we see that we've got direction vectors which are going in opposite
directions they're not similar at all and so in this instance the dot product is going to tend
towards minus one so I could use this information to really slow down my character as he struggles
to climb up the hill these two examples I've shown they're quite trivial but knowing the similarity
between vectors and in this instance the similarity between direction components of vectors
has a lot of utility in game development especially when it comes to things like collision detection
and physics and we're able to calculate these reasonably complicated relationships
without falling back onto any computationally intensive functions at no point during these
calculations do we actually need to know the angle of anything we're just looking at the
relationship between the two vectors which is expressed as a scalar value the dot product
though please remember everything I've shown assumes that the vectors you're using are unit
vectors the dot product can start to mean different things when you're doing the dot product
between non-unit vectors and I think that's a little bit beyond the scope of this video
okay another absolute mathematical essential is linear interpolation this is also known as
lirp for short or lirping let's assume I have two points in space p1 and p2 and I know that I
want to travel from p1 to p2 in precisely five seconds I want to travel in this direction along
that line and I wanted to take me five seconds to do so this means that every frame we're rendering
the game we're going to want to render a slightly different location somewhere along this line
now it's quite useful to think about this in terms of percentages so when we're at the start
this is going to be zero percent and when we get to the end that's a hundred percent halfway along
that line therefore is 50 percent so let's start by trying to understand where 50 percent along that
line is and we'll call that point point t well let's break this down into yep you've guessed it
a right angle triangle and we will work out where the point t is in the x axis and the y axis
separately so for point t in the x axis we know at zero percent we must start at p1 so it can't be
any less than p1 we also know where the location is at one hundred percent in the x axis
since we know the ending location in the x axis and the starting location in the x axis
we can work out the distance traveled in the x axis and 50 percent must be halfway along
that distance simply due to this proportionality of right angle triangles that we talked about
earlier so I can take my final point x work out the distance from the starting point and multiply
that by t which is my percentage of course in a computer we wouldn't represent 50 percent
we'd represent this as 0.51 and 0 exactly the same calculation can be done for y and so by
specifying a value for parameter t between 0 and 1 we can get any location along our line we have
interpolated linearly in a straight line between points p1 and p2 now let's assume that per frame
0.1 seconds of time passes and since the start of the motion I'm accumulating this time
in capital T I wanted the whole motion to take 5 seconds and therefore I'll express t over 5
as being the percentage that controls my parameter little t in my linear interpolation equations
I've taken the rather odd approach of introducing linear interpolation in two axes
the number of axes is completely irrelevant linear interpolation works with scalar values vectors
multi-dimensional variables it doesn't matter the fundamental principle is this that given two values
we can find another value in between them at any point and t equals n starting point
or starting value plus n ending value maximum value minus the starting value multiplied by
our percentage 100 percent will give us the end 0 percent will give us the start and it doesn't
actually have to be bound between 0 and 1 we could continue extrapolating along that line
and linear interpolation has uses all over game development not only for controlled
motion such as this so imagine we had a sequence of vectors and we wanted to linear interpolate
along each vector per frame so we got enemies that follow a path or perhaps we wanted to
fade between different colors we could use linear interpolation in the red, green and blue
spatial domain a slightly more advanced use of linear interpolation is to implement functions
that we can't define mathematically let's say I've some curve that looks like this
deriving the function for that curve would be quite a challenging thing to do but our game requires
that we follow a trend of y equals f of x if we can't define such a function easily we can
approximate it with known points that we wanted to provide these could be handcrafted so now when
we're given a specific x value and we want to look up in this function what its y value could
be we could just take the x value and round it to our nearest point which would end up giving us
something like this the problem here is our approximation to the function isn't very smooth
a better approach would be to given any arbitrary x value in this space work out the two neighboring
points that we do know and linearly interpolate between them it doesn't give us a perfect curvature
but it does remove the step like nature of just using x on its own so linear interpolation for
approximating functions can save you the headache of deriving the function in the first place
and it's also very useful in things like audio applications and sound if we assume that these were
sound samples we've taken a very crude approximation of the sound which might end up sounding
quite robotic or distorted and instead we can access any value on that curve as any spatial
resolution of x and we linearly interpolate between the points that we know to smooth out all of
of these rough edges we've just looked at how linear interpolation could be used to make an
enemy or a player follow a particular path but what happens if we don't know that path in advance
for example we have a player character running along and at some point the player presses the jump
button which causes the character to jump we haven't predefined a path in advance that we want
the character to follow but also the path is non trivial it's got curvature because the world
in our platform game has gravity we can use some very simple kinematics and bring together
everything we've seen in the video so far in order to solve this problem and you might think that
sounds absolutely horrific but I think also there's a certain elegance to it when you actually see
its simplicity we can represent our players position in space as a point and we know that a point
is also the same as a vector as the game is progressing time is moving forwards and time is an
important part of motion you may remember that speed equals distance over time and if you're not
familiar with this well think of it in terms of miles or kilometers per hour or meters per second
and so if we happen to know the speed that we're moving at we can rearrange this to tell us how
far we've traveled in a particular time it's simply speed times time now speed on its own is a
scalar value but we've been working with vectors so when you actually have speed in a direction
that's known as velocity so our player should also have a velocity vector in games we typically
chop up time into the amount of time that's passed per frame this can be known as delta time or
elapsed time and so taking a character with a given velocity vector how do we calculate where it
should be on the next frame well this is simply a matter of taking the players current position
and adding to it the distance traveled given the speed and the time now our speed is not scalar
it's a velocity vector it includes direction but we're only going to travel a certain distance
because we've got a cap on how much time we're traveling in that distance for this means we can
argue that the position of the player on the next frame is equal to the position of the player
on the current frame plus the velocity of the player on the current frame multiplied by our delta
time the elapsed time between frames so with respect to time velocity changes position the problem
is if our player has a velocity vector they're just going to keep on moving in that direction
nothing is ever going to stop them yes when the user presses the jump button at a particular
location we can change the direction of our velocity vector and potentially it's magnitude to
change the position of the player in time we can get this jump but that's going to happen
in a straight line because nothing ever changes the velocity well that's not true is it because
we know that we won't just keep jumping in a straight line all the way across the universe
at some point we do actually have to come back down to earth and there is a force that does that
called gravity and gravity is an acceleration and accelerations change velocity so i'm going to
use exactly the same type of formula but this time i'm going to change my velocity on the next
frame is equal to my velocity on the current frame plus an acceleration multiplied by our delta time
i can keep acceleration fixed so for gravity for example it might be something like
there's no change in the x-axis but it pushes us downwards in the y-axis we can use acceleration
to change velocity and we can use velocity to change position and this is a very nice system
because at no point do we need to know in advance where the player is going to be so whereas before
we were linearly interpolating along vectors between points now we can let the system just
update itself and the character will move in accordance to the motion that we've described
by our acceleration and velocity vectors i wanted to include this at the end simply because it
does bring everything together we're now working with vectors and we might be interested in
changing the directions of vectors based upon the landscape or the orientation or angle of the
player and now we're equipped with all of the tools to convert between vectors and angles
and understand how to work out where we are a longer vector with respect to time
this video has been a brief introduction to the essential mathematics that i believe aspiring
game developers need in order to get the most out of the crap and fundamentally i've only talked
about angles vectors and motion but we've seen throughout the course of this video that actually
all of these are related in quite simplistic ways and that's understanding the relationships between
these things allows you to make better decisions as a game developer it's up to you to decide
what to use and when to use it but don't make life difficult for yourself by making a decision at
the start i'm only going to use angles for things or i'm only going to use vectors for things
this video should have equipped you with some of the basic tools to translate between them
another way to look at this if you did want to explore things a bit further is angles is all
about trigonometry and vectors is really all about geometry motion and i know we've only looked at
it very simply comes under kinematics or indeed plain old physics those of you that have been
game developing already for some time or i've watched a lot of the videos on my channel may
notice there's one important thing missing from here and i'm not going to talk about this video
because i've covered it in lots of other videos on this channel but that would be matrices
or linear algebra all of these things combined together give you the ability to manipulate space
whether it's two dimensions or three dimensions or more and manipulating space or manipulating things
in space is fundamentally all game development is well perhaps i shouldn't say all game development
but most conventional game development that involves characters action movement that sort of thing
and i hope this video is provided enough confidence for people that may not have the strongest
maths that you don't necessarily need to know advanced maths in order to achieve
quite cool things in game development maths certainly helps i can't deny that but you don't need
much in order to do lots of fun stuff if you found this video useful a big thumbs up please
have a think about subscribing and i'll see you next time take care
Introduction to Mathematics for Game Programming
Pythagoras' Theorem Overview
Calculating Distance with Pythagoras' Theorem
Understanding Vectors
Magnitude of Vectors
Introduction to Coordinate Systems
Understanding Cartesian Coordinates
Angle Calculation with Tangent Functions
Working with Polar Coordinates
Applications of Coordinate Systems in Games
Transformations in Game Development
Understanding Sine and Cosine Functions
Unit Circle and Vectors
Dot Product and Vector Similarity
What is Pythagoras' theorem and why is it important for games?
How do we calculate the distance between two points in 2D space?
What are vectors and how do they describe points in both distance and direction?
What is a unit vector and how can it be applied in game development?
How can coordinate systems impact game navigation and mechanics?
What are the differences between Cartesian and Polar coordinates?
What transformations can be used in games to enhance movement?
Why are coordinate systems crucial for rendering graphics in games?
Why is the tangent function crucial for angle calculations in games?
How do sine and cosine functions help in game physics and navigation?
What is the importance of the dot product in understanding vector relationships?
In this video, we'll cover the core mathematical functions and operations needed to know as a games programmer. Starting with Pythagoras' theorem, which relates the lengths of the sides of a right-angled triangle, we'll see how it's used in games development. We'll also discuss how to use this theorem to calculate the length of the hypotenuse of a right-angled triangle, and how it can be applied to various game development tasks. By understanding these mathematical fundamentals, you'll be able to achieve some really cool stuff in your games programming endeavors.