Create a Catch The Egg game in Godot Engine – 3

This is the third part in the tutorial series, Catch The Egg game in Godot engine. In the previous parts, we setup the project and added the game bg. In this part, we are going to create the ground and the egg.

Setup the ground

Our current node setup looks like this.

Godot engine has 2D and 3D physics built in so that our game can make use of them. So when our egg is dropped to ground, the engine calculates the physical response and the object behaves like in the real world. Let us familiarize the two types of physics bodies we are going to use in this game.

StaticBody2D – This, as the name implies, is a static physics body. Whenever we want something like a ground, i.e., objects which is not going to move on applying external physical forces, we use this. If the egg is dropped on the ground, the ground is not going to move.

RigidBody2D – This is the real physical object which reacts and responds to every physical force. So our egg is going to be a RigidBody2D. So when it is dropped on the ground, it will collide.

Click on the ‘main’ node. Then click the Add Node button and add a StaticBody2D node. We don’t want any graphic for ground because the ground is included in the bg itself. If we wanted a graphic for this body, then we would add it as a child of the StaticBody2D.

Always remember, if we are using a physics body, then its sprite should be the child of the body node. This makes the art to follow the parent physical body reactions.

Back to the ground, rename the StaticBody2D to ‘ground’. We’ve added the ground body but how Godot engine is going to know where to collide, or the shape of the ground. For that, we have to define the shape of the ground, in our case it is a rectangle shape. So let’s add another node, CollisionShape2D as the child of the ground which is going to define the collision shape of our ground.

Also Read:   Resolution Switcher Plugin for Godot

On the inspector panel, there is a dropdown option for Shape, select RectangleShape2D.
collision shapeNow click on the ground node and select the move tool. We can also tap the ‘w’ on our keyboard to select the move tool. move tool

Using the move tool, we can drag the ground and position it somewhere at the middle bottom.

ground collision shape
Moving the ground collision shape (Please dont mind the cursor disappearance)

Click on the RectangleShape2D node. You can see two dots on the top and right side on the editor. Click and drag them to set the bounds properly.

Editing the ground collision bounds

Done!! We’ve set up the ground.

Creating the Egg

The process is same as above but instead of StaticBody2D we will choose RigidBody2D. So let’s start creating the egg.

Add a new RigidBody2D node as the child of the ‘main’ node. Rename it to ‘egg’. Add a ‘Sprite’ node as the child of the ‘egg’ node. Set the texture to our egg image. Add another CollisionShape2D as a child of the ‘egg’ node. Set the shape as we did for the ground. After all this, it will look like this.

Egg collision shape

Using the move tool, move the egg node to the top middle of our game world. Remember that, don’t move the child objects of a parent node unless it is really wanted to be offsetted. When we refer them in script, the ‘egg’ will be on 0,0 but the real egg sprite will be somewhere else if we move it. So to prevent that problem don’t move child objects unless it is really needed, instead move the parent itself. It was because of this we moved down the ground node earlier. I hope you understood this, if not post a comment so that I can help.

Also Read:   Creating an html5 game like concentration

If we press play, we can see the egg drops on the ground.

egg drop down preview
(Again, don’t mind the color bleeding, I want to find another GIF capture application)

If we want to increase the gravity by going to Scene > Project Settings and choose physics2d category. Change the default gravity to 980 and see the difference.

That concludes this part and we will create the collection basket on the next part.

[Total: 13    Average: 2.7/5]