Added continuous reflection on top and lower border
This commit is contained in:
parent
f728240db9
commit
cd7a1f15a9
@ -79,9 +79,15 @@ public struct PongGameState {
|
|||||||
state.Pos.X += BALL_SPEED * dx;
|
state.Pos.X += BALL_SPEED * dx;
|
||||||
state.Pos.Y -= BALL_SPEED * dy;
|
state.Pos.Y -= BALL_SPEED * dy;
|
||||||
|
|
||||||
if (state.Pos.Y < BALL_RADIUS
|
// If reflected from lower or upper border calculate continuous reflection
|
||||||
|| state.Pos.Y > HEIGHT - BALL_RADIUS)
|
// 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;
|
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);
|
public RectangleF GetCollider() => new(Pos.X - BALL_RADIUS, Pos.Y - BALL_RADIUS, 2 * BALL_RADIUS, 2 * BALL_RADIUS);
|
||||||
|
Loading…
Reference in New Issue
Block a user