Topic was automatically imported from the old Question2Answer platform.
Asked By
hidemat
Trying to replicate a pong game. I got the ball and the paddles moving and I’m trying to work out collisions. Both the ball and the paddle are KinematicBody2D’s. I what the ball to bounce at different angles depending on where it hits the Paddle.
Essentially, I want the ball to bounce upwards when it it’s the top part of the paddle, downwards when it hits the bottom, and straight when it hits the middle. What do you guys think is the best approach?
But yeah, i remember it being confusing. Godot 3.2 is planning to have more user-friendly signals, though.
Thanks. I was trying with Area2d before. So what I did was assign 3 Area2d to the paddle itself which is a kinematic body. Not sure if this was the best approach since I couldn’t get it to work properly.
In the end I got it to work by checking the position of the ball relative to the position of the paddle. Here is the code in case someone is curious.
func _physics_process(delta):
var collision_info = move_and_collide(velocity * delta)
if collision_info:
velocity.x = velocity.x * -1 # Bounce
var p_pos = collision_info.collider.position
if position.y > p_pos.y + 25 and position.y < p_pos.y + 75:
velocity.y = abs(velocity.x)
elif position.y < p_pos.y - 25 and position.y > p_pos.y - 75:
velocity.y = -abs(velocity.x)
else:
velocity.y = 0
velocity = velocity.normalized() * speed