Răsfoiți Sursa

Add light gravity to the ball

A ball moving almost horizontally could bounce between the side walls
for a long time before coming back to the paddle. Gravity of 250 px/s²
curves it back down. In simulated games with a perfect paddle, the
longest wait between paddle hits fell from about 25 s to 7-12 s, and
every level was still won.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Gabriel Capella 5 zile în urmă
părinte
comite
065e6d91c1
2 a modificat fișierele cu 4 adăugiri și 1 ștergeri
  1. 3 1
      scripts/ball.gd
  2. 1 0
      scripts/params.gd

+ 3 - 1
scripts/ball.gd

@@ -1,6 +1,7 @@
 class_name Ball
 extends CharacterBody2D
-## A ball that bounces off everything and speeds up slightly on each bounce.
+## A ball that bounces off everything, speeds up slightly on each bounce and falls under
+## light gravity.
 
 ## Emitted for every contact, so the game can react to what the ball touched.
 signal hit(ball: Ball, other: Node)
@@ -18,6 +19,7 @@ func throw() -> void:
 
 
 func _physics_process(delta: float) -> void:
+	velocity.y += Params.BALL_GRAVITY * delta
 	var motion := velocity * delta
 	for i in 4:
 		var collision := move_and_collide(motion)

+ 1 - 0
scripts/params.gd

@@ -6,6 +6,7 @@ const BALL_RADIUS := 15.0
 const THROW_ANGLE := 20.0 # degrees either side of straight up
 const INITIAL_SPEED := 500.0
 const BALL_BOUNCE := 1.005 # every bounce speeds the ball up a little
+const BALL_GRAVITY := 250.0 # px/s², pulls sideways-bouncing balls back to the paddle
 
 # Core
 const CORE_RADIUS := 50.0