Godot Version
v4.7.dev4.official [755fa449c]
Question
I've decided to go back to basics, and have been inspired to do the first suggested Project in the 'Godot Beginner Launchpad, a 'Pong' game. Although fairly at ease with 3D, this is my first 2D game. I managed to create a Pong-type screen, with left and right Walls, a Ceiling and a Floor. These are passive Area2D's with suitable CollisionShape2D's. I introduced a CharacterBody2D Ball, with a Collision Shape, which I got to move, bouncing around off the Walls, Floor and Ceiling. I then added a Left, then Right, Paddle, movement controlled by keyboard, and the Ball bounces off of these quite happily. It's looking to be successful up to now, but then comes the surprise...
Occasionally, when the Ball collides with the top or bottom of a Paddle, the Paddle 'sticks' to the Ball, which bounces, and travels through the Floor or Ceiling, taking the Paddle with it, lost for ever..! I can find no explanation for this behaviour; what is it that allows this to happen, and how may it be prevented..?
I have been unable t capture a 'real' screenshot of this anomaly, so I constructed a simulation; the Red Arrow indicates the path of the Ball and Paddle, which will ignore the Floor and disappear.
Here's the code for the (very few...) elements in the Project, and a series of Screenshots showing the Node Tree and Inspectors of these items. Any help would be greatly appreciate. Apologies for the long post; thanks in advance for explanations offered.
extends Node # GLOBAL VARIANTS
var ball_speed = 500
var ball_angle = 400
var ball_to_right : bool = true
var paddle_l_speed = 500
var paddle_r_speed = 500
extends CharacterBody2D # BALL
func _physics_process(_delta):
if Globalvariants.ball_to_right == true:
velocity.x = Globalvariants.ball_speed
velocity.y = Globalvariants.ball_angle
else:
velocity.x = -Globalvariants.ball_speed
velocity.y = -Globalvariants.ball_angle
move_and_slide()
extends CharacterBody2D # PADDLE_L
func _draw():
draw_line(Vector2(0,50),Vector2(0,-50),Color.WHITE,10.0,false)
func _physics_process(_delta):
velocity.y = 0
velocity.x = 0
var up_l = Input.is_action_pressed('player_1_up')
var down_l = Input.is_action_pressed('player_1_down')
if up_l:
velocity.y -= Globalvariants.paddle_l_speed
if self.position.y <= 66:
velocity.y = 0
if down_l:
velocity.y += Globalvariants.paddle_l_speed
if self.position.y >= 566:
velocity.y = 0
move_and_slide()
velocity.x = 0
for i in get_slide_collision_count():
var _collision = get_slide_collision(i)
Globalvariants.ball_to_right = true
if Globalvariants.ball_angle == -400:
Globalvariants.ball_angle = 400 - (velocity.y + randi()%50)
else:
Globalvariants.ball_angle = -400 + (velocity.y + randi()%50
extends CharacterBody2D # PADDLE_R
func _draw():
draw_line(Vector2(0,50),Vector2(0,-50),Color.WHITE,10.0,false)
func _physics_process(_delta):
velocity.y = 0
velocity.x = 0
var up_r = Input.is_action_pressed('player_2_up')
var down_r = Input.is_action_pressed('player_2_down')
if up_r:
velocity.y -= Globalvariants.paddle_r_speed
if self.position.y <= 66:
velocity.y = 0
if down_r:
velocity.y += Globalvariants.paddle_r_speed
if self.position.y >= 566:
velocity.y = 0
move_and_slide()
velocity.x = 0
for i in get_slide_collision_count():
var _collision = get_slide_collision(i)
Globalvariants.ball_to_right = false
if Globalvariants.ball_angle == -400:
Globalvariants.ball_angle = 400 - (velocity.y + randi()%50)
else:
Globalvariants.ball_angle = -400 + (velocity.y + randi()%50)
Ball CollisionShape2D…
Paddle_L CollisionShape2D…
Paddle_R CollisionShape2D…





