Added continuous reflection on top and lower border

This commit is contained in:
Michael Chen 2022-11-04 12:04:05 +01:00
parent f728240db9
commit cd7a1f15a9
Signed by: cnml
GPG Key ID: 5845BF3F82D5F629

View File

@ -79,9 +79,15 @@ public struct PongGameState {
state.Pos.X += BALL_SPEED * dx;
state.Pos.Y -= BALL_SPEED * dy;
if (state.Pos.Y < BALL_RADIUS
|| state.Pos.Y > HEIGHT - BALL_RADIUS)
// If reflected from lower or upper border calculate continuous reflection
// This prevents the ball from temporarily glitching into paddles
if (state.Pos.Y < BALL_RADIUS) {
state.Pos.Y = 2 * BALL_RADIUS - state.Pos.Y;
state.BallAngle = 2 * MathF.PI - state.BallAngle;
} else if (state.Pos.Y > HEIGHT - BALL_RADIUS) {
state.Pos.Y = 2 * (HEIGHT - BALL_RADIUS) - state.Pos.Y;
state.BallAngle = 2 * MathF.PI - state.BallAngle;
}
}
public RectangleF GetCollider() => new(Pos.X - BALL_RADIUS, Pos.Y - BALL_RADIUS, 2 * BALL_RADIUS, 2 * BALL_RADIUS);