Godot 4 3D Platformer Lesson #2: Solid Objects & Physics Simulation!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] constructing our 3D game level what that means is in this video we're going to begin constructing 3D physical objects objects that you can see and more importantly objects that other objects can bump into in this lesson we're going to be adding a floor into our game level which is a 3D solid object that you can see and we're going to learn how to create reusable objects which are going to be falling blocks that we can create as many of as we want in our game level and the neat things about these following blocks is that they can actually fall with gravity using godot's built-in physics engine and they can bump into each other and into other objects like our ground in our game level let's go ahead and jump in on my desktop here I have the godoa game engine icon and if I go ahead and open up Godot you can see that I have my project listed here just in case you're not aware and I'll cover this right at the beginning of this course if you open up the Godot editor and you do not see your project listed I'm going to go ahead and select my game project and press remove and remove what you can do is simply take your game project folder right here and drag it into this project manager and let go and it will add itself into the list of projects you can go ahead and double click on to open you can also open up that project folder and find the file that ends with DOT Godot that file will probably be called project.godot and if you find that file you can also drag it specifically into your project manager and it'll show up in this list just like it did before for me okay let's go ahead and double click on the project to open it up as of the last lesson in this course we made a level one game scene in the Godot editor in this level one we have a root node as well as a camera to add a physical object a floor into our game level we have to begin by adding a type of node into our scene recall that from the last video the way that we add objects into our game is by selecting the root node of our current scene and by the way if you don't have your scene currently open I'll close mine you can find it called level1.tsen down here in the file system dock on the bottom left of the Godot editor I'll go ahead and double click on it to open it up if I select the root node of my scene I can then add objects into my game level I'm going to select that root node and press plus because the type of object we're going to add for a floor is considered a physics object in Godot it's a physics object because it exists in a world with physics and that means that other objects like your character like falling blocks can collide with it so if I find the section the branch of this note tree called node 3D and I expand that out and then I find a section here called Collision object 3D and I'll expand that Branch out and then physics body 3D and expand that section out don't worry if you're not following along with me here I'll go ahead and show you how to search for this and just a moment but if I keep digging into physics body 3D I've opened that Branch up there are types of physical objects here including one called a static body 3D this is actually a branch on its own and I could expand it out and find animatable Body 3D but I'm just going to stick with the one called Static body static in this case means not moving which is what we want our floor to not do we don't want our floor to move so I'm going to select that type of node if you just want to search for static and find that exact node it's a red one it's called Static body 3D and I'll double click on it to add it into my game level there it is it should be a direct child that means there's a direct line indented just once here below my game level because I had my level one node selected when I press the plus and added this node in the first place so now what I'll do here is name this node if I double click on its name I can select the text here and I'm going to name this floor and press enter so now that static body 2D is my floor however you'll notice that in the scene in your actual 3D viewport you don't see anything you have it selected and you have these control gizmos on your screen that you can move the floor around with don't do that though you could but we can't see anything right now because it turns out there is an error in my scene if I go ahead and click on this little warning here it tells me what's wrong it says this note has no shape so it can't Collide or interact with other objects consider adding a collision shape 3D or Collision polygon 3D as a child of it to Define its shape that's what's wrong we have a physics object but it doesn't know what it should look like and it doesn't know what its boundaries are in other words what it's Collision surface is where its force field is where other objects will collide with it so I'm going to select that floor and we're going to add two children nodes to it when you construct an object in Godot it's very common that you combine multiple nodes together in a little mini hierarchy so that you have an object with children and the children give that object more information in this case I'm going to select the floor node that I just added and I'm going to press the plus button I'm going to add a node and I'll see if I can find it manually here I'll clear out my search it should be under node 3D it should be under visual instance we're looking for here is under geometry instance perhaps yes it is called mesh instance 3D we want to add a 3D mesh object like with polygons into the scene which is going to be a cube for our floor we're going to stretch that floor out but it's going to be a basic Cube if you don't want to find it in this node tree structure you can simply search for mesh and then I'll find it here mesh instance 3D is what you want I'll double click on it and if you did it right you'll see that it's now a child of the floor because I just had my floor selected when we added it okay this mesh instance doesn't look like anything either until we select it and go over to the inspector on the right side of the Godot editor the inspector will show you properties of the current node that you have selected so if I have a mesh instant selected well I can choose you can see the first property here of that mesh is empty well the mesh doesn't know what its polygons should be essentially so I'm going to click right next to empty here and I can choose one of the default primitive types of meshes that Godot itself knows how to build for you I'm going to select after clicking this little arrow right here and new box mesh when I do that it makes a new resource in other words A Primitive type of data that Godot knows how to make right inside of Godot in this case now we have a little cube a box in our game level if I want to make this box look more like a floor larger but maybe not as tall what I can do is click on this resource to expand it out you can click on the little picture of the cube to expand its properties the resource properties and contract it but I'll expand it and I can now type in values for the size that I want this Cube to be I want the floor to be quite big so I'm going to type in maybe 10 meters and then I'll press tab by 0.5 remember that y the y-axis is up and down in Godot so if I type in 0.5 for y and then press tab to go to the next data input slot here it'll make it shorter half as tall as it was before and I'll type 10 into the z-axis and press tab so I have a 10 by 0.5 by 10 10 size Cube there we go is that enough to make a proper floor well it turns out no this floor it still has that warning here it's telling us that it needs a collision shape in order for the physical object the static body 3D to know where its force field is where other objects will collide with it like its force field so I'm going to select that floor again not the mesh instance but the floor because I need to add a second child to it I'm going to select the floor and press plus I'm going to search for I'll clear up my search for what's called a collision shape 3D where do I find this well if I dig into the branch called node 3D it is right here it's called Collision shape 3D of course you can search for that I'll type in c-o-l-l and I'll narrow my search down to that and collision shape 3D is right here I'll go ahead and double click on that to edit a collision shape 3D is a lot like a mesh instance we need to give a collision shape a resource it's giving a little warning to tell us this by the way if I click on the little warning it tells me a shape must be provided for a collision shape 3D to function please create a shape resource for it what it's telling me is just like with the mesh node that we just added a moment ago it needs to know what actual polygons what type of a mesh to have our Collision shape needs to know what shape of a force field and the size of the force field this object should have so if I select that Collision shape 3D and go over to the inspector on the right side of the Godot editor it has a shape property it's First Property over here it's empty so I'm going to click on the little downward arrow and pick a box shape 3D as its shape if I select a new box shape 3D and click to confirm you can see that it made that same size of I think it's two by two by two for the Collision shape size how do I change the size of this one well like with the mesh I can click on the box shape to expand its properties or I can click to collapse that section as well I can type values directly in but I can also in this case because it's a collision shape grab these little pink handles and stretch out hopefully you can see what I'm doing here stretch out that Collision shape so it's approximately the right size 10 meters across and then I can go up here to its size and just type in manually 10 and press enter to confirm that I'll do the same thing with the z-axis as well I'll drag that little pink handle and then just change it to 10 and press enter and it's still too tall so I'll make it a little bit shorter but then type in 0.5 and press enter to make it exactly the same size as the mesh I can go ahead and hide the mesh to just see the Collision shape and hopefully if I show and hide it I can see that they're exactly the same size and in the exact same position by the way if you accidentally move your Collision shape up or your mesh in some weird position you can always select that node go to the transform section of the properties in the inspector and reset the position to zero and zero and zero to reset it back to the very middle of your scene okay so now we've constructed a floor for a 3D game level how do I actually move my entire floor all at the same time because right now if I select the floor over here in the scene Dock and I use my little move tool by the way if I have my arrow tool selected it'll give me all of the options for moving and rotating my selected object I'm going to press Ctrl Z to undo that move if you have the move tool selected you only have move or rotate or scale depending on which tool you select that will be the only one that's available for you but the arrow tool is a good one to stick with most of the time if I select the floor the root node of the floor object in other words the static body 2D and I move it well you could move the floor this way but I'm going to warn you to actually do one little step before you try moving your floor around I'll press Ctrl Z on my keyboard to put it back at zero zero zero in my world what you'll want to do here to make sure that you don't accidentally select just the mesh and move just the mesh around and then have to move the Collision shape separately but even when you do that well if you select the floor you'll notice the floor still thinks it's at zero zero zero in your world to avoid all this headache what you can do is I'll press Ctrl z a few times put everything back to the middle of my world at zero zero zero what you'll want to do is group your floor together and what that means is if you select your floor and press it's right here this little icon on your screen on the top bar it looks like a little piece of abstract art if you press this button this is called the group selected nodes button and it makes sure that selected nodes children are not selectable so if I select the floor and I press that button you'll see that same little icon show up right next to the floor it means that it's grouped with its children and that means if you simply Lee click in your 3D editor on that object it will not select the children of the floor it'll select the floor itself and then let you move the floor however you want okay so if I move my floor down just a little bit in fact what I'll do is I'm going to press the Z button up here in this Gizmo that's on screen that shows me which directions I can see my scene from if I click on a little Z button it'll take me straight to my rear orthogonal view but if I click on that button again it'll take me to my front view what I'm going to do is move my floor up so that the top of the floor matches where the red line is where that grid is in my scene I want the floor to just meet up with that so that's going to be the floor in my game level okay let's go ahead and do a save I'll press Ctrl s on my keyboard how does my game level look when I'm playing it well if I select My Level 1 tab up here and I press the play scene button up here it'll play the the currently active scene with the active tab it'll play that scene when you press the placing button and here it is it looks very dull but at least we can see our game floor through our camera and if you need to move the camera there's the camera back or up or rotate it you can do that to see your whole floor why does it look so well gray well our scene actually doesn't have any environment in it right now we can see an environment this nice sort of nice sky and Horizon and Lighting in our editor however it's just showing a preview of what a sky and a light setup might look like if I turn off these two buttons it turns off the environment and then it turns off the simulated default lighting you can see what the game level actually looks like to add our own light just a simple light for now into the game level I'll select my level one node here the root note of my scene and I'll press plus what I'm going to do here is find a light for my scene where is that well it's under node 3D it's under visual instance and I believe it is called where is it light 3D and I'm going to add a type of light called a directional light 3D if you just search for Direct and like that you'll find it and I can double click on it it'll add a light as a child a direct child of level one what this light will do is if I move it up and rotate it to point sort of downward but maybe diagonally downward is it'll light our entire world with a nice kind of even sunlight that reaches every corner of my Game World all at once it's kind of nice so if I drag this light object up if I select it and then move it straight up you can see it looks like a sun and if I rotate it to point downwards maybe not perfectly downwards maybe I'll rotate it a little bit on a couple directions to make it a nice diagonal light like that in fact I'm going to make it so that it kind kind of points away from the camera in my game level so that when my camera looks at my scene the light will from the light will be shining on the front of the objects from where the camera can see the object so the objects don't look like they're in Shadows so what I'll do here is I'll drag the light up and over a little bit just so it has a little bit of a top perspective as we're looking at it great let's go ahead and do a control s to save and I'll go ahead and press play scene to see how our scene looks now and now you can see we have some even Lighting in our environment to make some objects in this game level besides our ground we're actually going to do something a little bit different here you've seen in the first part of this video how we construct a 3D physical object in a game level we've made our floor when you make a physical object in Godot usually it's made out of three different nodes that you make into a little structure exactly as we have here the first note is often the physical type of object that that object exists as in your scene in this case we made our floor a static body 3D your static body though doesn't know what it looks like so another node that makes up your object should be how that type of object looks in this case a mesh instance of a stretched out cube lastly the physics object in this case the static body 3D needs to know its Collision bounds and for that we added a collision shape 3D so we have a nice little set of three nodes one parent two children to make an object however if we make all objects like this in our level one like if I were to make a falling block and I made a physical object with a mesh and a collision shape we could do that and build it in such a way that it actually falls with gravity right in this scene however if I want to take that same block object and add it into different levels well that would be trickier so what you actually do in Godot is you make every object that you want to reuse across different levels as their own separate scene to begin with so to make a falling block object we're going to go up this row of tabs where we create our first scene in the last lesson I'm going to press this new plus to add a new empty scene that's just going to be a block object that's reusable if I go back to level one of course I can still be in this scene and work on it but now I can switch scenes just like tabs in a web browser to make a following block we're not going to start off this scene with one of these default options we're going to create our falling block right away by pressing this plus and adding the type of physical object we want the object to start with in this case if I clear out my last search the type of node that a falling block is going to be is going to be one of those physics type of objects if I go under node 3D and then under if I can move my mouse to get this prompt out of the way if I go under Collision object 3D and then phys body 3D it turns out that unlike a static body 3D which do not fall with gravity on their own that there is a type of node called a rigid body 3D and if I read the description of this very quickly down here if I select it I can read a physics body which is moved by 3D physics simulation useful for objects that have gravity and can be pushed by other objects and that's exactly what I want so I'm going to select rigid body 3D you can search for rigid and find it pretty easily like that I'll go ahead and double click and that note not a node 3D like we started off our level with should be the root note so rigid body is the root node of this new scene not a node 3D to make our rigid body actually look like something I'm going to select the root note here and just like with our floor I'm going to press plus and I'm going to add a mesh instance 3D which I have down here in my recent lists here so I can just double click right there to add a mesh I don't have to search for it or find it in that node tree anymore it's in my recents list which is nice so I can select that mesh instance 3D I can go over to the inspector I can make it have a mesh I'll add a default oh let's add a cube mesh which is right here called box mesh and once I add that you'll see what it looks like if I turn on that fake or temporary lighting and environment you'll see how it look in a more proper level but again we don't have any lighting or any cameras in the scene at all and we won't okay so now I need to go back to my rigid body 3D and add one more thing it has that same warning from before it needs a collision shape 3D in order to work properly so I'm going to select the root node I'll press plus I'm going to go to my recents list here and select a collision shape 3D and double click on it just like that it has a warning of course the exact same is just a few minutes ago we need to give it a shape and so I'll select it I'll go over to the shape property in the inspector and I'll give it a new box shape 3D Collision shape just like that and because I didn't change the shape of my mesh my default Cube mesh and I'm not changing the shape of my Collision shape it will just work they're the same exact size by default isn't that handy that's it I think we're good if I save this scene it's not saved right now I'll press Ctrl s on my keyboard I can save this as rigidbody 3D no no I'm going to press cancel it's a good habit to get into to name your objects the root note of the objects to start I'm going to call this block with a capital B and I'll press enter there we go if I now save this scene control s Godot was smart enough to recommend the name that you give the root node in the scene to the actual scene file which ends with DOT tsen so block.tsen great it's in my res project folder which is actually my project folder and I'll just press save without changing anything so now I have a block scene and it's a rigid body object which should fall with gravity but it's not in my level how do I get this scene into this scene well what you do is you simply select the root node of your scene to add an object to it but instead of going up to plus to add new fresh default nodes what you do is you select the root node and then you press the link button this link button is called instantiate child scene if I press the link button it'll let me choose what other scene I Want To Make an instance of in my current level one scene in this case I will select block.tsen please never ever take your current scene and add an instance of itself into itself you don't want to do that so I'll select block not itself and with block selected I'll press open and now I have that falling Block in my game level of course I can move it up you'll notice that I didn't have to press in my block scene that group button like I did for the floor because this block is its own scene and I saved it as its own scene and now I'm making an instance of the Block in my level one scene I don't have to press that group together button because scenes automatically group themselves together when you make instances of them in another scene okay you might also notice a special little icon next to the block over here in the scene dock this little icon shows you that this is an instance of a different scene and if I didn't have the block scene open if I close this tab up here what I could always do if I want to edit the block ever is I can press this little button and it'll jump over back into the original scene where I can make changes save those changes Ctrl s and then go back to level one and it will affect all of the instances of this block in all of my game levels simply by editing the original one again so changes you make in one scene will propagate out to all the instances of that scene in all of your game levels so now I can take the block I can move it up and maybe I'll rotate it a little bit just to see what happens if I go ahead and press Ctrl s to save and then I'll go ahead and press play scene aha we can see it falls and kind of tumbles a little bit now you'll notice that our light does not have or it's not casting any Shadows which makes things hard to see so I'm going to fix that I'm going to select that directional light 3D and over in the inspector if I go to the shadow section of the properties I can enable Shadows to be on and now we see a shadow being cast by the cube on the floor and that will look especially good when I save control s and press play scene again because now I'll see a shadow underneath the block and it'll land and that'll look great can I make more blocks or more instances of that same Block in my level of course I can if I select this block I can simply right click on it and say duplicate but before I do that what I might do is change this block instances name to block one and then if I duplicate it Godot is smart enough to name the next one block two isn't that nice if I move this block up maybe I'll move it over a little bit maybe I'll rotate it I now have two blocks that might interact in interesting ways I think I'll move that one over like that and move it a little bit up let's go and press play scene to see what this looks like hey look at that some tumbling blocks what I'll do is take both blocks if I hold shift I can grab multiple blocks there I'll duplicate them both at the same time I'll then move them both up block three and four I'll rotate them and let's go ahead and save control s and I'll press play scene and now we have some nice falling blocks there we go okay so in this lesson we have learned how to construct 3D physical objects in our 3D environment for our game level we've learned how to construct a floor starting with a static body 3D object as well as using a simple 3D mesh object and a collision shape in doing so we learned about the inspector and different properties of each type of object in the inspector including how to make a resource Like A Primitive mesh or a primitive Collision shape for our Collision shape 3D and in this lesson we learn how to make reusable objects in their own scenes by making the entire object as we normally would in a different scene you can then reuse that object and make instances of it in different game levels and if you update or change the original scene and save it and go back to anywhere there's an instance of that type of object it will make changes to that object there I'll quickly do a demo of this if I go back to my block scene take the mesh instance go to it's mesh and expand out its mesh resources properties if I change the mesh to be only 0.5 meters tall and then I change the Collision shape to also be I'll expand out the box shape resources properties here and I'll type in the same 0.5 meters on the y-axis now that block is half as tall if I save this scene control s and then I go back to my level one scene you can see that now all the blocks have been updated to be that same exact size and shape and if I press play C well all of those new blocks fall in a different way okay that will be it for this one though thank you so much for watching I'll see you in the next one bye [Music]
Info
Channel: BornCG
Views: 40,822
Rating: undefined out of 5
Keywords: 3D, beginner, godot, engine, game, tutorial, lesson, floor, static, rigid, rigidbody, physics, falling, gravity
Id: zQRt94rmZGo
Channel Id: undefined
Length: 28min 7sec (1687 seconds)
Published: Thu Sep 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.