diff options
| author | Tom Smeding <tom@tomsmeding.com> | 2024-02-18 14:50:17 +0100 | 
|---|---|---|
| committer | Tom Smeding <tom@tomsmeding.com> | 2024-02-18 14:50:17 +0100 | 
| commit | 93eeb911b6491221de95924b19ca21c462e8d7c0 (patch) | |
| tree | 63aa533241a6bf9750dc67b81604bfa9137bf5a3 | |
| parent | 177ea7d32b16a8fdb4697033b01901fc5ce4b1ad (diff) | |
Add pad speed to ball bounce vy
| -rw-r--r-- | game.js | 11 | 
1 files changed, 6 insertions, 5 deletions
| @@ -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]; | 
