Why Is Math Important When It Comes To Game Programming?
Math is an integral part of game programming. As a matter of fact, games are all about math. For example, if
your game has gravity, your game is using math.
On the surface it’s a simplistic game, and full of math.
Let’s take a look at the bird in flappy birds.
The bird has 3 different things going on with it:
A force moving upwards when you tap the iPhone screen
A force moving downwards constantly (gravity)
The birds y position moving up or down
The pipe has things going on with it as well:
The pipes x position moving
A constant speed moving in one direction
What is a game world?
A game world is an artificial universe, an imaginary place in which the events of the game occur.
The most important thing is how do we describe where game objects should be in our game world?
The answer is simple, we define our game objects position in the game world through the x and y coordinate system
(also referred to as the cartesian coordinate system).
The position of our game object in the cartesian coordinate system is how we describe where our game objects are on the “game world”.
What does the cartesian system look like in Godot Game Engine?
In game programming, programmers use the term transform or transforming to describe changing a game objects
position, scale (size), and even rotation orientation.
Transcript
Hello and welcome to another episode in the Godot basics tutorial series. In this episode we will be taking a brief look at the Cartesian graph and transforms in-game. Programming. Before we look into that. However I want to go over. About the topic of math and why it's important. So in the script fundamental tutorial series I mentioned that you should look into math and now we will be taking a look at why we should be taking a look at math. Now math is an integral part of game programming.
On top of many things the most common thing you may want to do is simulate physics. Simulate. Interactions between different game objects in your game world. And to do that we would have to use math as a matter of fact. Math. Is what defines the behavior of our game objects and we could do this would be our nodes. Let's go ahead and take a look at this Flappy Bird game. Now I'm gonna play the video and while it plays I want you to note down some interactions and behaviors we could use math to describe
Things you can see here or game object the bird is moving up and down. On top of that are. Pipes are moving in one direction. Now let's go ahead and take a brief look at our math which I put in quotations and the behavior of our game objects in our game world. So for the bird just by looking at the video we notice that we have a force going up where we have a force going downwards. Again on our. Bird. And lastly you'll notice that our position is only changeable on the y axis.
You'll notice that the bird doesn't change in the x axis on top of that we have the behavior of our pipes or pipes position changes on the x axis. It does not change on the y axis. And on top of that you'll notice that the pipes are moving at a constant. Speed. There are other things going on such as the background moving. You could even use math to describe how our pipes are positioned before the end to the game scene. However the point of this interest to explain that math is what we use when we want to describe certain functionalities of our game objects or rather certain behaviors of our game objects.
Now. I mentioned the game world. And so we'll take a quick look at what exactly a game world is and a game world is an artificial universe an imaginary place in which events. Of the game occur. Do not confuse the game world with for example the. Camera scene. Those are two different things. The camera scene or the camera view is what describes what the player sees over what the player sees is just a small portion of the game world. The game world can be as big or as small as you'd like it to be.
Now the most important thing is how exactly do we describe where our game objects should be in the game world. Well. We use positions. And that's how we describe. Where objects are in the game world. As a matter of fact position is a very basic concept. You should learn about when you want to. Enter game programming. Now let's take a look at that. This is a basic Cartesian graph as you can see we have a Y axis.
Notice how our y axis as is Goes up. We are reaching positive infinity. Notice How we have an x axis. And notice how to the right We are trying to reach positive infinity on the x axis and we have our game object which has a coordinate which can be described as the following an x. Position value. And a y position value. And on top of that we also have an origin. This describes the center of our Cartesian graph.
Now in the game world the center describes in most cases where. Our game world starts from. We call this the origin. Now different. Game engines describe the Cartesian graph differently. For example this is the Cartesian graph. Unity uses however this is not what good dough uses and we'll take a look at that in a moment. Moving on. We can describe. Our graphs and divide them into four quadrants. We have Crutcher in. One quadrant two Quadrant Three in Quadrant Four the most important thing I wanted to take note of this quadrant one as a matter of fact oh game programmers want
To program in Quadrant 1 because it's the easiest to manage in terms of using math formulas as you can see here we have positive X and positive y. And. Having positive X and positive y is a good thing you want that. You want your game world to run around in the Quadrant one area. I'm not saying you can't do it for the other quadrants however this is the most convenient. Now let's take a look at Cartesian graphing computers and most computer software.
The Cartesian graph Has the y axis inverted. That means, That positive y goes down and negative y goes up. And the reason for this is basically a history lesson in computers when they were first made what they were made with and the limitations of early computers. However even in the modern times when we do not. Have to worry about those limitations anymore for example unity does not have the y axis inverted when programming games. But despite that
It is still more convenient because many programmers are already used to. Programming with the y axis inverted and. The inverted y axis is what Godot uses when we. Use the coordinate system or the Cartesian graph. So this is what good doing Cartesian graph looks like as you can see here we have our origin on the top left. So as you can see here the y axis is going down. As it's. Reaching positive infinity. And notice the x axis hasn't changed our x axis is not inverted.
So as we keep going rate we're reaching positive infinity on the. X axis. When going rect and notice how when we have a positive x value in the. X section in our coordinate system and a positive y value that we are showing on the screen. Now because our y axis is inverted or quadrant 1 is no longer at the top right. It is instead on the bottom right. And as you can see here. Because of inversion. Notice quadrant 1. And then to the left of that is quadrant 2 quadrant 3 quadrant force instead of having R quadrants move counterclockwise.
We are instead moving clockwise. As a matter of fact it's not a big deal. Having the y axis inverted it's not a huge entry to barrier when it comes to formulating our math formulas or getting the behavior we want. All that means is going. Up. If we want to simulate the behavior of going up we need to reach negative infinity on the y axis. And if we want to go down we need to reach positive infinity on the y axis and no behavior changes are seen in the x axis because the x axis has not been inverted.
That's a matter of fact. Let's take a look at basic movement in Godot. So. In our inverted y axis to go right we want to reach positive infinity on the x axis to move left. We want to reach negative infinity on the x axis to move. Up. However. Since now we're dealing in the y axis territory to move up. We want to reach negative infinity on the y axis and to go down. We want to reach positive infinity on the y axis and that's basically the. Only changes we see in our behavior when the y axis is inverted.
So again it's not a huge deal when it comes to using math with an inverted y axis and the more you program the easier. You will absorb this the easier it comes to notice what you need to do when you want certain behaviors. Now next. Is transforms. And transforms is short for transformations. This is how we describe. And. Change. Our object's position orientation also known as rotation and skill in the game world. As a matter of fact transforms is a common feature among game engines.
Now let's take a look at change in position so we have our game object in the inverted y axis and our original position is. X and y. Over when we want to. Move it. To the right. We change the x axis in the positive direction so to X and notice how we haven't changed anything in the y axis because we're just going straight to the right now. This is how we would do a change in our Cartesian graph. And this is. An option. That game engines should give you easily and it's usually through transforms. Moving
Moving on. We can also Change Our rotation. So for example our original position our original orientation in the game world is always zero degrees. And that would be zero degrees on the x axis and y axis. If we're working in 2D and in 3D would be zero degrees on the x y and z axis. However in 2D world when we want to describe a change in rotation we just simply use degrees and let's say we want to translate that we want to transform our rotation to 180.
Well a game engine should give us that ability and when we do change the rotation. Notice how we basically. Flip. Upside down. Now. Sometimes you may not find yourself using rotation. However in some games rotation is a nice additional feature to have. For example in our flappy bird. Video we were able to see that the. Rotation was changed when going up and when going down. Lastly we also have a change in scale. So when we want to say our game object has an original scale we use 1 in 1
One in one is what we use to describe our original scale. And from our original scale we can either increase or decrease and in this example we are increasing the scale to 2. We're saying we want to double. Our image on the x axis. And we want to double our image on the y axis in games you will most likely never use scale unless you are programming for a mobile games and in that case you'll use scale. However I recommend that you never scale up. But instead you scale down we'll take a look at scaling in a different episode.
Now, Again as I mentioned transforms is a Very popular option, a very common option in game engines. So in unity, Notice how we have. Transform. And you can see. In the 3D world we can change our position along the x axis the y axis and the Z axis. Same thing for rotation we can change or rotation in the x axis the y axis and the Z axis. And on top of that we can also change our scale on the x axis. The y axis and the Z axis. And this is a good way to describe not only where our object is
In the game world. We can also describe the rotation in the game world and we can describe. How big how small our scale is in the game world. Now. Of course we're. Programming and good don't. So I'm gonna show you the transforms featuring the dough. Now we haven't gone over this but space show is basically the nodes we use in the 3D world. And as you notice we have an option for transform again. Now notice how instead of position we use translation. However this is the same as position we can change our position in the x axis.
The y axis and the Z axis on top of that we can change our rotation. In the x axis y axis and z axis and. We can also change our scale on the x axis y axis and z axis. Now. Notice how. Our. Position and our rotation are all set to zero. This is basically our default setting. When we describe our object on the game world however notice how our scale is set to 1 1 and 1. And that's because. When we want to describe our. Default scale our original scale we use the value 1 among all the axis is
And you'll notice the same thing is true where the unity transform. Notice how our scale was set to 1 1 1 and everything else is 0. This is how we describe our original position and our original rotation using the zero values over for scale when we want to describe our original scale we use the values 1 and that just means. We multiply our width or height. And in the z axis the reach. And same thing for Godot or scale or original scale in the x axis as one or original scale on the y axis is 1 in our original scale on the z axis is 1.
Again these numbers. Are multipliers. So obviously if you were to change these values to 2 you're multiplying the original length within reach by 2 therefore doubling. Your. Image your game object by 2. Again we will go over scales in another episode. And that's basically it for this episode. Just a brief introduction to the Cartesian graph and transforms. Thank you so much for joining me. If you have any questions or need anything clarified please feel free to leave that in the comments section down below.
I look forward to seeing you in the next episode. Have An amazing Day.