Creating a Physics Based Ball | Making Your First Game in Godot 

With the main Pong Scene created and a player controlled paddle working, the next logical step is to start creating the ball.

Building the Bouncing Ball

Like most things we’ve discussed this far, the ball is also fairly basic. The biggest difference to what we’ve done so far is that we will be using a CharacterBody2D node as the foundational node for the ball. The CharacterBody2D has built in functions and interactions that will make building the ball logic significantly easier.

Rather than using a ColorRect to draw the ball, we will be using a TextureRect. The major advantage to this is that we can change the texture to any image file we may want to in the future. If we wanted to randomize the ball texture from an array of images we could do so. For right now however, we will set the texture to a placeholder texture giving us a nice magenta ball. This is then set to a 10px square to size the ball correctly and then centered in the screen. A collision shape is created for the ball using a similar 10px square.

Adding the Logic

The ball will have the most robust script out of all the game objects we create. We begin by creating three variables (window_size, speed, and direction) and two constants (START_SPEED and ACCELERATION). window_size and direction are set to be Vector2 while the rest are normal integers. Like we did for the PlayerPaddle, we use the _ready() function to define window_size.

Variables and _ready()

From here, we will define two new functions: spawn_ball() and set_direction(). The function for spawning the ball is a misnomer as we are really just resetting the position and speed of the ball to the original values and then restarting the game. The set_direction() function will use two random values to define the starting direction of the ball. We use a random integer of -1 or 1 to set the X Direction and then a random float to set the Y Direction. This vector is then scaled to a unit vector by using .normalized(). Right now the spawn_ball() function is never called.

New Ball Functions

Creating the Spawn timer

In our main Pong Scene we need to create a Timer (named BallSpawnTimer) that will serve as a cooldown timer before spawn_ball() is called. We can set the time for the timer, have it autostart, and be a one shot timer. Certain nodes (such as timers) have built in signals that we can connect to other scripts in the game. We will connect the timer’s timeout() signal to our main game script. This means that on BallSpawnTimer timout() that we will call the ball_spawn() function.

Calling Function on Timeout

Handling Ball Collision and Bouncing

If we ran the game right now, the ball immediately stops when it collides with the player’s paddle or the boundaries. To fix this, we add the _physics_process() function to our ball’s script. Inside of this, we define a local variable for collision setting it equal to our move_and_collide result. If a collision is detected we can use a built in bounce() function to bounce off in the direction of the collision’s normal. We can also check if the collision’s collider is a player’s paddle and accelerate the ball.

_physics_process() Function Handling Collisions