Godot Version
4.2.2
Explanation
Hello everyone, I have made my version of PONG using Godot 4 as a way to learn the engine. Currently, the game is 99% complete except for one bug that I can’t figure out how to solve it.
I won’t waste your time explaining how PONG works in general but, basically, the problem I have is when there is the collision of the ball between the wall and the paddle, especially the latter when it’s near the wall, the ball just goes outside the play area.
All three objects, paddles, ball and walls (top and bottom) are all Area2D nodes with a CollisionShape2D node as a child. Both the paddles and the ball have a RectangleShape2D as the shape for the collision, while the walls have a WorldBoundaryShape2D as the shape for the collision detection.
Also, those are the scripts I have written for the collision detection
- Wall
func _on_area_entered(area):
if area is Ball:
# Increase the speed of ball
area._speed += randi_range(15, 20)
# Change direction
area._direction.y = -area._direction.y
- Paddle
func _on_area_entered(area):
# If the ball is entered
if area is Ball:
# Increase the speed
area._speed += randi_range(25, 50)
# Get the difference in position of the two objects
var pos_diff = (area.global_position - self.global_position).normalized().y
# Change the direction
area._direction = Vector2(-area._direction.x, pos_diff)
If you want to give a look to the source code the game is open source and can be easily downloaded without any additional stuff to add into the folder project.
If you want to check this bug, here is the link to the game
Question
How can I solve this issue? Should I change something in the code?
I know the existence of a pong demo made already in Godot but, I’ll be honest, I don’t like that if the ball goes out of bounds it simply resets.