 Are you curious to learn about other trick functions? Well, in this tutorial, we'll look at the Scratch Sign and Cross Functions and how we can use them in a gang. It's another trickier tutorial, just take your time and go over whatever's unclear. Remember, I believe in you. You got this. Let's crash this ship in just a second. 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. Welcome back to our Trigonometry series, in this tutorial we're going to be exploring the sign and cause trick functions and we'll be doing that to expel our rocket ship when it crashes with the turret because currently it can just glide straight over it and lose lives and that's no good. If you're just jumping into this tutorial, check out the starter project down in the description. We can grab a copy of that to follow along or suss out the card in the top right hand corner right now and that'll link you to all the videos in this series. Alright, let's get stuck into it. In the last tutorial, we detected a collision between our rocket ship and our turret. We'll do first is to a bit of a revision and use ATAN to find the angle between these two objects. We can use that angle to then position the rocket ship a little ways away from the turret. We can also create a simple spin animation as well. So let's jump over to the sketchbook first to build your understanding. Over here in sketchbook, we've got our X going across, we've got our Y going up and down. We've got our two objects, the turret and our ship. And just around them, we've got our circles that we were using for collision detection. We're just going to lower the transparency of our objects here so we can better see the triangles and what's going on. So I've just got our little right angle triangle here on the screen, measuring the distance between the two center points of our two objects. We're going to call it distance Y and we just get that by taking the ship's Y and subtracting the turret's Y. This yellow line, we're going to call it distance X. And we calculate that by taking the ship X and subtracting the turret's X. And this angle in that triangle, we can get by using ATAN, distance X, divided by distance Y. Why is that the case? Because when we want to find the angle of tan, we use opposite over adjacent. And the opposite is our distance X here. And our adjacent length is our distance Y. Check out the card in the top right hand corner right now for a longer explanation on that. OK, I've just cleared the screen because when we have a collision here, we want to send our rocket away from the turret. So let's just create an arbitrary distance here away from the turret. We want our rocket ship to end up at this point when it collides with the turret. So how can we do that? Well, we already have that angle that we just computed through ATAN. We actually want that hypotenuse length to extend all the way back to the new position of our ship here. And that's represented here just by blowing up this smaller triangle into this larger triangle. We know what this angle is. Remember, that's just ATAN and we can make this length an arbitrary length. So let's just make it something like 200. So if we've got two bits of information, we can then use that to figure out the information we need. And the information we need is the new X position of the ship and the new Y position of the ship. The X and Y is just here. We know that the X is the opposite side and we know that Y is the adjacent side by referencing our trigonometric ratios down here. We've got two bits of information we know. We know the hypotenuse at 200 and we've got an angle here. I'm just calling it 45 degrees. When we look down here in our trigonometric ratios, we know the hypotenuse, that's just 200. And we know what the angle is for each. We know it's 45. So what we're trying to find is the opposite, which we'll use for the X and the adjacent, which we'll use for the Y. So let's work out our opposite side first. I've just represented our SO ratio here and I've plugged in the values that we know. So the sine of 45 degrees is equal to the opposite divided by 200. I just got that by plugging in those values down here. Now, we want to find the opposite. So to do that, we can just multiply this side using a little bit of algebra by 200. When we multiply this side by 200, we also do the same thing on the other side. When we do that, these cancel out because 200 divided by 200 is just one. And that leaves us with 200 times sine 45 is equal to our opposite or X because opposite is just X. So this is what we're looking for. We can use the same process to find our adjacent side. Yeah, I'm gonna get the car values and just plug them in. So the cos of 45 is equal to the adjacent divided by 200. But we just do the same thing, multiply by 200 on both sides. And that leaves us with 200 times cos is equal to the adjacent and now adjacent is just Y. We can now head over to scratch and code this up but I encourage you to rewatch this section if it's a little bit fuzzy, maybe even changing around our values of our angle and their hypotenuse here and see if you can plug these values into our SO ratio and our car ratio. But it's time to scratch or cross and scratch and hold onto your hats because things are about to get a little bit gnarly. I've laid out all of our formulas here that we're going to use and now we'll just use the scratch blocks to fill in the blanks. In our detect collision custom block we've already defined distance X and distance Y. And if you're using this data project you'll already have this too. We're gonna be responding to this lose life broadcast. So we're gonna be doing all of our work in when I receive lose life. Let's just scroll down and give ourselves some space. First thing we do is our angle and I'm actually gonna stick this in a variable. Gonna call it collision angle. I'm gonna make it for this sprite only just so I don't clutter up all the other sprites. Now I'm gonna set collision angle and here I need the ATAN block. So grab the operator block with the drop down and find ATAN. We need to divide distance X by distance Y. For reasons I'm not gonna go into right now cause I did it in a previous tutorial. We need to add 180. When our ship's Y is beneath the Y of our turret. So we need to add to ATAN. This is gonna be 180 but only when the ship's Y is less than the turret's Y. So let's get a less than operator. We're gonna get the Y position of the ship and we will get the Y position of the turret. If I click this, it's gonna be false. But when I move the ship beneath the turret here it will evaluate to true and true it's just one false mean zero. So we can multiply that one or zero flag by 180. So this will either be 180 or zero, awesome. And this is our collision angle. The next thing that we need to do is to find the expulsion distance. So what's that? So when we have a collision with our turret we wanna send our rocket backwards a certain length. So we just need to define that. I'm gonna make a variable and call it expulsion distance. Call it crash distance, crash length, whatever floats you boat. Then in the, when the green flag is clicked block we wanna set that constant. I'm just gonna go for 100 for now. Then scroll back down to the lose life hat block. Now we're ready to use that distance and multiply it to the angle that we have. So we're going to set X to the sign of that collision angle. We need to multiply that by expulsion distance. Grab our sign, talk it in the multiplication operator block and then put that whole thing into the set X. Now we wanna set the Y. I'm just gonna right click and duplicate it because it's essentially the same thing. But instead of sign, we wanna use the cause function. I'm just gonna click the green flag and test this out. And with any luck, we'll see our rocket be expelled from it and boom, there it is. Our ship lives are now nine. Our indicator has gone down and our rocket ship was expelled away from the turret. Those of you with a keen eye would have seen me adding turret X and turret Y here. This only applies when our turret isn't in the center. So when it's in the center, our turret's value is 00. But if we move it down here, we need to compensate. I'll show you what I mean. So when our rocket ship hits the turret from down here, ooh, it does something really funky. And then when we collide with it, it bounces it really far away. You can get around that by changing the X and changing the Y. Gonna change the X by the X position of the turret and we'll change the ship's Y by the Y position of the turret. And that should fix up our little bug. Boom. But remember, that only applies if our turret is not at 00, which for our game isn't the case. So I'm gonna get rid of it. And just that we keep saying crash. So I'm actually just going to get rid of that in the detect collision custom block. Scroll back down to lose life. For those of you who like attention to detail, what we're gonna do now is create a little spin animation when our rocket collides with the turret. Making you variable and call it collision spin. We're going to set collision spin to one and a half rotations. So that's just 360 degrees for one full rotation plus 180 degrees, which is equal to 540. Then we're going to repeat until our collision spin is say less than 10. Then we're going to set collision spin to itself times nine tenths. Now this is just 90% of its current value. So it's going to decay over time. Lastly, we wanna point in the direction of collision spin. Okay, let's take it for a spin. So here we go, our rocket collides. And there we go. We've got a nice little spinning animation there happening when our rocket ship collides. It's a little bit dazed and that's not too bad. I just wanted to give you a really quick way to do this. Obviously it's not perfect because the orientation flips over there and that looks a little bit strange. But I'll leave that as the challenge for you to tweak as our focus is on trigonometry. But here's a hint. You probably wanna record the current direction in a variable and add the collision spin to it and then animate it down to that recorded direction. The last thing I'm gonna do just to tweak our game here is just add a sound. Sound I'm gonna add is a crunch effect. And in my sound blocks, I'm just going to start the sound crunch when we lose a life. So now when we collide with the turret, we get a sound effect. So great job. We just uncovered the sign and cause trick functions. Now our game is starting to take the shape of a game. The next thing that we're gonna do is keep some missiles to both our turret and our rocket ship. So they can start attacking each other and we'll be exploring the tan function to be able to help us out with that. Thanks for checking out this tutorial on the scratch of sign and cause trick function. I hope you're starting to get a little bit more comfy with trigonometry. There's only a few more weeks left in this series so keep at it, you're nearly there. But until next time, I'm off to go find a wave or catch you in the next one.