Training Project at Godot - Pong (Part 2) Creating and Setting Up the Ball

I greet you, Khabrovites! In this part, I will create and customize the ball for the Pong game. If you missed the beginning, then it is here . The article level is still a beginner.







Under the cut, there are still many screenshots.

Call the children and welcome under cat.







For editing, the project is opened by double-clicking on the name of the project or by the "Edit" button.













When you restart the engine, the previously created scene did not open. You can open it by double-clicking on it in the "File System" panel, the analogue panel in Unity is called Project.







Now you can create the ball, the correct order for this engine, the physical body and its two heirs: the collider and the graphics. But I will do it in a different order, at the same time and tell you why this order of nodes is needed.







The first thing I want to do with the ball is to see it. Therefore, I will start with the graphics. I can draw a 3 by 3 pixel white square in any image editor. However, I do not like to do in a third-party program, what I can do right in the engine.







To add a node to the scene, click on the plus in the scene panel. (Of course, it is possible to use the context menu or hot keys)













A huge list of various types of nodes appears in the window that opens. They all need to be viewed, ideally read about each of the nodes in the documentation , or at least view all the nodes directly in the list by reading the description of each of them.







This will help to better understand what nodes are in the engine and what to do with them. And for our current goal, the Polygon2D node is suitable.













By default, all nodes are created as children relative to the root node. If you need to create a child node for a specific node, you should create it by first selecting the parent node. Obviously, this is not necessary with the root node.







It's time to pay attention to the right side of the window, there is an inspector panel.













For the smallest

A square is a geometric figure consisting of 4 angles and 4 sides, with it all sides being equal to each other.







The size of the polygon determines the number of angles, after which a list of points appears, the coordinates of which should be entered, personally I prefer to start from the upper left corner and move clockwise around the points. Outlining a figure around the center. By default, in Godot, the center of all nodes is the upper left corner of the scene. And the introduction of fractions of a pixel is allowed.













Now you can set up “Transform” and position the ball in the center of the scene.

To see it better, you need to slightly increase the game scene in the main part of the window.

Scrolling the scroller on the mouse allows you to enlarge and reduce the scene, and clicking on the scroller and moving the mouse moves the scene.













Now you need to make the ball move, and there are two main ways:







  1. You can change the “Transform” setting, and move the ball in the same way as I moved it to the center of the stage but, using the program code, and smoothly.
  2. And you can add to this graph a node of a physical element such is called "Solid" or "Physical Object". And move the effect of the physical engine on this object.


The physical engine allows you to use the most accurate, smooth and believable movements without the need to describe all complex calculations yourself.







In addition, in addition to the movement of the ball, we still need to determine whether the ball has encountered the border of the screen? If faced, which border? Did the ball hit a racket? If faced with which of the rackets and which part of it? But these things are easiest to verify using a physical engine.







In general, there is a very simple scheme for this choice. If all the objects in the game move along the grid and there is a movement queue. For example, in chess, a black player does not start his turn before the white player completes his move. And none of the players has the right to move several pieces at once.







In such games, it is convenient to directly change the "Transform" settings.

For all other cases, there is a physics engine.







Now, I think it’s obvious that it’s more convenient to use the physics engine in the Pong game.







To do this, you need to the ball, now this is the node "Polygon2D" add the node "PhysicsBody2D"













But there are three such nodes: Kinematic, Rigid and Static. According to the description:







  1. Kinematic is a physical object moving according to the written program code, not obeying the physical engine. (Our option)
  2. Rigid is a physical object completely subordinate to the physical engine.
  3. Static is a motionless physical object.


Analogs of these nodes are available in all game engines I know, and in Unity they have the same names and properties.







After adding the "KinematicBody2D" node, an error notification appears.













Moving the cursor over it, it becomes clear that the cause of the error is the lack of a collider mask for this node. In this case, I was offered a choice of one of two options: CollisionShape2D or CollisionPoligon2D.







I already tried to create a polygon, now I want to look at the form. Which when creating reports another error. This time, she says that the mask seems to be there, but no one created the shape of the mask.







It is created in the "Inspector" panel. For our square, the "RectangleShape2D" shape is suitable with the following settings.













Now you can start moving the ball around the screen but, with the physical engine you can only move the “Physical body” with all the heirs and it will move relative to its parent. In this case, the body along with the collider will move relative to the square drawn on the stage, which will remain stationary on the screen. The fix is ​​very simple:























Now moving the physical body, I will move the collision mask along with the drawn square.







The ball is ready to move, next time we will finally launch it and see how it will look in the game.







I will be glad for your criticism in the comments.








All Articles