Collision in Pong [BEGINNER]

Godot Version

4

Question

I’ve picked up Godot a few days a go as a small project while university is on a break and I decided to make Pong as a first game and I’m currently stuck on collision.

extends Area2D

@export var ballspeed = 100
var screen_size = get_viewport_rect().size
@export var direction = Vector2(randf_range(-PI, PI), randf_range(-PI, PI))
var velocity = Vector2.ZERO
# Called when the node enters the scene tree for the first time.
func _ready():
	position.x = 576
	position.y = 324


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	velocity = direction
	velocity = velocity.normalized()
	velocity = velocity * ballspeed
	position += ballspeed * direction * delta


func _on_top_body_entered(body):
	print("top")
	direction.y *= -1

Yes, I know the code itself is a bit of a mess and the speed of the ball isn’t working as intended right now.


2D view - I’m not yet allowed to add two pictures

Whenever the ball touches the top, the function at the end of the code doesn’t trigger.
If anyone maybe found a post that answers it (I searched but didn’t) or can take some time to answer it, It’d be extremely helpful!

Looks like you are using Area2D for all objects in the game. In that case, connect the signal area_entered, not body_entered. There are no bodies to collide.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.