Hello and welcome to another episode in the Godot basics tutorial series. In this episode we will be taking a look at velocity and acceleration. There is quite a lot you can do with physics in game programming besides the usual such as speed acceleration gravity. Sometimes we need formulas to do more complex things such as calculating our initial velocity when we want to reach a certain height in our jumping function. However we're not going to go into a lot of detail in this episode. I
I will go into more detail towards the end of this series however I just want to reintroduce the idea of physics and its importance in game programming. Let's start with distance. Distance is a new numerical measurement of how far apart objects or points are. Let's go over an example of distance and how we would calculate that. So let's pretend we have Dora, Who's going to school and the schools a thousand meters. So when she reaches school she would have traveled a distance of 1000 meters. Now let's pretend that she forgot her books at home.
And so she must travel an extra 1000 meters with distance we would say that Dora has traveled a total distance of 2000 meters. And one thing to keep in mind is notice how there are no arrows no arrow heads on these lines. So distance is not concerned with direction. Now another word I'd like to reintroduce to you is displacement and displacement is a vector whose length is the shortest distance from the initial to the final position of a point P undergoing motion.
The big take away from displacement is that it's a vector and let's go over an example of displacement. So we have Dora in the same situation she's travelling to school and so we say that Dora has traveled a total displacement of 1000 meters
And the same thing happened again. Dora has forgotten her books. And notice. How. Initially. We had her travelling. With a vector
Basically a direction to the east.
But now because she forgot her book we're having her travel west and so notice how initially we travelled 1000 meters 1000 meters away from our initial position. However because we're heading back towards the position we initially came from. We are in a sense subtracting. And so in this case we would say that Dora has travelled a total displacement of zero meters. Why. Because Dora hasn't left her initial position she's moved 0 meters away from where she initially started.
Now that we went over briefly about the difference between distance and displacement let's look at the velocity formula. Now normally you may be introduced to the concept of speed and the speed formula and velocity is basically the speed formula. Except we are dealing with vectors so that's the only difference speed does not worry about vectors whereas velocity does worry about vectors and since we're in game programming we do in fact worry about vectors. So a quick look we would say our velocity formula as the following the average velocity is equal to the change in displacement over the change in time.
Again we won't go into a lot of detail in this episode that will be towards the end however the big takeaway I want you is that we can calculate for displacement and so we would say the change in displacement is equal to the average velocity multiplied by the change in time. And so now we have this formula we can work with to calculate our displacement in the game world.
Let's go ahead and take a look at acceleration. The formula for acceleration is the following. We would say the Average acceleration is equal to the change In velocity Over the change in time. So the most important takeaway from the acceleration formula is that the change in velocity is equal to the average. Acceleration multiplied by the change in time. Now how exactly do we use these formulas in game programming. How do we use the change in position or a change in displacement. And
And how do we use the change in velocity to help. Us with our game programming journey. It's really simple. You can combine the position. Also known as displacement but I'm gonna keep it as position. And we can change our position in the game world based on velocity multiplied by time and the same thing for velocity. We can change our velocity based on an acceleration multiplied by time. And one important thing to note is that our acceleration must be a. Constant Value. Must be a value that never. Changes. And so from our formula we can actually make
A pseudocode for gravity so let's go ahead and take a look at that. Notice how we have a velocity vector 0 0 and a position vector 0 0. Notice how we have an acceleration And
The only value we have in our acceleration is nine point eight which is gravity, well gravity is nine point eight one meters per second squared. But we're gonna use the nine point eight value here in our own acceleration formula because we're trying to write pseudocode for graph. And if you look here. Over doing is we're changing our position vector to add on to the lost city multiplied by delta time. Now we haven't gone over delta time. Yet. But. No matter the game engine no matter what game engine you pick. All game engines will provide you with some sort of variable that represents the time in between frames and we're gonna take a look at that later. However
However, The most important thing to notice is that Our position plus equals velocity multiplied by delta time line of code looks almost like our displacement formula that I showed you earlier word displacement was equal to velocity and time. And so as you can see right there we are able to use our velocity formula to help us create a line of code that allows our object in the game world to move. And in this case we're not moving so much as having a force act upon us which is the acceleration and that's the next line of code.
Our next line of code is velocity plus equals acceleration multiplied by delta time and the second line of code looks almost like our acceleration formula. Where were we changed the variables for time. We swapped them across the equals sign and we were able to get velocity equals acceleration multiplied by time. And right here in just two lines of code with a basic understanding or not even an understanding just the basic memorization of physics formula we were able to create pseudocode for gravity.
So these two lines of code are basically applying some gravitational force on our game object or rather our position vector. And keep in mind that even though we use positive nine point eight in our acceleration vector pseudocode remember that positive goes down in the good dough game engine game world. For 2D and from there we can do quite a lot of stuff. So let's look at another example. And in this example we're gonna do pseudocode for jumping. Now we have the same thing. We have a velocity vector position vector we have a gravity vector that we are signing nine point eight one.
And that vector is applying or rather we would like to apply some type of vector force upon our game object in the y axis only. But notice here are velocity vector has a starting value. And if you play around with physics formula or even just a basic understanding of how physics work. This would also be called an initial velocity. So as you notice it's negative 400. That means we're gonna have an initial velocity of our game object going up and then because we're having gravity act upon it over time our velocity is going to get closer to zero and from zero it's going to get closer to positive infinity.
And notice our position starts at zero zero.
And notice that our two lines of code haven't changed at all. Position plus equals velocity multiplied by delta time again. We're gonna go over Delta time in a later episode it just means the time between two frames. So think of it as a fraction and then on top of that notice our second line velocity plus equals gravity multiplied by delta time. And I'm gonna go back to the previous slide and notice. It's exactly the same. It's just. With a. Basic rudimentary understanding of physics. We were able to get a jumping mechanic working because we changed our initial velocity to go up when we know we have a constant velocity or force acting upon us going down and we'll go into much more detail towards the end of this series again.
I just wanted to introduce the idea of physics and how we can use it to help us in game programming. And as a matter of fact these formulas you'll notice you can use these. No matter which game engine you choose. And. In a later episode we're gonna go over player movement. And you'll notice we're going to use line 1. On a different episode. I'm going to introduce the jumping mechanics. And you'll notice. When I do write that code eventually. We're going to go over by adding the second line of code velocity. Now keep in mind that Godot does have the ability to give you gravity
Acting upon your game objects. However for this intro series. I do want to show you how you can implement your own physics with just a basic understanding of physics formula and as a matter of fact you don't even need to know physics. Or rather you don't need to understand physics at a high level. All you really need to know is at what time do you need to use the appropriate physics formula. And just by doing that you can create quite amazing and complex games or rather game mechanics. Well that's all I have for you
In this episode. Thank you so much for joining me. If you have any questions or comments please
Feel free to leave them in the comments section down below. I look forward to seeing you in the next episode. Have an amazing day.