 Have you ever been led scratching your head with Scratch's direction system? Well, in this tutorial, I'll explain how it works and how we can use it to point the direction of a coordinate using a tap. Remember, that's just an inverse trick function. But I want to be real with you. This tutorial, it's a little bit on the tricky side. Heck, when I was first out researching this, it took a few tries for it to really land with me. But now here I am explaining it to you. So I know if I can do it, you can do it as well. Remember, ask questions, hope each other out, and strengthen the magnet and ask more girls. You've got this. All right, let's get started in just a sec. Hey, what's up, crew? It's the Serving Scratcher here, Teacher-Server Programmer, and I help curious people just like you along with their learning journeys through video tutorials. Welcome back to our Scratch Trigonometry series. If you're just jumping in, check out the starter project down in the description. Or the cards in the top right-hand corner now is we've done a little bit work to get up to this point. You are going to need some understanding of Soka Toa. So if you're unfamiliar with that, I suggest you go check out a Khan Academy video first, or an earlier video that I've recorded, replying it in Scratch. More cards coming your way and links in the description. Currently, our rocket ship zooms around the stage, which is just dandy, but it's always oriented up north. So what we're going to be doing in this tutorial is manipulating its direction based on our keyboard presses. I'm going to use Trigonometry to help us out with this. So let's jump across to Sketchbook to build a visual understanding first. Okay, we're over here in Sketchbook, and we've got our little rocket ship here in the center. We've got our X going across, and we've got our Y going up and down. And we've also got our trig functions here, our Soka Toa. And we'll use this to help us out with rotating the direction of our ship. First thing I want to do is kind of translate these terms, hypotenuse, opposite, and adjacent. And create a new triangle with labels that will be a little bit more familiar. So that brings me to this little triangle here. Whenever we go a cross, we're referring to the X value, and that's the X velocity, the amount that the rocket ship moves across the screen. When the rocket ship moves up, well, we're talking about the Y position. We're actually adjusting the Y position of the rocket ship. So we're going to call that one Y velocity. Remember, in our Scratch project, we've actually got these variables already defined. In the last tutorial, I showed you how to create speed based on these two values using Pythagoras' theorem. Card coming your way in the top right-hand corner to look at that if that's fuzzy. Okay, but we're here to get this ship rotating. So let's just say the ship was again a travel to this point here. It's currently here, and then it's going to travel to here. I can simulate that by just rotating the ship basically pretty close to it. And you might be tempted to do something like this. Okay, our ship travels along the X axis with our X velocity, and it travels up with our Y velocity. So we can draw a nice right-angle triangle there. And this angle would refer to the direction. I can get our velocity triangle and just pop that over there just so you can see it with all the labels. But this isn't the triangle we're after. Let's jump over to Scratch to see why. The answer lies over here in the direction system. So in Scratch's direction system, you can see that 90 degrees is east and zero degrees or not. To make matters worse, our ship is even pointing west when it's zero degrees. Man, that's confusing. So let's go fix that first. Open up the project if you haven't already. We're going to jump into the costumes, and we're going to select the whole rocket ship here. And what we're going to do is we're going to rotate it east. I'm just holding down the shift key so it's directly east. And you can see when it's pointing east on Scratch, when it's zero degrees, it's pointing up. Just going to pop open the direction box again. And I'm going to rotate the direction clockwise. And you notice that the direction values start to increase positively. So these are positive numbers. And when we get to 90 degrees, our ship is pointing east. When I rotate the ship all the way down to south, we get to 180 degrees. When I reset the ship back to north and we turn anti-clockwise now, look, the direction values are now negative. They're kind of reflecting what was happening on the other side. And they go all the way down to negative 180. And then they kind of flip over like this. So the big takeaway here is zero degrees is pointing north through 12 o'clock. And recall back in Sketchbook, our rocket ship was sort of pointing around this direction. And you can see this is the triangle that we want to emulate. So let's jump back over to Sketchbook to suss it out. Okay, so we're back over here in Sketchbook. And let's draw our starting point first. So remember, going up north or 12 p.m., that's zero degrees. We're wanting to go over to this spot right here. And then we can just complete our right angle triangle and signify our right angle triangle. And this is the angle that we're interested in. I've just placed our colors back here and shaded in the angle. Now let's get our labels back up. There's our angle, signified by this shaded area. And first we go up with the y velocity, the change in the y of the ship. And then we go across because that's our change in x value to arrive at our destination point. Now that our reference angle is here, our y velocity is actually the adjacent angle. And our x velocity is the opposite angle. And when we have two bits of information, we can compute a third bit of information. This is where our Sokotoa ratios will come to our rescue. We know the opposite. We know the adjacent. And we want to find out what the angle is. So we can use Toa to help us out. Up here on the screen, I've got our trig functions available to us in Scratch. Here we've got tan and a tan. So which one do we want? Our standard trig functions of sine, cos and tan, they all take angles. And when we pass in an angle, we get a ratio. So a sine of 30 would return 0.5. Now over on this side, the right hand side, these are all the inverse trig functions. So arc sine, arc cos and arc tan. And they take a ratio and they take a ratio of the division between the opposite, the hypotenuse or the adjacent sides. So the arc sine of 0.5 is going to return the angle of 30 degrees. If that's all a little fuzzy to you, go check the card in the top right corner to do some revision. We're focusing on Toa and we want an angle. So that will be an inverse trig function. So we've got the opposite and the adjacent referring to tan, we want arc tan. So I've just gone and grabbed our arc tan function and I put in x velocity divided by y velocity. How do you know it's x velocity divided by y velocity? Well it's opposite divided by adjacent. And our opposite side is our x velocity. And our adjacent side is our y velocity. Why is it adjacent? Because the side is adjacent to the angle. And this is the side, our x velocity, that's opposite to the angle. It's time to head over to scratch and code this up. Back here in scratch, I'm going to create a custom block first. I'm going to call it set ship rotation. I'm going to call set ship rotation after we set the speed inside the when the green flag is clicked set of instructions and inside the forever loop. Next up, jump into your variables and we're going to create a variable called ship rotation. Inside the custom block, we're going to set the ship rotation. And this is where we're going to put the a tan function. Jump into your operators and drag out the operator block with that drop down menu. Find a tan, get the division operator block, jump over to your variables, grab x velocity, you're going to divide x velocity by y velocity. Then grab that whole block and put it inside that variable. Last thing we need to do for the moment is jump over to the motion category, going to grab the point in direction block, head back over to the variables and you're going to grab the ship rotation and that is the direction we're going to point the ship in. Okay, technically, you don't need this variable, but I like to have it just for clan winners. So you see what's going on. Now let's click the green flag and test out our handiwork. So I'm going to go up here and cool. There goes our rocket ship. Now I'm going to go down. Uh-oh. Hang on a minute. Something's a bit funky going on there. What about when we go up again? Oh yeah, that's looking good. Okay, so it looks like there's a bug in our code. I knew about this and we're going to explore it now. So to test our bug, I'm just going to press left on the arrow key and that looks pretty good. I'm going to press right. That looks pretty good. I'm going to go up and that looks pretty good. Now I'm going to press down. Okay, something's a bit wonky when we go down. Now I'm going to press left and down and that's pretty wonky. I'm going to go back up and I'm going to press right and down and that's also wonky. So there's something happening when we are going down and in the left quadrant and the right quadrant. So let's jump over to Sketchbook to explore what's happening. Okay, the issue happens when our spaceship is pointing down in this quadrant and in this quadrant down here. Remember that our spaceship's rotation starts here at 12 p.m. or direct north. I'm just going to draw a line out to the direction that we want our spaceship sort of turning and the angle that we're interested in is actually this one here. I've taken a screenshot from scratch that shows the direction that we're after and here it's saying it's 135. There's a way that we can test this. I've just got our a10 function up here that we've been using x velocity divided by y velocity and we can test out this point. So we've got x is equal to 2 and y is equal to negative 2. It might be a little bit hard to see there but y is equal to negative 2. We can take those values and test it out in Scratch and see if we get the same angle. Over here in Scratch I've just hard coded those values into our function. So x velocity being 2, y velocity being negative 2. I'll click this and we get negative 45. That isn't 135. Can you think of where that comes from? Let's go explore it. We know that when our ship is fully rotated south that angle is 180 degrees. So what's the difference between 180 degrees and 135 degrees? Yep it's 45 degrees. So it seems like what Scratch is giving us is this right angle triangle right here but this is 45 degrees not negative 45 degrees. The answer lies in this clock that I've got up on the screen. Remember 12 o'clock refers to zero degrees, three o'clock 90, six o'clock 180 or negative 180 and nine o'clock negative 90. Now that works true for the y values that are greater than zero but when the values are less than zero our clock actually rotates all the way around. So our new zero degrees is actually down here and this is where our 12 o'clock is. I know it's pretty funky right? So if we trace along anti-clockwise and we see the values from zero degrees we're going to negative 90 which is why this angle here is negative 45 degrees and not 45 degrees. When our ship is oriented in this quadrant and we look at our values they're actually increasing from zero to 90 so they're positive angles. This is good to know. So when our ship is angled toward this quadrant we're getting angles between zero and 90 degrees and when it's angled into this quadrant getting angles between zero and negative 90 degrees. I've just cleared the screen and got us back to where we started. Well we know we only want to do some action when y is less than zero and we know if we rotate fully around all the way it'll be 180 degrees. So we can say that when y is less than zero we can add 180 degrees to whatever value we get. So instead of our starting value being zero we're making the new starting value 180 essentially and then we're just taking away the angle. So it would be 180 take away 45 and that's going to leave us at our 135 degree value. For the angles over here we're just going to add whatever angle is here to 180 degrees and thankfully scratch just converts this angle to its negative counterpart for us. How nice thanks scratch. We could use some logic to make this a negative number but I'll leave that for you to discover. Now it's time to head over to scratch and code this up. So back in scratch we need an if block and put that point in direction outside the if block we need the less than operator and we're looking for when y velocity so the change in the y amount is less than zero so that means it's a negative number and when that happens we need to change the ship rotation by 180 so we're adding 180 to the ship rotation. Let's hit the green flag and test it out. So we're going up across back left and now we're headed down and everything is as it should be. Yee-haw. I was in the scratch forms researching and I found a really slick way to do this in one line thanks to zero seven one six so this is an extension for those who are looking for a challenge. So when y is less than zero this is going to evaluate to true and true is a number one. When y is greater than zero or zero then this is going to evaluate to false and it will return in a zero so we can use the one and the zero to our advantage. We can multiply 180 by that true or false value so right now this is going to evaluate to false but as I move the ship down and I click this it results in 180 I move up and it goes to zero it's basically this if block in a reporter block then what we can do is we can add our a10 value to our 180 degree switch and slot that in and the really cool thing is we don't need that if block but hey just do what works for you and there's all that code so you can see it and there you have it our ship is just zooming around the screen looking pretty nice and it feels pretty nice as well I hope yours does too but if I look at our game it's a little bit easy for our rocket nothing really happens I mean it's not really a game so what we're going to do is we're going to set up a turret to fire on our ship and we'll get some practice with the a10 function again in later tutorials we're going to be shooting missiles and we're going to be using different trick functions so make sure you stick it out you're doing an incredible job this isn't easy but you're persevering with it you're doing incredible so keep at it I'm here to support you along the way I hope this video helped wriggle you along with your understanding of finding angles using trigonometry and that's basically the shortcut if you're out there building a game be it in scratch or some other programming language and you want to find an angle 90 95 in time you're going to be using a10 if you're still a little bit fuzzy it's cool because we're going to have another crack at it in the next tutorial and if you reckon you got it down that's cool because repetition is the mother of skill a team great to have another pass at it anyway if you found it helpful be sure to smash that like button and educators they'll have a resource for you down in the description below that you can go and suss out until next time I'm off to go find a wave I'll catch you in the next one