feat: Add collisions with paddle

This commit is contained in:
sigmasternchen 2024-11-01 19:15:09 +01:00
parent e357656d00
commit e864112755
2 changed files with 14 additions and 0 deletions

View file

@ -58,6 +58,7 @@ export class Game {
private readonly handleCollisions = (ball: Ball): void => {
this.handleEdgeCollisions(ball);
this.paddle.handleCollisions(ball);
}
private readonly handleEdgeCollisions = (ball: Ball): void => {

View file

@ -59,4 +59,17 @@ export class Paddle {
this.ball.setup(gameElement);
}
}
public readonly handleCollisions = (ball: Ball): void => {
if (
ball.position.y + ballSize / 2 > paddleY &&
ball.position.x + ballSize / 2 > this.position - this.size / 2 &&
ball.position.x - ballSize / 2 < this.position + this.size / 2
) {
const t = (ball.position.x - this.position + this.size / 2) / this.size;
const phi = (t - 0.5) * -2 * Math.PI / 8;
ball.collision(phi);
}
}
}