 So now we're here to wrap up our series on how to make a digital scan model of the solar system that uses scratch. In this video, we're going to use the orbital period values from that NASA planetary fact sheet. That just means the number of days that a planet takes to orbit around the sun. We'll also incorporate that with some relative distances to create a simulation of what that might look like. Now remember, this is just a model, it's not perfect, it's just there to give you some visual understanding of how this might work. Because those large distances, they're really hard for my head to comprehend. And if you're a teacher, educator, or homeschooling parent out there and you're searching for some solar system printable worksheets, then go check a link in the description for some resources that you can use with your group of learners. But let's go and complete our voyage around the sun in just a sec. Hello world, Surfing Scratcher here, Teacher's Surfer Programmer and on this channel, I help curious owners just like you along on your learning journeys. In this video, we're going to take our planets that we've constructed in previous videos and get them orbiting around the sun. If you're just jumping into this tutorial, go check the description for a link to a starter project. And there's also a link to the playlist up in the top right hand corner coming your way. You won't need the project to work along with me, but it's going to make your life a lot easier. Okay, so let's dive into it. First things first, you're going to need to go down in the description and click on the link for this NASA planetary fact sheet that we've been using. And the two columns that we're interested in is the distance from the sun and the orbital period, which is just the number of days the planet takes to go around the sun. And these are Earth days. Once you've found this file, I want you to open up a text editor. I've just got text edit here on the Mac, but any plain text editor will do. I'm just going to change that to plain text. I'm going to go back to my fact sheet here. And I'm just going to shift, click all these values and copy them, control or command C, head back over to text edit. And I'm going to paste those values in. Got my values here, then I'm going to save it. And you'll see I've already got it listed here as distances. So I'm just going to overwrite that for now and replace it. That's good. Close it. Do the same thing with your orbital days. Copy, you text edit and save that one. We're going to save this one as orbit days. Boom. Now that we've got our data, let's head back over to scratch. Now that we're back over here in scratch, I want you to click on the variable so that you can access the lists and let's make some new lists. We'll call one distances, call this one orbit days. Great. So we've got our two new lists here. Now what we want to do is right click and import and locate that text file that you saved. Just make sure that you've got the match. So this is the distances text, right click, import for the orbit days and import the data. Cool. So we've got our data in here. Now it's not to use it. I'm just going to have a look over here at the comment that I've created. And this is sort of the blueprint of what I'm hoping to achieve here. So we've just gone ahead and imported all the NASA data. You'll recall that this application runs on different states and we need to add a new state for the orbits for currently in a gravity state. So do that. I'm going to head over to the backdrops. I'm just going to make this a little bit smaller so we can see what's going on. There we go. So let's click on the variables and create a new variable. We're going to call this the state underscore orbit scroll down. Going to grab a set block. We're going to set state underscore orbit to whatever you want, but I'm calling it orbit. We've been using our key presses. I've just duplicated one of those blocks. I'm going to scroll down and make this the number four. And we're going to set that to the state underscore orbit. I'm going to do something that's going to look a little bit strange. It will make sense soon. I'm going to grab a, when the green flag is, click block, then grabbing a forever block. Then we'll get an if condition. Now condition, we want to see if state is equal to the state underscore orbit. And when it is, we want to broadcast the message. In the message we want to broadcast, I'm going to call orbit planets. And what this block is going to do, it's going to send a message to anyone who's listening to it and all of our planets are going to listen to this whenever we're in a state of orbiting. The reason I'm doing this way is so I don't have to have a forever block in every single planet. I'm just doing it centrally here in the stage. Now I'm just going to hide these lists because we don't really need to see them. I'll also hide that state underscore orbit. Just going to click the green flag here and press the number four. And you can see we're now in the orbit state, which is great. Now we've got all the planets lined up here, but what we want is we want our planets lined up from the center because we're going to create a sun and we're going to space them out the relative distance to each other. So we can't have a relative size with a relative distance as we saw when we created the distance sprite here. Those distances are quite large relative to their size. So we can put the relative distances between them. The sizes will be funky, but we can also put the relative orbit times. And that's what we're going for in this particular view. So let's jump into Mercury and I'm going to scroll up. Currently we've got the when the state has changed and we're going to set the size. So we're going to set a constant size for when we're in the orbit state to do that. I'm just going to grab an if block. I'm just going to do the check to see if the state is equal to orbit. And I'm going to grab that if block and put it up the top of our conditions here. And then we do something a little bit sneaky. Our state is equal to orbit. Then I want to stop this script. And what this will do is it will stop executing the rest of the instructions because what would happen if we didn't do this, it would skip over this one because state is not equal to scale, but it would execute this size. And I don't want that to happen. So we can execute the business inside this block sandwich and then quit this script. And what I'm wanting to do here is set a size. So I'm just going to duplicate these blocks. I'm not going to worry about the x value for now. And I'm just going to set a size here. We've got a constant default planet size. And I'm going to change this to a new constant and make a new variable. I'm going to call it orbit planet size in all caps. I'm going to scroll down. I'm going to set it to 1.5. So basically this will be 100 times 1.5. Then I'm going to right click and select that variable quickly jump back over to the stage and just make sure that when I'm setting up all these constants that I set up the orbit planet size as well. Cool. Let's jump back into Mercury. So now if I press number four on the keyboard, you can see that Mercury has gone to a really tiny size. And that's what we're looking for. Now I've just duplicated that code block. I'm going to drag and drop it over each of the planets. I'll go inside of a planet and I will just attach that if blocked to the top of set size. I'm going to do that for all the planets. And you go ahead and do that too. Cool. That only took a minute. I'm going to press number four and you can see all of our planet sizes has tripled down. I'll go back to this third state and we've blown the back up when we're in the gravity state. So that's just what we're looking for. This next step is easy peasy. I've just created a sun sprite, drawn a circle, set it at 0x and 0y. And when we receive the state has changed, I'm checking to see if we're in that orbit state and then I'm just showing it if we are and hiding it if we're not. Now let's get all these planets and spacing that with their relative distances to each other. On the screen, I've just got our distances list back up and I've also got the grid backdrop happening here. The way this is going to work is we know that the X width from the center to the edge of the stage is 240. And we want to put our planet with the longest distance away from the sun. So this would be Pluto here, pretty close to the edge of the stage. We basically want to calibrate it. So 5,900 and million miles. But I'd have to check that. We want to say this number is equal to around about 230. We want to say Mercury up here is going to be pretty close to the sun, but we don't want it right on the sun. We want it about here. So I'm going to compensate for about 10 or so pixels. And the way that we'll do that is by using some simple division. And this is our formula. We're going to take the planet distance. So this is going to be the variable that changes. And we're going to divide that against the longest distance. So to give you an example, the smallest distance is going to be Mercury. And if we divide Mercury by Pluto, we're going to get a value that's really close to zero. If we get the longest distance and we divide it by itself, or we know if we divide anything by itself, we just get the number one. And then what we can do is we can use multiplication. So we can times one by say 220. And that will give us a value around about 220. And when we're close to zero, this number is going to be much closer to zero. And then what I'm going to do just to bump it a little bit away from the sun when it's really close to zero, I'm just going to shift it by a constant of say 10 pixels. So the maximum x is going to be 230 and the minimum will be 10. So let's turn this into scratch code blocks. So we want to find the planet distance of Mercury. Now we know it's the first value of our distances list. So to access that, we can get the item number planet names. The planet we're interested in is Mercury. If I click that, we'll get number one, because we want to refer to the first item in the distances list. Then we want to take that index that we just got and get that item number of distances. If I click this, we should get 57.9. Now we've got our planet distance. Let's get our longest distance. If we just duplicate this code block, we'll get the item of Pluto. And we click this, we get the plutonium value. Last thing we need to do is get our division block here. Let's work that in. I click this block, we get a number that's really close to zero. And if I change this to Pluto and I click it, we get a number that's one. Okay, now we need to do the multiplication. So let's grab a multiplication block. And hey, you'd probably want this as a constant value because it'll be really painful to update if we want to change this. But I'm going to leave it at 220 for now. And then I'm going to whack that code block inside. And you can see this block is getting pretty long. But now if I click it, it's equal to 220 for Pluto. And then when we set it to Mercury, it's equal to two. The last thing we want to do is just add the shifted amount. So we're not always on top of the sun. What we need to do there is grab an addition block, grab that number 10, and combine all those blocks together. Now, these two values, like I said, you probably want them as a constant because I don't want to have to go through and change the number 10 for all of these. So I'm just going to go ahead and quickly do that. And the process is just the same for the orbital default size that we set earlier in the video. You see, I've just done that with max width and sunshift. And I've also created one in the future for our max height. So now that we have this code block, we can go ahead and use our orbit event. So when we receive orbit planets, let's set the x position to this block. And then for now, we're just going to set the y position to zero. Now, if we make this a little bit bigger, you can see that Mercury has taken up its position next to the sun here. Now it looks like it's a little bit far from the sun. So I can go ahead and change that sunshift value to say around five, click the green flag and look, it's pretty close to the sun. And I'm happy with that. Now I'm not going to duplicate this across the other planets yet, because I think we'll actually get this planet orbiting around the sun. And then we can copy this code block across to the rest. We're going to be using some trigonometry to help us out. I'm not going to go through it in depth in this tutorial. Go check the card in the top right corner for a deep explanation on how this will work. Okay, so let's go execute this plan. First thing we need to do going to make a variable. And it's very important that we make it for this sprite only going to call it orbit angle. What will happen here is when we drag and drop this onto other sprites, it will recreate that variable automatically for us, making our labs a little easier. Now this variable orbit angle is going to tell us what amount of degrees the planet is around the sun. Recall that for a full orbit of a circle, it's going to be 360 degrees. We're just going to make that one planetary year. So what we can do is get our orbital days list and we can take that 360 degree value and divide it by the orbit days. We'll get larger angle amounts of the mercury and we'll get really small amounts for Pluto. To get a reference to our orbit days list here, I'm just going to duplicate this code block and we're going to get the mercury planet name and we're going to get the orbit days. I'll click this and you see we get 88. Then we'll grab a division block. We're just going to hard code in 360. This one's not going to change. I'm not going to worry about a constant for this. If I click this, you'll see that the angle amount that we get is four. Now this four is just an arbitrary number, but I'm going to treat it as an angle amount. So you can think of this as 4.09 degrees. I'm going to get a change block. Then we're going to change the orbit angle. We're going to change the orbit angle by this block that we've just created. Then I'll grab an if block. We're going to check to see if the orbit angle is greater than 360 degrees. And if it is, we need to set the orbit angle to the orbit angle value mod 360. And what the mod operator does here is it just divides whatever the orbit angle is by 360 and that just leaves us with the remainder. Now you can see when I click this code block, we can see our orbit angle increasing. And I've just set the orbit angle to 357. I'll click this block again. And you can see it's reset it to one degree, which is awesome. And I'm going to grab this code block and attach it to when I receive all the planets. And you can see our orbit angle is updating and resetting back to zero. Next, it's trick time into grab out this ABS operator block, click the drop down and find where it says cause and the angle that we're interested in, that's orbit angle. If I click this now, you'll see it keeps updating and it's going to oscillate between values of one and negative one. What we can do with that is multiply it by a constant value. And the constant value that we want to use is the X values that we discovered earlier in the video. So I'm going to grab a multiplication block here. I'm going to grab that big gnarly block and combine it with the cause block. And then we can whack that inside the X. And you can see Mercury kind of looks like it's orbiting around the sun, but just behind the sun. It's kind of being a bit cheeky here. So we also need to do this for the Y value. What we can do is really just duplicate this code block and change a couple of things in it. Instead of cause, we want to focus on the sign. And let's just scroll across to where we see max width, because we don't want the max width. We want the max height. Okay, we're not quite done. We just need to put that inside the set Y. And now you can see Mercury is orbiting around the sun. Now I'm going to grab this code block and I'm going to drag and drop it onto Venus. I'm going to head inside Venus. It's a bit of a mess. So I'm going to clean it up. And wherever I see a Mercury reference, I'm going to change that to Venus. And there you have it for very little work. We've also got Venus orbiting around the sun as well. And you can see it's going at a much slower rate than what Mercury is. And there you have it. I've gone ahead and just dragged and dropped all those code blocks onto every other planet and just changed these three variables here to correspond to the planet that it's on. You can see that they move at a snail's pace. And I can set a challenge for you to see if you could speed it up. And the hint would be you do that in your change orbit angle code block. And you probably just want to increase the move amount. And there you have it. That is how you can create relative orbit values and move amounts for each of the planets that are sort of spaced to their relative distances as well. Of course, the planet sizes, they're not relative as they're all clambering into each other here into the inner solar system. And on that, another extension idea that you can do is maybe create some different states. You create an inner solar system orbit state and an outer solar system orbit because you can see it's all kind of bunched up and sort of hard to see here. Another extension idea could be to grab the pen tool and trace the path of the orbit. That would look pretty cool. If you really want to get gnarly about it, you could move the X position of the sun because I believe it also has a little slight orbit as well. But that's where we're going to leave this tutorial. I hope you enjoyed this series of making a digital solar system model using a scratch. I'd also encourage you to have a look at the NASA planetary fact sheet again and seeing if there's some other values here that you could replicate in your digital scale model because you know how to make states so you can create different views. You know, I'm thinking about mass. How could you do that? Thinking about the length of the day, so maybe you get some rotation values and perhaps you could even do the temperature. You're really limited by what you can think of with this. Down below in the description, I'll leave a link to a scratch studio where you can post your version or your digital solar system and hey, tag me and let me know if you went ahead and you created some different values because I would be really keen to see them. I hope you enjoyed this series. I will catch you in another one.