From 93eeb911b6491221de95924b19ca21c462e8d7c0 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Sun, 18 Feb 2024 14:50:17 +0100 Subject: Add pad speed to ball bounce vy --- game.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/game.js b/game.js index 8cf5e83..3bbfe30 100644 --- a/game.js +++ b/game.js @@ -261,26 +261,27 @@ function advancePhysics(deltaT) { ballVY = -ballVY; } - function bounceLeft(x, y, vx, vy, pady) { + function bounceLeft(x, y, vx, vy, pady, padvy) { if (x + vx * deltaT > padX + padWidth + ballRadius) return null; const t = (padX + padWidth + ballRadius - x) / vx; if (Math.abs((y + t * vy) - pady) >= padHeight / 2 + ballRadius) return null; const frac = ((y + t * vy) - pady) / (padHeight / 2 + ballRadius); - const angAdj = Math.PI / 4 * padHitAdjustCurve(frac); + const angAdj = Math.PI * 0.15 * padHitAdjustCurve(frac); const curAng = Math.atan2(vy, -vx); // normal outgoing angle const newAng = Math.max(-0.45 * Math.PI, Math.min(0.45 * Math.PI, curAng + angAdj)); const speed = Math.hypot(vx, vy); const newvx = speed * Math.cos(newAng); - const newvy = speed * Math.sin(newAng); + // TODO: not sure if simply adding of padvy to newvy is a good idea + const newvy = speed * Math.sin(newAng) + 0.1 * Math.abs(frac) * padvy; const newx = x + vx * t + newvx * (deltaT - t); const newy = y + vy * t + newvy * (deltaT - t); return [newx, newy, newvx, newvy]; } - let bounce = bounceLeft(ballX, ballY, ballVX, ballVY, padPos); + let bounce = bounceLeft(ballX, ballY, ballVX, ballVY, padPos, padVel); if (bounce != null) { newballX = bounce[0]; newballY = bounce[1]; @@ -289,7 +290,7 @@ function advancePhysics(deltaT) { ret.bounce = true; } - bounce = bounceLeft(1 - ballX, ballY, -ballVX, ballVY, pad2Pos); + bounce = bounceLeft(1 - ballX, ballY, -ballVX, ballVY, pad2Pos, pad2Vel); if (bounce != null) { newballX = 1 - bounce[0]; newballY = bounce[1]; -- cgit v1.2.3-54-g00ecf