 Are your animations looking a little jerky? I bet you play some games where the movement is slick and it feels so good. Well check out this tutorial on Scratch Velocity that also includes speed and acceleration. It's a slightly more advanced tutorial, but it's going to set you on to make a pretty epic trigonometry game. Smooth movement come in your way in just a sec. Hey what's up crew, it's The Surfing Scratcher here, teacher-surfer programmer, and I help curious people just like you along on their learning journeys through video tutorials. This video is all about speed, velocity, and acceleration. We're going to define what each of them are through a few drawings over in sketch note and then we'll jump over to Scratch to see what's going on there. We're going to be doing all this to set you up to build a game and learn about trigonometry in future videos, so let's get stuck into it. All right we're just over here in sketchbook. We've got the y-axis going up and the x-axis going across. Both axes are incrementing in 10 pixels. We're going to be using our little spaceship here to understand speed, velocity, and acceleration. So let's get ready to blast off. So first we're going to be looking at speed and speed is just the amount an object moves or changes by. So here our object is our spaceship and the amount it's going to move or change by, we don't know yet, but let's just say down here we've got 10 pixels. Now 10 is a positive number and that's important because speed is always positive. To illustrate this I'm just going to grab our ship and place it over here at this spot. This spot here has a y value of 10 and it has an x value of 0. Now we're going to get a block that you're all probably pretty familiar with. This block is a change x by block and here we've got change x by 10. Each time we click this block our rocket ship should move 10 pixels across the x-axis to our new position where our x is equal to 10 and our y is equal to 10 also. The new position of our ship will be at x10 and y10. Remember the power of variables? We can use variables to change x and here we've just got a speed variable and currently it's set to 10 but it doesn't have to be set to 10. We could set it to 20 so the next position that our rocket ship would go it would skip 20 pixels across to x being equal to 30. The next spot would be 50 and the next spot would be 70 and so on. Notice how this is kind of just like skip counting that currently our ship's got a bit of an issue because it's going to change its x position by 20 each single time. Well what about if we're at x is equal to 70 and we want to go the other direction? Well speed's not going to help us out because remember speed is always positive. This is where we're going to need velocity. Velocity is just defined as speed except it's got a given direction and it can be negative. You might say hang on a minute we've already been moving in a direction. Well you're right we have been. We've actually been using velocity but we're just using speed as equal to 20 across the x-axis to use as a starting point. Now you could go ahead and just change speed and make this velocity instead and then chuck a negative sign in there. That would be fine. A nice little way that you could do it is keep speed there and you could multiply it by either one because if you take speed and you multiply it by negative one it just becomes that number as a negative. But most of the time you're going to work with something like this. You're just going to have a velocity variable specified here on the x-axis and we'll just be changing that velocity. So if we were to change our ship's x-position by x-velocity its next position would be at x is equal to 50 because we've just subtracted 20. We could do the same thing to the y-position we just need to change the y-position by a variable called y-velocity and here I've just set it to 10. So the next position if it'll just change the y-value would be up here at positive 20. If we change both of them at the same time we would change the y-value which would take us to here at y is 20 and then we'd need to change the x-value which is negative 20 and the cool thing is when we move both of these we end up creating a right angle triangle but I'm getting ahead of myself. We'll check that out in future videos. Now that we have some understanding of both velocity and speed let's head over to scratch and see our rocket ship in action. I've created a project to help our understanding of speed and velocity in scratch you can find a link to this project down in the description. I encourage you to go and check it out and play around with it. Now I've got our rocket ship here and our rocket ship in this project just moves along the x-axis. When I click the green flag you can see this. There goes our rocket ship across the screen. Now you notice when it gets to the end of the screen it bounces. When it bounces that signals a change in velocity. So what are these green and red little segments here? Well this is just a display of the current speed. We can change the value of speed using this slider. We can slow the ship down and you can see that as we slow it down these segments get closer and closer together. As we increase the speed they get farther and farther away. When you play around with this slider to either slow down or speed up your rocket what you're doing there is you are accelerating or you are decelerating it. I encourage you to use this slider to both slow down and then speed up your rocket. You can see when I do it quite quickly we can create some nice little easing motion there to simulate some acceleration and some deceleration. We can get our rocket to take off nice and easy and we can get our rocket to also stop. With the rocket stopped I'm going to grab the speed slider and I'm going to slowly increment it by one at a time. So there's two three four five six seven eight nine and before it gets to the end I'm going to stop it. Now this line is interesting because you can see here at the start all these line segments they're really closely packed together and as we increase the speed they got farther and farther apart and this is a visual understanding or visual demonstration of what acceleration actually looks like. I've just switched across to another project which you can find in the description down below that uses key presses to control the acceleration of our rocket ship. All right key press adds positive acceleration and the left key press adds negative acceleration. Check out the little box up here I'm going to hold down the left key press and you can see it's now set to negative one. If I hold down the right key press it's set to positive one so when we go left we add negative one and when we go right we add positive one. We can change this value by manipulating this slider here by changing the acceleration so if I put this down to zero point five when I hold down the left key we're adding negative point five and when I hold down the right key we're adding point five. The key thing I want you to remember about acceleration is that acceleration controls the velocity which is just speed and that makes sense right because when you're in a car and you accelerate you're controlling the speed you're either speeding up and when you put on the brakes you're slowing down you're decelerating. Okay I'm going to add some left key presses to our rocket ship and what I want you to do is watch the value of x velocity here and see you can spot the acceleration changing. Here we go we're going to hit it all the way up to negative 10. There we go we made it to the end now we're at the end let's inspect our line of acceleration. Back here at the start we had a really small velocity value you can see that by how close these line segments are together and then we increase the velocity and look what's happening to our line segments. They're getting bigger aren't they until we got all the way to the end where they're quite spaced out. Remember each of these line segments represents the velocity that we changed the amount that the ship moved so the width of all these line segments is the absolute value of the velocity and that's 10. Absolute just means we stripped the negativeness out of this negative number because you can't have a negative width so if you've ever been driving or seen someone else drive you'd notice that on the road there are all these speed limit signs that tells you the maximum speed that you should be traveling the same thing applies here in this project that refers to our max x velocity. If we didn't have this value velocity would just continue forever so I can minimize this value and restrict the speed to five so now when I press the right key our spaceship has a maximum speed of five I've just bumped that back up to 10. It's quite fun to play this game and try and get the rocket to get all the way to the end and have this nice little easing back and forth effect it's quite fun to play with and it feels really nice. You can increase acceleration so the rocket ship feels a little bit more responsive and the smaller that the acceleration value is the more sluggish it feels so it's a bit slower to get going. Now you've got some visual experience of what velocity acceleration does let's go ahead and program it in scratch. Okay I've got my fresh scratch project open and I've just added in the rocket ship sprite you can see over here in the costumes that I've reduced its size by selecting it and reducing it here using the box but size is set to 100 over in the project viewer. Go ahead and make it two new variables it's going to be called x acceleration and x velocity. On the rocket ship's right go ahead and grab out a when the green flag is clicked and a forever block and inside that forever block we want an if block and the condition is we want to sense for a key press so we want to sense when the right arrow is pressed and we want to sense when the left arrow was pressed. Do you remember what we're going to change inside of these blocks? Well it's acceleration and a right key press we're going to add positive acceleration and a left key press is going to be negative acceleration so let's grab a set block and we're going to set acceleration to one on a right key press and we're going to set it to negative one on a left key press. Now I've just hard coded this in remember in the other project we had that max acceleration value if you want to do the same thing just create a new variable called max acceleration and instead of setting the x acceleration to one we'll set it to the max acceleration in the negative acceleration we're going to go 0 minus max acceleration and this does the exact same thing so up here after I press the green flag watch the acceleration value as I press left and as I press right notice that it gets stuck so what we need to do is set x acceleration to zero before we sense for key presses you can see here it's reset back to zero and now when I press right it goes back to zero now I know that this block of code sets the acceleration so I like to create a custom block for that and now I'm going to grab all that code and pop it under set acceleration and then we can set acceleration forever now we're going to get our ship moving so the first thing we'll do is it'll set the x velocity to zero and we click the green flag and then we need to change the velocity by the current acceleration to do that we'll get the change block and we're going to change the x velocity not by one we're going to be changing it by the acceleration okay I'm just going to press the stop sign and the green flag to reset those values and now if I press the right key down you'll see our x velocity goes quite to a high value and when I press left it goes to quite a large negative number as well so we need to constrain the speed limit we need to constrain the velocity let's first create a variable and we'll call it max velocity I'm going to set max velocity let's say to five when we click the green flag remember we can change this if you're wondering why I'm using capital letters here it's just because this is a constant value I'm not going to change this value and I leave the variables that I will change to lower case or camel case now we need to create some conditions so if our current velocity is bigger than the max velocity well then we're going to set x velocity to that max velocity so what this is saying is that when the velocity is bigger than the maximum value we're just going to set it to that maximum value the condition is a little bit different for the lower end of the range we need a less than sign we want to know when x velocity is negative max velocity so do that we're going to get a takeaway operator go zero takeaway max velocity then we just need to duplicate this if statement switch out the conditions and instead of setting x velocity to max velocity we need to set it to the negative max velocity which is just zero minus the max velocity in the same deal I'm going to create a custom block and call it constrained velocity because that's what this block of code does and then I'm going to call constrained velocity after we change the x velocity by the x acceleration now test that out I'm just going to click the green flag here and when I press the right arrow key you'll see that x velocity is set to five and when I press the left arrow key it's gone down to negative five now it's been incrementing and decrementing the acceleration value so quickly that we can't even tell so now let's actually get our ship moving across the screen recall that velocity is just the amount that we're moving or changing the ship's position across the screen so to do that we can get a nice simple block we're going to change the x value not by 10 see you later I'm going to change the x value by the x velocity and now when I press the left key our rocket comes back into the center goes right and it goes left you notice a slight problem with our rocket if I take my finger off the keyboard it just keeps going how do we get around that the reason that's the way it is is because velocity always remains at five now ideally if we took our finger off the keyboard we would decay this value basically just means that we're automatically going to decelerate it we're going to change the velocity over time by making its value smaller the best way to do that is to set the velocity not to zero but to itself times now if we did times one it would just be setting it back to itself but we want to set it to a value that's a little bit smaller than itself so if we multiply it by something like 0.9 or 0.8 it's going to decay over time let's test it out there we go i'm going to speed up the rocket and now take my finger off and you can see here x velocity decreases over time so our for-revolute is getting a little bit unwieldy so you know me I like creating custom blocks to make it a little bit cleaner so this whole code block what it does it moves the ship so let's just move the ship after we set the acceleration there you go that's everything that we've done up until now I encourage you to go and explore this a little bit further of course our ship only moves in the x direction so you might want to do the exact same thing that we've done except the y direction another thing to check out is changing the values of max velocity and the acceleration you know bumping it up to 10 and making acceleration even smaller play around with these values and see which ones feel the best to you and there you have it this is the solution with all the y values and it as well and the other thing that I did is when the green flag is clicked I just set the starting position to zero zero so now you can watch our ship move across the screen with some nice feeling I'll leave a link to this project down in the description I know we kind of went the long way around in this tutorial but that was to build your understanding there was a quick and easy way to use velocity in your project so check out the card in the top right hand corner now to see that one you'll notice that as we move our ship around the screen it stays in the one orientation so to be really nice to rotate it and that's where our trigonometry series is going to start I'll see you in that video okay just before you go this tutorial is going to set you up to make a trigonometry game and if you want all the starter projects for the game in this series then I've got a downloadable pack where you can get all those scratch projects you find a link to that in the description down below until then I want to go find a way I'll catch you in the next one