Learniverse

The Mathematics of Games Programming

00:00

Hello, there's no two ways about it.

00:03

If you want to get started in games programming, you're going to need to know some mathematics.

00:08

But it's not all doom and gloom.

00:10

As I have demonstrated many times on this channel, you can achieve some really cool stuff

00:14

by just understanding a core set of mathematical functions and operations.

00:20

The recent videos on the channel this year have been a little bit more complicated than

00:23

usual, and this is prompted quite a few of you to ask the question, what maths do I need

00:27

to know in order to be a games programmer?

00:30

Well I hope this video goes some way to answering your question.

00:34

So I'm going to start with something that's really fundamental and that's Pythagoras theorem.

00:39

And as games developers, we'll be using this all over the place.

00:44

And it works with right-angled triangles.

00:47

Now a right-angled triangle means that one of the angles is 90 degrees.

00:51

And that's really important and we'll see this a lot in this video.

00:55

If I label the sides of the triangle in the following way, so here we've got A and B and

01:00

C, Pythagoras theorem links together the lengths of these three sides.

01:05

And there's an easy way to remember this relationship.

01:08

It's as follows.

01:09

We have C squared equals A squared plus B squared, which means if we know the length of A and

01:16

we know the length of B, we can calculate the length of C, which is known as the hypotenuse.

01:22

Some simple algebra, we can rearrange this to be C is equal to the square root of A squared

01:29

plus B squared, calculating the length of C is quite an important thing in game development.

01:35

Let's for example assume we're working on a two-dimensional game and we represent our

01:40

screen as a per of x and y axes, so here I've got x and here I've got y.

01:47

This of course then is our origin 00.

01:50

Now let's say I have two points on my screen and I wanted to calculate the distance

01:56

between the two points.

01:58

One of these points for example could be the player and the other point could be the

02:03

enemy.

02:04

Perhaps when the player is close enough to the enemy, the enemy's response changes.

02:08

So knowing this distance becomes a critical form of gameplay manipulation, but all we have

02:14

are the two points, so here we've got p1x and p1y and here we've got p2x and p2y.

02:27

If we form a right-angled triangle out of those two points then we could say that B

02:34

is equal to p2y take p1y, it gives us the difference between the two points and likewise

02:42

we could say that A is equal to p2x take p1x which gives us the distance in the x axis.

02:51

Therefore we can substitute into our formula to determine the length C. C equals the square

02:59

root of, it's going to be a long one, p2x take p1x squared plus p2y take p1y also squared.

03:10

In principle it doesn't matter whether we do p2 take p1 or p1 take p2, all C is the distance

03:17

between the two points and that distance, well it's just a fixed scale of value.

03:23

So whether we look at it from the player's perspective or from the enemy's perspective

03:27

doesn't make any difference.

03:29

Once we know this distance we can make assumptions, so for example if C is less than

03:34

or equal to 50, that might be some unit in our game space, then attack.

03:40

Whenever we're working with two-dimensional games we'll always be working with an x and y axis

03:45

like this and it's important that the x and y axis are at 90 degrees to each other.

03:50

And this is enforced by the layout of the screen and the layout of our arrays in memory

03:55

so it's very natural to think of things in a two-dimensional system such as this.

04:00

And even though we didn't specify a triangle explicitly we could derive a triangle just

04:06

from these coordinates in the 2D space and this is a principle which we'll keep coming

04:11

back to time and time again throughout this video.

04:14

If you're going straight into three-dimensional games or four-dimensional games or 27th

04:19

dimensional games you can simply add more terms to this as required.

04:25

Where each term represents a distance along one of your world's axes.

04:30

The next area of essential mathematics that you'll need to know about for game development

04:34

is vectors and I do not mean standard vector.

04:38

This is not what I'm talking about, they are not the same thing.

04:43

Let's take an axis again and somewhere on my axis I have a point.

04:47

We know that the coordinates of that point is defined by its location along both of those

04:53

axes.

04:54

So here will be its x coordinate and here will be its y coordinate and we can happily

04:58

represent this position, let's say it's p as being x, y.

05:04

On the one hand this point is just a point in 2D space but we can also consider it as

05:10

being a distance travelled from the origin.

05:14

So starting at 0, 0 and going to our point p.

05:19

If we always assume it starts from the origin we never need to include that anywhere.

05:24

So the only information we need is the location of the point.

05:27

When we think about things in this way what we have described is a vector and vectors

05:33

are quite important because not only do they describe a point in space but they also

05:38

describe a direction.

05:40

Our vector can point in any direction and be any length long.

05:47

When we want to describe a vector we usually see a little arrow above the letter we're

05:52

using to describe it and it's convention to use a column of parameters to specify the

05:58

point part of the vector.

06:01

In this case we know that our point was at x and y so in the literature if you start

06:05

to see this notation you know you're going to be working with vectors.

06:09

The key thing to remember is a vector has a length and a direction.

06:14

Now how do you think we calculate the length of a vector?

06:17

Well hopefully it's already obvious that what we have created here is a right angle triangle.

06:23

And if we know these two sides and we do this one is y and this one is x then we can

06:28

use Pythagoras theorem to calculate the length of this vector.

06:32

The length of a vector is also known as its magnitude so here is our vector and usually

06:37

you would put two bars either side of the symbol to represent the magnitude or the length

06:43

of the vector.

06:44

And we know now that that is simply Pythagoras' theorem.

06:48

Vectors can be added and subtracted.

06:50

So let's say I get to this point and I then use another vector to define another point.

06:56

And let's say I do it again this time we have a vector quite a long one going this way

07:00

and one more time why not?

07:03

Big long vector going over here.

07:05

Now label my vectors b1, b2, b3 and b4.

07:11

We always assume that a vector in isolation starts at the position 0 0 and when we add

07:18

vectors together we assume there's a little 0 0 just here and then there'll be another

07:24

little 0 0 just here and up here.

07:28

Adding vectors is very simple.

07:30

If we wanted to work out the location of this final point over here we can add up all

07:37

of the vectors on this path to give us that point and therefore we can say that p is equal

07:44

to v1 plus v2 plus v3 plus v4.

07:50

At the vectors, don't forget these will each have their own sets of x and y's.

07:55

We add together the common components of each vector.

07:59

So we come as x plus x plus x and y plus y plus y plus y which will give us the position

08:09

of our point as a vector and this holds true because this is still it's a point in 2D space

08:15

but it is still a vector starting from the origin.

08:21

So let's assume we have a player character which is walking along a path and it visits

08:27

all of these points.

08:29

We can describe in 2 dimensions that path quite nicely with vectors.

08:33

We can also sum the lengths of each vector to see how far the player is walked by working

08:39

out the magnitude of each vector as we go along and after we've walked along that path

08:45

because we've got a resultant vector p we can work out the length of that vector which

08:50

will tell us how far we are from our starting position.

08:53

The important thing to remember about vectors is they are a length and a direction but what

08:59

do we do if we don't care about the length of the vector and really all we're interested

09:05

in is the direction.

09:07

So we're going to lose this scalar value of the vector.

09:11

Well there's no such thing as removing the length, the best we can do is assume that

09:15

the length is 1 and vectors that have a length of 1 are a unique type of vector that

09:21

are called a unit vector and we'll see later in this video that there's quite a few

09:25

properties of unit vectors we can exploit to our advantage.

09:28

The nice thing is any normal vector can be converted to a unit vector through a process

09:34

called normalizing, fundamentally our vector is a right angle triangle and I've already

09:39

shown that the magnitude or length of a vector is calculated by Pythagoras' theorem.

09:46

If we assume the original length of our vector was 10 but we want to represent it as

09:52

a unit vector its length must become 1.

09:55

The direction remains the same but this is of course changed our x and y values.

10:01

They've become smaller and due to the proportionality effects of right angled triangles

10:07

this is in fact become x divided by 10 and this has become y divided by 10.

10:13

They've become 1 tenth of their original size.

10:16

The hypotenuse will have become 1 tenth of the original size and therefore the sides

10:21

of the triangle must also reduce by a factor of 10.

10:24

This means if we can calculate the length of our original vector and divide that vector's

10:29

components by the length we can establish the unit vector which has a length of 1.

10:35

The notation for a unit vector has a little hat over the top or a shepron and therefore

10:40

we can also imply now although you won't see this written out very often that the unit

10:44

vector consists of components x divided by the square root of x squared plus y squared

10:51

and y divided by exactly the same thing and therefore by taking our vector dividing

10:58

its individual components by the length of that vector we end up with our unit vector.

11:03

This has a magnitude or a length of 1 and of course it still has direction and it's

11:08

this direction that we're interested in.

11:10

If we always assume the length is 1 all that's left is the direction the only interesting

11:14

feature of this vector.

11:16

Unit vectors have many uses but perhaps the most obvious one is that they can be scaled

11:22

very simply.

11:23

Let's say I wanted to find a point along the direction of my vector but that point must

11:29

be and let's just pick a number for the sake of argument, 8.3 in length away from the

11:34

origin.

11:35

Instead of doing any complicated calculations we can simply multiply our unit vector by 8.3

11:43

because multiplying 1 by 8.3 is equal to well 8.3 and the direction is still the same.

11:51

So this point here we could define as being p equals our unit vector v multiplied or scaled

12:01

by 8.3.

12:03

Unit vectors in games are great for specifying direction.

12:07

So here I've got a crude car and if I assume that the centre of the car is lying at

12:12

the origin I can represent the direction the car is travelling in as a unit vector.

12:18

The distance that the car travels I can just represent as well the distance.

12:23

A scalar value I don't need to know the direction.

12:26

The direction is included in this unit vector the distance is what will scale the unit

12:32

vector by to work out where the car is in the future.

12:36

So we know that the distance will lie anywhere along that direction vector and to work

12:42

out where the car is in the future we just scale our unit direction vector by the

12:47

distance that we're interested in.

12:51

I think fundamentally vectors are a great way of navigating space so in 2d we'll use 2d vectors

12:58

like I have here but the same applies to 3d as well.

13:02

The next area I want to cover is angles.

13:04

This is very basic trigonometry and when working with angles it's useful to just memorize

13:10

a few formulae.

13:12

We're already now quite familiar with a right angle triangle it can represent.

13:16

The distance between an enemy and a player and it can represent a vector.

13:20

The important thing is this angle here is 90 degrees but what if we wanted to know what

13:26

this angle was?

13:27

This is the symbol theta commonly used to represent angle and as usual we've got our x-axis

13:33

and our y-axis.

13:35

When we look at the triangle from the perspective of this angle here we can label the sides.

13:41

The longest side of a right angle triangle is always called the hypotenuse.

13:45

In this configuration over here the y-axis will be called the opposite side and the

13:51

x is called the adjacent.

13:53

So we're looking at the triangle from this corner and the rule I want us to learn here

13:58

is that the tangent function you'll see that on your scientific calculators of theta

14:03

is equal to opposite divided by adjacent.

14:08

It's a ratio and therefore we can also assume that theta is equal to the inverse tangent

14:15

with tan with a little minus one of the same thing and just to keep the writing down

14:20

I'm going to use y over x and so that's simply not.

14:23

A hypotenuse could be a vector we know how to calculate the length of a vector now and

14:27

we know its direction and we know how to break it into its x and y components we can work

14:32

out the angle of that vector relative to the x-axis.

14:36

Now just a little side note in a lot of programming languages we don't see tan to the

14:42

minus one instead what you'll see is a tan y divided by x but whenever you see a divide

14:49

over potentially a variable you don't have control of we need to be sure that this x

14:54

isn't equal to zero and well why is this we know that the internal angles of the triangle

15:01

must add up to 180 degrees and if we draw a triangle where the length x is equal to zero

15:09

well we don't have a triangle at all we have just a line going up and this means

15:14

this division can start to yield problems with our program it could crash it could make

15:19

the numbers invalid it's better if we can perform this calculation with a degree of safety

15:24

and so again a lot of languages provide a secondary tangent function often called a tan

15:31

two which takes as arguments y and x and this function performs the necessary safety

15:37

checks to make sure we get an answer that we might expect so if we were looking for the

15:42

angle of a point away from the origin in this instance we would get theta and as the angle

15:48

moves around towards y we would hope to get here 90 degrees and the a tan two function

15:55

will make sure that we get the result that we expect I should also point out at this

15:59

time that most programming languages don't work directly in degrees instead they work

16:04

in radians and convention is to always specify these angles in radians or degrees relative

16:11

to the x axis and as we go around we start to form a circle and a circle starting here

16:18

is zero degrees or 360 degrees they're the same thing and over here we would have 180 degrees

16:26

90 and 270 while degrees are great for humans to understand they're a bit enough for

16:32

mathematics to understand and instead maths prefers pi don't we all so in radians zero

16:39

degrees is zero radians 180 degrees is pi and a full circle is 2 pi 2 times 180 our 90 degrees

16:50

is pi over 2 or if we look at that in relation to the entire circle it's 2 pi over 4 it's

16:58

the first quarter of the whole 2 pi 180 would be 2 pi over 2 it's the first 4 half 3 quarters

17:08

of the way around would therefore give us 2 pi times 3 quarters which is 6 pi over 4 which

17:16

is 3 pi over 2 it's always a bit awkward that one but what we can see is there is a linear

17:22

relationship between degrees and radians and we can define that as being radians equals

17:29

degrees divided by 180 times pi and naturally it follows that degrees therefore our radians

17:38

divided by pi times 180 it takes some getting used to as humans we like to think in terms

17:45

of degrees but the functions in our computer programs they like to think in terms of radians

17:50

with a bit of practice and experience eventually humans start to think in radians 2 but

17:55

I wanted to highlight this in case you start using some of the trigonometric functions

18:00

and you're not quite getting the results you expect now I started this section by saying

18:04

there are things you just need to memorize and one of those things was that the tangent

18:09

of theta is equal to the opposite over adjacent and I like to remember that as toa tangent

18:16

of theta is equal to the opposite divided by the adjacent toa is one third of this phrase

18:22

so care toa and depending on what information we have to hand decides which part of this

18:29

phrase we use for calculation so when we knew the opposite side and the adjacent side we

18:36

can work out the missing angle with the tangent function if we know the adjacent and the

18:41

hypotenuse then we can work out theta with the cosine function and if all that we have

18:48

is the length of the opposite side and our length of the hypotenuse then we can work

18:52

out theta with the sine function and writing it out like this makes it a very easy thing

18:58

to remember so care toa now I've just introduced the sine and cosine functions so let's

19:04

have a think about those sine and cosine are periodic functions why they do what they do

19:09

is well beyond the scope of this video but they do have some useful properties here I have

19:14

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

19:21

going to do my very best to accurately plot y equals sine of theta it goes up it comes down and it goes

19:34

up again and it'll keep doing that forever and ever and ever and ever and it goes between

19:41

plus 1 and minus 1 do you know what just for clarity I'll write these in in the colours so red was

19:49

sine theta in green on the same axis I'm going to plot cos theta and cos theta looks like this

20:04

it's not bad and again carries on doing its thing forever and ever and ever now you're thinking why

20:10

sine and cosine important let's plot a second axis y and x at this point here we've done a full

20:18

revolution so this would be where the angle is 360 degrees or of course 2 pi in radians so let's

20:26

have a look where x and y lie when we calculate x and y using sine and cos and I'm going to use

20:33

y is equal to sine and x is equal to cosine starting with the theta of 0 and looking at the green line

20:41

which is our cosine in x we can see that our x coordinate is plus 1 and if we look at the red line

20:47

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

20:54

other locations on this plot of sine and cosine so let's look here where it's crossed 0 in the y axis

21:00

this also happens to coincide with the peak of the sine wave here my x is now 0 but my y

21:07

is plus 1 0 comma 1 look at the next interesting feature which is here where the sine wave crosses

21:13

the x axis also happens to be the lower peak of the cosine wave which gives me an x of minus 1

21:22

and a y of 0 and no prizes for guessing if we look at this last crossing point we see that x which

21:28

is cosine is 0 and our y is negative 1 now I've picked out points here that are easy to draw and

21:34

represent on this axis but if I picked every single point along this waveform absolutely every

21:40

single one what we would find is it plots a perfect circle as we increased theta and this circle

21:52

always has a radius equal to 1 it's a unit circle and if we think back to our vectors where we

21:59

had a unit vector it looked very similar it was a vector pointing in a direction with a length of

22:04

1 so given a specific theta we can calculate a point in x and y which also happens to be a unit

22:12

vector and this is quite important because just having a theta value alone we're able to describe

22:18

a unit vector and we know that we can scale a unit vector which will give us any point in our 2d

22:24

space this is also known as a polar coordinate we're given any theta and any radial length we can

22:31

describe any point in 2d space so why is this important to game developers well in a similar way

22:37

to our car example before let's assume this time we have a spaceship but all we know about this

22:44

spaceship is what angle it is relative to the x axis we now know how to turn this angle

22:51

into a direction vector for the spaceship to travel along and it's also now very easy to rotate

22:58

the spaceship because all we need to do is increase or decrease the angle so knowing about

23:03

sine cosine and tangent we can easily swap between using vectors and using angles and it depends

23:11

on the gameplay scenario you are trying to invent as to which approach you might prefer to use

23:16

rotating things is certainly easier to think about when working with angles whereas moving things

23:22

along paths and in certain directions is much easier to think about using vectors so being able

23:27

to swap between the two when necessary is a requirement for most aspects of game development

23:33

however there are some things to be aware of sine cosine tangent and square roots

23:40

can all be considered quite computationally intensive i.e. they require a lot of CPU cycles

23:47

in order to give you an accurate result however what we've seen so far with vectors other than

23:52

the square root function vectors are trivial for a computer to compute so we're possible

23:58

we prefer to work in the domain of vectors instead of angles and this is where another important

24:04

mathematical concept which is essential for game devs is the concept of the dot product which

24:10

amongst many things allows us to think about angles in terms of vectors here i have a space

24:17

with two vectors i'm going to call this one v1 and this one v2 and i'm interested in trying

24:25

to work out the angle theta between these two vectors well let's look at doing this the long way

24:31

round first i can break my vector down into a right angle triangle to give me an x and a y

24:38

component and i can use inverse tangent to give me an angle relative to the x axis i can do exactly

24:45

the same for my other vector give me a big theta this time and it should be intuitively obvious

24:52

that the red theta is equal to the orange theta minus the green theta so we can express that

24:58

the angle between the two vectors is going to be equal to the inverse tangent of v2 y over v2

25:07

x minus the inverse tangent of v1 y over v1 x but here we've got lots of things to worry about

25:16

firstly we've got this divide condition as come back that's okay you can use the a tan two

25:21

function then we've got these computationally expensive functions to use anyway these inverse

25:26

tangents but we'll get the result that we want eventually there's a slightly different way

25:30

to think about solving this problem recall that vectors contain direction and the length but the

25:36

length is largely irrelevant in this situation it doesn't matter how long these vectors are

25:41

the angle between them isn't going to change so let's represent these vectors as unit vectors

25:47

the dot product is defined as being the sum of the product of the individual components of

25:54

the vectors so in this instance here we might see v1 x multiplied by v2 x plus v1 y multiplied by v2

26:08

y this is a scalar result it doesn't give a vector as a result just a single number and let's

26:14

have a look why for the time being I'm going to assume that our x axis is also a unit vector

26:21

so here we've got one zero so if I were to take the dot product between v1 and my x axis

26:28

I would have v1 x multiplied by one plus v1 y multiplied by zero therefore the dot product in this

26:37

instance is simply the same as the x component of the vector what we've calculated is how much

26:44

does our v1 vector project onto the x axis and by project what you can think of

26:51

is if there was a light source casting down onto our x axis this line here would be where

26:57

the shadow stops and this line will always be at 90 degrees to this vector which represents our

27:05

x axis notice and not uncow incidentally we've created a right angle triangle and in this right

27:12

angle triangle we happen to know the adjacent we happen to know the hypotenuse and what we're

27:20

trying to calculate is the theta so if we go back to the cat part of soccer toa we can see that

27:30

cos theta is equal to our dot product result divided by our hypotenuse but our hypotenuse

27:39

was a unit vector so we know it's length it's one therefore our theta is simply the inverse

27:46

cosine of our dot product we got this optimization because we moved to unit vectors

27:53

if we didn't move to unit vectors we could still get the correct result but we would have to

27:58

use the proper length of the hypotenuse so we would have to use Pythagoras' theorem to get that

28:03

value but this is one of the reasons I wanted to emphasize the importance of unit vectors

28:09

they can save us a lot of computation so now we've worked out the angle of one of our vectors

28:15

relative to the x axis we could go and do the same for our original v2 calculate how much it

28:21

projects onto the x axis and perform a similar equation to help here where we look at the

28:26

difference between those two results however there is no reason at all for us to project directly

28:33

onto an axis our dot product will allow us to project one vector onto another so assuming we want

28:40

to project v2 onto v1 we want to see at what point does the shadow extend to a long v1

28:51

providing we've got some light source that is perpendicular to v1 and this is precisely

28:58

what the dot product calculation calculates doesn't matter whether it's the x axis or another vector

29:03

and so assuming we're working with unit vectors we can calculate that angle as simply being

29:09

to the minus one of the dot product between the two vectors and that's usually represented literally

29:15

by a dot but I emphasize this is for unit vectors if you don't have unit vectors you're going to

29:22

have to calculate the magnitude's first calculating the angle is all well and good but we've got

29:28

this nasty inverse cosine function to use now if you do need the angle there's no two ways

29:34

you've got to do this function to convert this ratio into a meaningful angle there are intuitively

29:41

other uses for the dot product scalar value that we get as a result of this calculation consider

29:47

the following here I've got my x axis which I'm going to describe as a unit vector and here

29:53

I've got another vector which is also a unit vector I'm going to calculate the dot product

30:00

between these two unit vectors so we've got v dot large x in this case which are both unit

30:06

vectors we'll calculate the dot product so that's 1 times 1 plus 0 times 0 equals 1 now let's

30:17

consider our unit vector traveling along this y axis I'm still going to take the dot product

30:23

with the x axis unit vector dot x is now 0 multiplied by 1 plus 1 multiplied by 0 equals 0 let's

30:35

take a look at a final vector again a unit vector in this instance I've got minus 1 times 1 plus

30:44

0 times 0 equals minus 1 what we've seen is as we travel around like this we've gone from 1

30:52

through to 0 through to minus 1 I like to think of this result as how similar are two unit

30:59

vectors when they are the same we get the result 1 when they are perpendicular to each other at

31:05

90 degrees we get 0 and when they are in opposite directions we get minus 1 and so if I were to have

31:13

two vectors like this they're quite similar I'd expect my dot product between those two vectors

31:19

to be somewhere near 1 if I have two vectors like this well they're not very similar

31:26

so I'd be expecting the dp to be somewhere around 0 and if I have two vectors like this well they're

31:33

not similar at all in fact they're quite opposite so I'd be expecting the dp to tend towards

31:39

minus 1 and I find identifying the similarity between vectors to be a very important thing indeed

31:47

let's consider a small section of racing track and on that racing track we've got some sort

31:54

of finish line marker and I have a car driving along the track when the car passes this marker

32:00

I want to increase the number of laps I know the direction vector of my car and for this region

32:07

of the track I've specified a line that we want to cross but I've also specified a direction

32:14

that that line must be crossed at in fact it's 90 degrees as the game is being played

32:21

at some point the car crosses that line and we detect it and at that point of detection I can take

32:28

the dot product between the direction of the track of the direction of my car and I know that if my

32:36

dot product result between those two vectors is greater than 0 then I have crossed the finish line

32:42

the right way round so I can increase my lap counter however if the direction vector of my car

32:48

dot product with the track direction is less than 0 I know that they're not similar at all

32:54

and in fact I must have crossed the finish line in completely the wrong direction so maybe I want

33:00

to decrease the number of laps let's consider a slightly different scenario here I have some terrain

33:06

for a small simple platform game the character has a direction vector and these segments of terrain

33:12

have what's known as a normal vector it's at 90 degrees as my character is traveling along the

33:18

terrain I can take the dot product between the two vectors here we can see that they are 90 degrees

33:24

to each other so the dot product is going to be 0 and the same applies down here but as the

33:29

character gets onto the hill the direction vectors become far more similar they tend towards one

33:35

so I could use this number to influence the speed of my character as it's going down the hill

33:40

it's traveling faster let's look at it from the other side this time my character is trying

33:45

to go up the hill well now we see that we've got direction vectors which are going in opposite

33:51

directions they're not similar at all and so in this instance the dot product is going to tend

33:56

towards minus one so I could use this information to really slow down my character as he struggles

34:02

to climb up the hill these two examples I've shown they're quite trivial but knowing the similarity

34:08

between vectors and in this instance the similarity between direction components of vectors

34:13

has a lot of utility in game development especially when it comes to things like collision detection

34:19

and physics and we're able to calculate these reasonably complicated relationships

34:25

without falling back onto any computationally intensive functions at no point during these

34:30

calculations do we actually need to know the angle of anything we're just looking at the

34:34

relationship between the two vectors which is expressed as a scalar value the dot product

34:40

though please remember everything I've shown assumes that the vectors you're using are unit

34:44

vectors the dot product can start to mean different things when you're doing the dot product

34:49

between non-unit vectors and I think that's a little bit beyond the scope of this video

34:54

okay another absolute mathematical essential is linear interpolation this is also known as

35:01

lirp for short or lirping let's assume I have two points in space p1 and p2 and I know that I

35:10

want to travel from p1 to p2 in precisely five seconds I want to travel in this direction along

35:18

that line and I wanted to take me five seconds to do so this means that every frame we're rendering

35:24

the game we're going to want to render a slightly different location somewhere along this line

35:30

now it's quite useful to think about this in terms of percentages so when we're at the start

35:35

this is going to be zero percent and when we get to the end that's a hundred percent halfway along

35:40

that line therefore is 50 percent so let's start by trying to understand where 50 percent along that

35:47

line is and we'll call that point point t well let's break this down into yep you've guessed it

35:55

a right angle triangle and we will work out where the point t is in the x axis and the y axis

36:02

separately so for point t in the x axis we know at zero percent we must start at p1 so it can't be

36:10

any less than p1 we also know where the location is at one hundred percent in the x axis

36:17

since we know the ending location in the x axis and the starting location in the x axis

36:23

we can work out the distance traveled in the x axis and 50 percent must be halfway along

36:29

that distance simply due to this proportionality of right angle triangles that we talked about

36:35

earlier so I can take my final point x work out the distance from the starting point and multiply

36:42

that by t which is my percentage of course in a computer we wouldn't represent 50 percent

36:48

we'd represent this as 0.51 and 0 exactly the same calculation can be done for y and so by

36:57

specifying a value for parameter t between 0 and 1 we can get any location along our line we have

37:06

interpolated linearly in a straight line between points p1 and p2 now let's assume that per frame

37:14

0.1 seconds of time passes and since the start of the motion I'm accumulating this time

37:23

in capital T I wanted the whole motion to take 5 seconds and therefore I'll express t over 5

37:31

as being the percentage that controls my parameter little t in my linear interpolation equations

37:39

I've taken the rather odd approach of introducing linear interpolation in two axes

37:44

the number of axes is completely irrelevant linear interpolation works with scalar values vectors

37:50

multi-dimensional variables it doesn't matter the fundamental principle is this that given two values

37:57

we can find another value in between them at any point and t equals n starting point

38:05

or starting value plus n ending value maximum value minus the starting value multiplied by

38:14

our percentage 100 percent will give us the end 0 percent will give us the start and it doesn't

38:20

actually have to be bound between 0 and 1 we could continue extrapolating along that line

38:26

and linear interpolation has uses all over game development not only for controlled

38:31

motion such as this so imagine we had a sequence of vectors and we wanted to linear interpolate

38:37

along each vector per frame so we got enemies that follow a path or perhaps we wanted to

38:43

fade between different colors we could use linear interpolation in the red, green and blue

38:49

spatial domain a slightly more advanced use of linear interpolation is to implement functions

38:54

that we can't define mathematically let's say I've some curve that looks like this

38:59

deriving the function for that curve would be quite a challenging thing to do but our game requires

39:05

that we follow a trend of y equals f of x if we can't define such a function easily we can

39:12

approximate it with known points that we wanted to provide these could be handcrafted so now when

39:18

we're given a specific x value and we want to look up in this function what its y value could

39:24

be we could just take the x value and round it to our nearest point which would end up giving us

39:29

something like this the problem here is our approximation to the function isn't very smooth

39:34

a better approach would be to given any arbitrary x value in this space work out the two neighboring

39:42

points that we do know and linearly interpolate between them it doesn't give us a perfect curvature

39:48

but it does remove the step like nature of just using x on its own so linear interpolation for

39:54

approximating functions can save you the headache of deriving the function in the first place

40:00

and it's also very useful in things like audio applications and sound if we assume that these were

40:05

sound samples we've taken a very crude approximation of the sound which might end up sounding

40:11

quite robotic or distorted and instead we can access any value on that curve as any spatial

40:17

resolution of x and we linearly interpolate between the points that we know to smooth out all of

40:23

of these rough edges we've just looked at how linear interpolation could be used to make an

40:28

enemy or a player follow a particular path but what happens if we don't know that path in advance

40:35

for example we have a player character running along and at some point the player presses the jump

40:41

button which causes the character to jump we haven't predefined a path in advance that we want

40:47

the character to follow but also the path is non trivial it's got curvature because the world

40:52

in our platform game has gravity we can use some very simple kinematics and bring together

40:57

everything we've seen in the video so far in order to solve this problem and you might think that

41:02

sounds absolutely horrific but I think also there's a certain elegance to it when you actually see

41:07

its simplicity we can represent our players position in space as a point and we know that a point

41:14

is also the same as a vector as the game is progressing time is moving forwards and time is an

41:20

important part of motion you may remember that speed equals distance over time and if you're not

41:27

familiar with this well think of it in terms of miles or kilometers per hour or meters per second

41:33

and so if we happen to know the speed that we're moving at we can rearrange this to tell us how

41:38

far we've traveled in a particular time it's simply speed times time now speed on its own is a

41:45

scalar value but we've been working with vectors so when you actually have speed in a direction

41:51

that's known as velocity so our player should also have a velocity vector in games we typically

41:57

chop up time into the amount of time that's passed per frame this can be known as delta time or

42:04

elapsed time and so taking a character with a given velocity vector how do we calculate where it

42:10

should be on the next frame well this is simply a matter of taking the players current position

42:16

and adding to it the distance traveled given the speed and the time now our speed is not scalar

42:24

it's a velocity vector it includes direction but we're only going to travel a certain distance

42:30

because we've got a cap on how much time we're traveling in that distance for this means we can

42:35

argue that the position of the player on the next frame is equal to the position of the player

42:40

on the current frame plus the velocity of the player on the current frame multiplied by our delta

42:47

time the elapsed time between frames so with respect to time velocity changes position the problem

42:55

is if our player has a velocity vector they're just going to keep on moving in that direction

43:01

nothing is ever going to stop them yes when the user presses the jump button at a particular

43:06

location we can change the direction of our velocity vector and potentially it's magnitude to

43:13

change the position of the player in time we can get this jump but that's going to happen

43:18

in a straight line because nothing ever changes the velocity well that's not true is it because

43:24

we know that we won't just keep jumping in a straight line all the way across the universe

43:28

at some point we do actually have to come back down to earth and there is a force that does that

43:33

called gravity and gravity is an acceleration and accelerations change velocity so i'm going to

43:40

use exactly the same type of formula but this time i'm going to change my velocity on the next

43:45

frame is equal to my velocity on the current frame plus an acceleration multiplied by our delta time

43:53

i can keep acceleration fixed so for gravity for example it might be something like

43:59

there's no change in the x-axis but it pushes us downwards in the y-axis we can use acceleration

44:06

to change velocity and we can use velocity to change position and this is a very nice system

44:11

because at no point do we need to know in advance where the player is going to be so whereas before

44:17

we were linearly interpolating along vectors between points now we can let the system just

44:23

update itself and the character will move in accordance to the motion that we've described

44:28

by our acceleration and velocity vectors i wanted to include this at the end simply because it

44:34

does bring everything together we're now working with vectors and we might be interested in

44:39

changing the directions of vectors based upon the landscape or the orientation or angle of the

44:45

player and now we're equipped with all of the tools to convert between vectors and angles

44:50

and understand how to work out where we are a longer vector with respect to time

44:56

this video has been a brief introduction to the essential mathematics that i believe aspiring

45:01

game developers need in order to get the most out of the crap and fundamentally i've only talked

45:07

about angles vectors and motion but we've seen throughout the course of this video that actually

45:13

all of these are related in quite simplistic ways and that's understanding the relationships between

45:19

these things allows you to make better decisions as a game developer it's up to you to decide

45:25

what to use and when to use it but don't make life difficult for yourself by making a decision at

45:31

the start i'm only going to use angles for things or i'm only going to use vectors for things

45:36

this video should have equipped you with some of the basic tools to translate between them

45:41

another way to look at this if you did want to explore things a bit further is angles is all

45:46

about trigonometry and vectors is really all about geometry motion and i know we've only looked at

45:53

it very simply comes under kinematics or indeed plain old physics those of you that have been

45:59

game developing already for some time or i've watched a lot of the videos on my channel may

46:04

notice there's one important thing missing from here and i'm not going to talk about this video

46:09

because i've covered it in lots of other videos on this channel but that would be matrices

46:14

or linear algebra all of these things combined together give you the ability to manipulate space

46:21

whether it's two dimensions or three dimensions or more and manipulating space or manipulating things

46:27

in space is fundamentally all game development is well perhaps i shouldn't say all game development

46:33

but most conventional game development that involves characters action movement that sort of thing

46:40

and i hope this video is provided enough confidence for people that may not have the strongest

46:44

maths that you don't necessarily need to know advanced maths in order to achieve

46:49

quite cool things in game development maths certainly helps i can't deny that but you don't need

46:55

much in order to do lots of fun stuff if you found this video useful a big thumbs up please

47:01

have a think about subscribing and i'll see you next time take care

00:00

Introduction to Mathematics for Game Programming

00:33

Pythagoras' Theorem Overview

01:35

Calculating Distance with Pythagoras' Theorem

04:30

Understanding Vectors

06:30

Magnitude of Vectors

12:07

Introduction to Coordinate Systems

13:04

Understanding Cartesian Coordinates

14:36

Angle Calculation with Tangent Functions

14:39

Working with Polar Coordinates

16:05

Applications of Coordinate Systems in Games

18:04

Transformations in Game Development

19:04

Understanding Sine and Cosine Functions

21:53

Unit Circle and Vectors

24:04

Dot Product and Vector Similarity

00:33

What is Pythagoras' theorem and why is it important for games?

01:54

How do we calculate the distance between two points in 2D space?

04:38

What are vectors and how do they describe points in both distance and direction?

09:06

What is a unit vector and how can it be applied in game development?

12:07

How can coordinate systems impact game navigation and mechanics?

13:04

What are the differences between Cartesian and Polar coordinates?

18:04

What transformations can be used in games to enhance movement?

16:05

Why are coordinate systems crucial for rendering graphics in games?

14:36

Why is the tangent function crucial for angle calculations in games?

19:04

How do sine and cosine functions help in game physics and navigation?

24:04

What is the importance of the dot product in understanding vector relationships?


AnimationMathematicsTrigonometryGame physicsAlgorithmComputer sciencePythagorean theoremSpatial abilityVector (mathematics and physics)Polar coordinate system2D computer graphicsDot productVideo game programming

Description

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.