Godot Version
4.6Beta 2
Question
Hi
I’ve spent almost 2 weeks trying to solve a collision issue in my ping pong game, whenever the ball hits the paddle at the edges and the paddle moves, the balls collisions get messed up as shown in the video. I’ve tried everthing from different collision shapes to different ways to disable the collisions on impact, even to moving the ball’s global position on impact. But alas nothing worked, other than that, the collisions work fine when the ball hits the paddle’s face.
This is the ball code:
extends CharacterBody2D
var collided = false
const BALL_SPEED = 400
func _ready():
velocity = Vector2(-0.5,0.2).normalized() * BALL_SPEED
func _physics_process(delta: float) -> void:
var collision = move_and_collide(velocity * delta)
if collision:
var normal = collision.get_normal()
#$AudioStreamPlayer.play()
#print("ball collide")
print(normal)
velocity = velocity.bounce(normal)
collided = true
global_position += normal * 2.0
start_colliding()
func start_colliding():
#print("starting collision")
await get_tree().create_timer(0.2).timeout
#print("ended")
collided = false
$CollisionShape2D.set_deferred("disabled", true)
and this is the paddle’s:
extends CharacterBody2D
var speed = 10
var accleration = 10
var is_rebounding = false
func _physics_process(delta: float) -> void:
if is_rebounding == false:
if Input.is_action_pressed("go_down"):
velocity.y += speed * accleration
if Input.is_action_pressed("go_up"):
velocity.y -= speed * accleration
move_and_slide()
# Slide collision + normal + reboundation
for i in get_slide_collision_count():
var collision = get_slide_collision(i)
var normal = collision.get_normal()
print("I collided with ", collision.get_collider().name)
#print(normal)
if collision.get_collider().name == "TopWall":
#print("top rebound")
velocity = 300 * normal
is_rebounding = true
start_rebound()
elif collision.get_collider().name == "BottomWall":
#print("bot rebound")
velocity = 300 * normal
is_rebounding = true
start_rebound()
func start_rebound():
#print("starting rebound")
await get_tree().create_timer(0.2).timeout
#print("ended")
is_rebounding = false