 Did you know that you can use trigonometry to make circles? If not, that's perfect because in this tutorial, that's exactly what we're going to set out to do. We're going to be creating an asteroid that's going to be orbiting around our turret to give it a little bit of protection and to make life a little bit more difficult for our player. You've got all the building blocks, so let's go piece them together 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. In this tutorial, we're going to get an asteroid orbiting around our turret here to give it a little bit of protection, but also so our rocket ship can have a little bit of protection. We're going to be using the sine and cos trig functions and investigating the unit circle, so let's get stuck into it. As always, check out the description for a starter project so you can follow along with this tutorial. The first thing we're going to do is create our asteroid. Let's paint a new sprite. I'm going to zoom in a little bit and I'm going to get the brush tool. I'm going to change the fill color. I'm going to make it this dark gray. Then I'm just going to reduce the size to about five. Then really, I'm just going to draw a wonky sort of circular shape that kind of looks like a rock. Select the paint bucket and we just want to lower that gray color a little bit. Then we're going to fill it. Grab that brush tool again and you might want to increase the size. Let's make it slightly more dark and then you're just going to like draw some little squiggles. Kind of like this. So you're getting this rock shape over here on the screen. It's looking a little bit big at the moment, so we can just select it all and reduce its size. Make sure it's in the center. It should snap and lock and that's looking pretty good. Make sure it's this gray color and not brown so it's not some floating poop in space. With our asteroid drawn, let's jump over to the code tab. Let's rename our sprite to asteroid. We just check out the stage. We want our asteroid to be orbiting our turret like something like this. So it's going to be taking a circular shape. So we're just going to jump over to GOGBright to check out the unit circle. Build our understanding. Okay. On the screen right now, I've got the unit circle. The unit circle just has a radius of one and that just means from the center point to the edge, that is the length of one. Of course, going across is our x-axis and going up and down is our y-axis just like you'd expect in scratch. Here is our right angle triangle. You can see that we can control our right angle triangle by dragging this slider and it increases the angle and decreases the angle. You can check out here that we've got this red line and we've got this blue line. This red line is always going across so it's on our x-axis and our blue line here is on our y-axis going up and down. I want you to focus on the red line for a second. I'm going to make the angle zero degrees. What is the length of this red line? Well, it's equal to one. It's just the radius, isn't it? It's going across. What happens to this red line as I increase the angle? What do you notice? What's happening to the red line? We've picked out that the values are decreasing. Its length is decreasing in size. Okay, let's roll it back to the start. We're going to focus on the blue line now. What is the length of the blue line going up and down? Well, it's currently nothing. There's no height to it. There's no y-value. But as we increase the angle, what happens to it? It starts to get bigger, doesn't it? It's increasing in size all the way up to when it's 90 degrees and it's a length of one. Okay, I'm just going to reset this back to zero. So the point that our asteroid is going to be, it's always going to be on the edge here. The point at which our right angle triangle intersects with the circle. And that's just our x-value and our y-value. And the way that we're going to get that is we can pass in an angle to our cos theta to get the x-value. That'll be this x-length. We can pass in the angle to the sine function and we will get the height. So what we need to do in scratch is basically create this angle variable and it's going to go all the way to 360 degrees and we're just going to increment it and reset it back to zero. And that's how we're going to get our asteroid to orbit around the turret. Let's go code it up. Here in the asteroid sprite, I've just dragged out a when the green flag is clicked. Going to make a variable. I'm going to call it asteroid angle. And when the green flag is clicked, we want to set asteroid angle to zero. Then we're going to grab a forever loop and forever, we're going to change the asteroid angle by one. If the asteroid angle is equal to 360, then we want to set the asteroid angle back to zero. Then I'm just going to grab that if block and put it just above the change block. I'm just going to click the green flag here and we're going to see the asteroid angle values increase. And when it gets up to 360, it will reset back to zero. And there we go. It just reset. So what the asteroid angle is doing is it's just creating that angle that we saw in GeoGebra. We can use this angle and pass it into the sign and cause functions to get the coordinate of the circle. The next block that we're going to need is in the motion category. And we're going to get the go to X and go to Y. Okay, I've just got our unit circle back here on the screen. So this green angle refers to our asteroid angle. And remember, we said that when we pass in that angle to the cause function, we're going to get the X value. So we can go over to our operators category, grab out that operator reporter block with that dropdown menu and find the cause trick function. Then all we want to do is pass in that angle, the asteroid angle. So we're going to duplicate that and this is going to give us our X length. Now, if I set asteroid angle to 51 and I push this, we should get a value that is very close to 0.63. And there we go, 0.629, which is essentially 0.63. Then we can just duplicate this block and change it to the sign because the sign is going to be our Y value. Then let's pass in our sign to the Y and our cause to the X because X isn't cozy, it's crossing, remember? We can put that block just above that if statement. And then when we push the green flag, oh, but nothing happens. Why is that? Well, these values, they're incredibly tiny at the moment. There are values between zero and one. So we need to change that. The way that we can change that is multiplying that cause or sign value by a constant. So what on earth does that mean? So let's just say we want a constant of 100. We know that the cause function is anything between negative one and one. And we know one times 100. Well, that's just equal to 100. What's 0.5 times 100? Well, that's 50, isn't it? And then what is zero times 100? Well, that's just zero. So that's why the constant is going to work. So let's grab out that cause trick function and multiply that value by 100 and slide that back in. We can do the same thing with our sign function. And there it is. So you can see it. And I put our grid back on the stage. I'm just going to blow this back up and click the green flag and you'll see our asteroid starts to orbit the turret now, which is exactly what we wanted to do. Currently, our asteroid orbits in a perfect circle. You'll notice that it intersects with the 100 points because that's our constant, it is 100. But the X width is a little bit wider than the Y height. So we could change the X constant to say 150. That way, its maximum value will be 150X and its minimum value will be negative 150X. Let's check that out. So now the asteroid has this oval orbit, which is kind of neat. The next thing that we'd want to do is create some collision detection because right now our asteroid and our rocket ship, they can just collide with each other with no consequence. I'm now in the rocket ship sprite and I'm checking out the detect collision custom block. Currently we've got the collision detection for our rocket and our turret. I'm actually gonna put all this code into a new custom block called detect turret collision. Cool, then I'm gonna get all that code and put it in that custom block. And now in detect collision, first thing we wanna do is check the turret collision. Then you guessed it, we're gonna create a new custom block, call it detect asteroid collision. Okay, and then after we detect the turret collision, we're also gonna check for an asteroid collision. If you wanna do it the trick way, the way that we were detecting the turret collision, go check out the card in the top right hand corner right now but we're just gonna do it the fairly simple way. And that's if we're touching the asteroid. We also want to expel the ship and lose a life. So we can just duplicate those two. We're gonna run into a small problem here but let's just check it out first. So when I click the green flag here and we test it out, we collide with the asteroid, you can see that it kind of gets stuck to it and we lose all of our lives straight away. So that isn't the solution that we're looking for. And the problem is in our expel ship because the expel ship is currently hegged to the turret. It moves the rocket ship a specific distance away from the turret. There's a couple of ways that we can solve this. I'm gonna give you the quick and dirty way because this has been a long series and we're nearly at the end. I encourage you to check out the card in the top right hand corner right now to check out when we did that expel ship custom block. Our quick and dirty way is just to get rid of that expel ship custom block and we're just gonna put it in a wait block. We're gonna wait for around two seconds. Two seconds is just enough time to pause our rocket ship and also get out of the way of an incoming missile if it hasn't already yet fired. So I'm gonna click the green flag and then I'm gonna go around and collide with the asteroid and you can see the asteroid passes and I can just get out of the way of that incoming missile. Sometimes we might lose two lives and hey, that's just the nature of the game. And that brings us to an end of this tutorial. You can extend this tutorial as well so you can get the rocket ship to be able to destroy the asteroid. You could also create multiple asteroids. That would be kind of fun as well. In the next tutorial, we're gonna put the finishing touches on our game. We're nearly at the end and all of our trig is essentially done. So congratulations on making it this far. You're nearly at the end of the road. Well done. Hey, thanks for checking out this tutorial that showed you how to make a circular scratch using trigonometry. If you made it this far, I just wanna first congratulate you and your journey with learning trigonometry. It's an epic feat. This series has been a marathon. It hasn't been easy. I hope you learned something about yourself and how you respond to challenge. If you created some bugs, you found it difficult and you wanted to give up along the way, what I say to that is awesome. Because what matters, it's not the mistakes that you make. Rather, it's how you choose to respond to those challenges when they come your way. Do you ask yourself questions that help you solve the problem? Or are you someone who tells yourself those negative stories that make you feel a little bit silly? The secret is, if you start asking yourself those quality questions, then you're gonna start an exponential road curve. We've just got one video left in our series to tie up some loose ends. But until then, I'm off to go find a wave. I'll catch you in the next one.