Éviter que la balle aille trop horizontal

This commit is contained in:
François Pelletier 2024-09-01 01:10:57 -04:00
parent bbeec79627
commit b41f555f59

View file

@ -116,7 +116,7 @@ function update() {
// Bounce off top wall
if (ball.y + ball.dy < ball.radius) {
ball.dy = -ball.dy;
} else if (ball.y + ball.dy > floorY - ball.radius) { // Changed to use floorY
} else if (ball.y + ball.dy > floorY - paddle.height) { // Changed to use floorY
if (ball.x > paddle.x && ball.x < paddle.x + paddle.width) {
// Calculate where the ball hit the paddle
let hitPos = (ball.x - paddle.x) / paddle.width;
@ -124,8 +124,15 @@ function update() {
// Change angle based on where the ball hit the paddle
let angle = hitPos * Math.PI - Math.PI/2;
// Add some randomness to the angle
angle += (Math.random() - 0.5) * Math.PI/4;
// Limit the angle to a maximum of 45 degrees (pi/4 radians) from vertical
const maxAngle = Math.PI / 4;
angle = Math.max(-maxAngle, Math.min(maxAngle, angle));
// Add some randomness to the angle (within the limited range)
angle += (Math.random() - 0.5) * Math.PI/12;
// Ensure the angle stays within the maximum limits after randomness
angle = Math.max(-maxAngle, Math.min(maxAngle, angle));
// Set new velocity based on this angle
let speed = Math.sqrt(ball.dx * ball.dx + ball.dy * ball.dy);