Hello and welcome to another episode in the Godot basics tutorial series. In this episode we will be talking about collision shapes in Godot. Collision shapes defined the area of an object inside the physics engine. The collision shape 2D is the classic shadow uses in order to define a space in which the collision algorithm can work with when detecting. If Game Objects intersect or collide.
Now collision shapes by themselves are completely useless as a matter of fact a collision shape should be attached to a node in the same tree that inherits from the collision object to the class. In this case where keeping the scope of the episode to the realm of 2D now in this case nodes that inherit from the collision object 2D class is the area 2D and physics body 2D. I went ahead and attached a collision shape to Team node onto the seeing tree and as you can see here the Godot app is throwing us a warning.
Now if you were to click this you would get a pop up. That's basically telling you that collision shaped to be only provides an area and that in order to get the benefits of using a collision shape 2D we need to in fact attach it to basically an object or rather a node that inherits from the collision object 2D class. In this case it would be area to the aesthetic body rigid body can emphatic body and the list goes on. Now in this example I went ahead and I attached a collision shape to the node on 2.
In this case a rigid body 2D node and as you can see here no errors thrown. Keep in mind that in this picture example I have attached a shape to the collision shape 2D we'll go over that and on a different slide. One thing to keep in mind is that a collision shaped to Dean must be a direct child of your node. That's either an area to denote or a node that inherits from the physics body to the class. Now a collision shape to Dean needs a shape property the shape.
Property must be a shape to the object. However it cannot be the shape to the object itself it needs to be a shape object that inherits from the shape to D class. Now in this case when we have our collision shape to be attached to the same tree we should see some properties in the ED the first property which is also the most important property for the collision shape to be is the shape property the shape property is basically a shape 2D object and as you can see here by default it is empty.
It does not assign us a shape to the object we have to choose from amongst a list ourselves. And so when you click on this you are in fact given choices. Now if you don't know which choice to pick as a beginner there is nothing wrong with picking the rectangle shape to D object after you are free to choose from amongst many different types of shape objects and you are even free to load your own resource. You have to remember that the simpler your collision shape the more performance your game will be.
We talked a little bit about this in the last episode. However it's important to keep in mind that there are different algorithms for different shapes. When it comes to collision the more complex these shape the more complex your algorithm needs to be in order to detect collisions. So obviously simpler shapes outperform polygons. So you have to keep in mind that if you need to use polygons you need to have a purpose in doing so. Just make sure that the image you're using justifies the need for using polygons and poly.
Guns are great polygons. Give us the ability to shape complex looking images when our basic shapes such as squares or excuse me rectangles and circles cannot give us the desired area boundary. Let's go ahead and take a look at what a polygon is used for. So as you can see here are polygon shaped object is some complex looking object. We cannot get this from using circles and squares and lines for example. Ever notice that the Godot engine has partitioned our polygon object into basically three different shapes and so the algorithm is going to calculate under the hood a little differently than it would calculate.
For example collisions with a rectangle. So again make sure you have a purpose. When you start using polygons again you are free to use different shapes for different circumstances. However keep in mind that Godot is actually quite flexible when it comes to the finding shapes for different purposes. For example you can use complex shapes for a complex image sprite you have and you can use simple shapes for detecting objects next to a player using a simple shape for example. Maybe you want your 2D character to pick up objects when it's near another game object and just trying your best to make sure that you prefer simple shapes over complex shapes when dealing with the collision shape 2D is one way of keeping performance in mind by doing so by using simpler shapes you are in fact increasing performance.
So again when you have the opportunity to use a simpler shape. Go ahead and use that one thing to note is that your player can use multiple shapes for different animations. For example you may want to use a big rectangle box for your walking animation. And when your character is ducking or crouching you may want to use a smaller rectangle box for that animation and so forth. So on top of attaching multiple collisions shape 2D objects for your physics body we are given a property called disabled and we can basically turn it on and off.
This basically allows us to easily switch between different collision shapes for our game objects. One thing to keep in mind is that if you use multiple collision shaped 2D objects and none of them are disabled which means that they are on Godot will basically work with the union of all these shape objects that are attached to your physics body Tootie or area to the game node. Platforms are a big genre in the indie game development industry actually platforms are quite popular even for beginner programmers because they are fun to make.
Now Godot makes it easy for us to implement platforms. For example imagine the situation where you want your character to jump through an object but you don't want that character to fall through it. You can think of that as a hill and Super Mario for example. Well if you want that behavior Godot provides us a way through the collision shape to the property and that property is called a one way collision. By default it set off. Now if you were to turn this on what you're telling the collision shape is that when going from down to top you would like to go through the collision shape.
However when going from up to down you do not wish to fall through it. It also comes with a property that basically allows you to define the thickness of your one way collision object. The higher the value you assign to this property the more you're able to react to faster moving objects. Now you are free to change the size and pull session of a collision shape to it. You are free to change the size and position of a collision shape 2D Node. However whatever you do do not edit the scale value leave the scale property alone.
Speaking of changing the size collision shape to date doesn't have a size property. However it does have a property called extents extensive. Basically what you use to define the width and height of your game object. So to recap a collision shaped 2D needs a child of the shape to the object. The child of the shape to the object in this case rectangle 2D will give you extensive property that will define its width and height. However in the case of the rectangle 2D in order to get its specific pixel width and height you need to grab the extents value from the X or Y position and multiply it by 2.
Let's go ahead and take a look at that real quick. So here we have our collision shape. Today we've picked the rectangle 2D shape and as you can see here we have extents the extents on the x axis is sixty form the extents on the y axis is sixty four. Now if we want to play both values by 2 We're gonna get the what then height. In this case the width of our collision shape 2D is one hundred twenty eight the height of our collision shape 2D is 128 pixels. So just keep that in mind if you want to edit the width and height of your collision shape to see you're going to edit the extents property of your shape object which is attached to your collision shape 2D Node.
Well that's all I have for you in this episode. Thank you so much for joining me. Thank you for clicking the Like button and thank you for clicking the Subscribe button. If you have any questions or comments please feel free to leave them in the comments section down below. I look forward to seeing you in the next episode. Have an amazing day