Reputation: 369
I'm working on a game of Pong and I've finally figured out how to bounce the ball off the paddle but when it hits the paddle, the ball bounces back in a straight line. Even when I hit the ball on the top or bottom tips of the paddle it goes straight back in the opposite direction. This makes for a boring game of Pong as I'm attempting to make the ball bounce back so the player will have to figure out where exactly the ball will land.
Here's my Paddle
code.
extends CharacterBody2D
var speed: int = 400
func _physics_process(delta):
var direction = Input.get_axis("p1_up", "p1_down")
velocity.y = direction * speed * delta
var collision = move_and_collide(velocity)
And my Ball
code
extends CharacterBody2D
var speed: int = 14
func _ready():
var target_position = Vector2(0, 350)
var direction = global_position.direction_to(target_position)
velocity = direction.normalized() * speed
func _physics_process(delta):
var collision = move_and_collide(velocity)
if collision != null:
velocity = velocity.bounce(collision.get_normal())
And finally, my Main
code
extends Node2D
func _ready():
$Label.text = str(0)
$Label2.text = str(0)
func _physics_process(delta):
if $RayCast2D.is_colliding():
Globals.p2_score += 1
$Label2.text = str(Globals.p2_score)
if $RayCast2D2.is_colliding():
Globals.p1_score += 1
$Label.text = str(Globals.p1_score)
Upvotes: 0
Views: 85
Reputation: 653
I suspect the ball is at a right angle with the paddle CollisionShape2D
which is why it's going back in a straight line, If that is the case then the position at which the ball collides i.e. top or bottom is irrelevant.
I would recommend 2 things:
Rotate the CollisionShape2D
or giving the player the input option to rotate the paddle while in game
Take into account the velocity vector of the paddle and "bounce back" with respect to the ball's own velocity vector i.e. the speed of the paddle should also determine it's angle of reflection
Upvotes: 1