 Now our digital scale model of the cell system moves into a little bit more complicated territory. What's that thing that roots you and I to the ground? It's gravity and every planet has it. So in this video, we're going to use the gravity values from that NASA planetary fact sheet data to help simulate what gravity might be like on other planets. Now remember, this is just a little model to simulate that. It's not going to be perfect. We just want to give a visual representation of it. And what's cool, if you got some interest in how to make a platformer in scratch, then this video is going to help you out because gravity is used in these platforms. And the cool thing is maybe your platformer can go to different planets. And if you're a teacher, educator or homeschooling parent 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 code some gravity in just a sec. Hello world, Surfing Scratchy here, teacher, surfer, programmer. And on this channel, I help curious learners just like you along on your learning journeys. This video is part of a larger series on creating a digital scale model of the solar system in scratch. Go check the card in the top right hand corner right now and see those previous videos. And there's a link down in the description for a starter project to get yourself up and running with it. This video is all about simulating some gravity on each of our planets here on the screen based on data obtained from a NASA fact sheet. And I just want to make a quick shout out to FunArt Tutorials. Man, I hope I said that right. Let me know if I didn't because I checked out your how to make a platformer video that was very helpful. We're going to be using elements of this. So you know where the card is to go check that out. And quick disclaimer, I don't claim to be physicist in this video. I'm just using data obtained from a NASA fact sheet to just make an approximation of some gravity to simulate it visually. Hey, if we can get Neil deGrasse, Dyson, Neil Nye, or Derek Muller in here to perfect this, then hey, I'm all for it. And as always, I've got our blueprint set out here. Let's go execute it. All right, I'm just over here in the stage code area. And the first thing that we're going to do is create another state. And this is going to be called gravity. So let's just duplicate this code block and we'll set it to number three. For the three number key. And we need to create a new state variable. So I'm going to head over to variables, make one, call it state underscore gravity. I'm going to scroll up to where I'm initializing all those states to a bit of scroll down. And let's do it. Let's initiate it, call it gravity. Boom. And now that we've created that state variable, we need to make sure that we change the state to state underscore gravity. So now I'm going to go ahead and click the green flag and with any luck, as we press the third key on the keyboard, we should have all of our planets line up down the bottom here and we shouldn't see the distance sprite. Boom. That's what we're looking for. Okay. Now we're going to need a couple of sprites. So I'm just going to paint one. And this is going to be a ground simulating the planet's surface. Now obviously not all planets have a surface, but we're going to make the assumption that they do. And I'm just going to draw a rectangle. It's not going to have an outline. So I'm going to get rid of that just like that. And I'm going to drag it down. So it's just above the planets. We're going to make that our ground. You can change the height of this if you want, but I'm just going to leave it like that. The next sprite that we want to add, we're just going to choose one. You can import your own, but I found this dog in a space hat and I quite liked it. So we're going to have dot and dots a little bit big. So I'm just going to reduce dot size to about 70. Yeah, dots looking pretty good there. I'm just going to head back over to the planet distance sprite and grab this code block here when I receive state has changed. I'm just going to drag it on top of dot. I'm going to head into dot. And instead of state underscore distance, we want to be state underscore gravity. And I'm going to grab that same block and put it on the ground. So what that is going to do is only going to show dot and the ground when we're in the state of gravity. Okay, let's do a little bit of setup now. We need to create a couple of variables and a list. We need to create a gravity variable. We're going to create a wide velocity variable. If you're not sure what velocity or gravity is, go check the card in the top right corner. I've got a video that explains this. And I'm going to scroll down and create a list. And just like we've got the diameters, we need the gravities. I'm not really sure there's a plural for gravities, but you get the idea. And this gravities list is going to refer to all the different gravity measurements that we're going to obtain from that fact sheet. So let's go to that fact sheet right now. Okay, I've got the gravity fact sheet down here. Link below in the description for you to go and check it out as well. And I've got this column highlighted for our gravity for meters per second squared. We're not worried about the units here because our units are a little bit different on the screen. We're using pixels per frame, essentially. So we're just going to copy these values, open up a text editor, and we're going to create a new document. And I'm going to convert it to a plain text file so we can simply paste those values in. Now I'm going to save it as a text file. Just call it gravities. Cool. Now I'm going to head back over to scratch. I'm going to right click on our list and import. And I'm going to go to where I saved that, to gravities, open, and you'll see that our list is populated. So number one is going to refer to Mercury. I'll just give you a quick refresher on that. Here are our list of planets, and we've got a one-to-one mapping of all the locations. Now I'm just going to turn that off. And the first thing that we're going to do is we're going to grab the event when the selected planet has changed. And what we can do is set the gravity of that new planet to whichever one that we've selected. Let's get a set block. We're going to set the gravity. Let's scroll down to our list blocks, and we're going to need the item number and the item. We want the item number of the planet names, and the thing that we're looking for is the selected planet. And then we want the selected planet's index of gravities. So this is essentially going to give us the gravity value based on the selected planet. So the selected planet is currently Jupiter. The index of Jupiter is five, so I'm expecting a gravity value of 23.1. So let's click this to check it out, and there we go. Let's whack that block inside of there. And then when we click on a couple of different planets here, so we've got Pluto, and our gravity has changed to Pluto's gravity. Now we've got Mars' gravity. Okay, we're just about ready to get dot moving. So now let's drag out a when the green flag is clicked block. Put a forever loop down. Then we're going to create a custom block, and we're going to call it gravity. And then we're going to have an input and that input. I'm just going to call it acceleration, because I mean that's what gravity is. It's just the rate at which we accelerate. It's just a special name that we give to it. And let's go okay. Then we're going to call the gravity my block. Okay, great. Now it's time to construct our gravity function. Okay, so it's got it pretty crazy at the moment. Now we need to check if dot is touching the ground. So we'll put an if block in. We're touching the ground. And you want to repeat until we're not touching the ground, and we'll duplicate that code block just there. And then what we'll do is we're going to change the y value by one. So essentially what's happening here is dot's going to fall from the sky. It's going to touch the ground. We want to reverse the velocity. We want dot to go up. And after dot is no longer touching the ground, we want to set the y velocity back to zero. Okay, we want to make sure that we're only calling this gravity function. If our state is equal to the gravity state. So let's click the green flag and see where we're at. Going to head across to the gravity. You can see some funky business is going on here with dot. And the reason for that is we don't have run without screen refresh on. So let's right click, edit our block, and go run without screen refresh. We'll click okay, and our dot just sort of hovers above the ground. And that's good enough for now. Okay, and the way that we're going to show and visualize our gravity values here is we're going to get dot to jump. So inside of out, if we're touching the ground condition here, we're going to sense for a keyboard press. And let's just sense for when the key space is pressed. And what we can do here simply is just set the velocity to a value. And a value of around 15 seems to me some pretty nice jumping. So I'm going to press the green flag. I'm going to go across to it. And I'm going to press the spacebar. And you can see there that dot's having a hard time. And the reason for that is that we've got a pretty high acceleration value going on here. What we're going to change this to at the moment is negative one, because that's going to be our baseline. And then when I press the spacebar, you can see dot has a nice little jump here. So we've got our little test working, which is great, but we've got our values hard-coded in. So now we need to change it so we can map it to each of the gravities here in our list. We know that for a negative one value, that's mapped to the earth. So we can divide earth by earth. So if we divide earth gravity by earth gravity, we're going to end up at one. So let's go get a division block and let's hard code in earth's gravity at negative 9.8. Then we can go grab that gravity variable that we created earlier. And remember, this gravity variable has the current selected planet's gravity. And if we divide it by a negative number, then we're going to end up with a negative number. So if we click earth and we click this value, we should get negative one, which is great. Now let's click mercury. And I've got to be careful what I say here, because if I ask you if this number is going to be smaller than negative one, the answer would be wrong. It'd actually be a little bit bigger because negative 0.37 is actually larger than negative one. But it's kind of less gravity is what we're going for here. If we click on Jupiter, for instance, this is going to be a lot smaller. So let's whack that code block in there. And now to test it, I'm just going to click the green flag and see there's Jupiter's value and there's earth's value. And if I click mercury dot is going to try and leave the screen there. So that's looking pretty good. Now our work isn't quite done yet because for every gravity, we're hard coding in the wide velocity. And it'd be neat if our jump height was actually a little bit different for each planet. Now I'm just kind of playing around with these values a little bit. And what I arrived at was kind of switching these around to invert it. And we want it to be a positive number because this is a jump value now. So we're just going to divide earth's gravity by the selected gravity. And then because we like this 15 as a baseline, we know when earth is selected, this is going to be equal to one. We can multiply this by 15. But there is a slight problem with this code because if we go to a planet with really low gravity dot is just going to go off into space really quickly. And it's not going to look as nice as it could. It's going to be even worse for Pluto. So what we can do is just when the key space is pressed, we can just cap the velocity if it's greater than say 15 or 20, just like you see here. And now when I press the space bar, the gravity is looking pretty good. And I'll go over to Jupiter now and we press the space bar. You can see there's quite heavy gravity going on there for our large value. We go to earth and there's still quite a nice jump back to Mercury. And that's looking pretty nice, feeling pretty good there. Go to Pluto for our smallest gravity. And off goes dot and dot gently floats back down to our ground. The last thing I think we should do is when the state has changed, we should set dots, y to a positive value so the user can see dot drop in. And that's going to be pretty simple to do because all we need to do is go to an x and y. And the y that we want to go to, let's just make it 200. Okay, so now let's switch back to a different state. Here we are in the distance state and then we'll go back to the gravity state and you can see dot floats into our screen. And there you go. That's how we can do gravity. I've just gone ahead and reduced the size of our ground here. It's quite fun to play around with the ground just to watch dot go kind of up and down. Quite nice to play around with. An extension that you could do as well is change the color of the ground or the surface depending on which planet is selected. I'm not going to do that in this tutorial, but there's something for you to think about. All right, that's the end for this tutorial. Like I said earlier, I don't claim to be physicists. So this is just my approximation of these values. If you are in the know out there and you are pretty hardcore with your physics and you've got a better implementation of this, I'd love to hear from you. You know, post your solution down in the comments section. That would be awesome. I hope this video has given you some sort of visual reference point for the gravity values across the planets.