Add angle variation based on hit position

This commit is contained in:
shylie 2025-06-05 08:33:09 -04:00
parent decb27cbdc
commit 875a25e428

View File

@ -120,22 +120,48 @@ int main()
player.y += 2 * right_encoder_delta;
float diff = ball.middle_v() - ai.middle_v();
ai.y += 1.4f * diff * dt;
ai.y += 1.6f * diff * dt;
ball.x += ball_dx * dt * ball_speed;
ball.y += ball_dy * dt * ball_speed;
if (ball.overlaps(player))
const rect player_top = { player.left(), player.top(), player.w - 2, 1 };
const rect player_bottom = { player.left(), player.bottom() - 1, player.w - 2, 1 };
if (ball.overlaps(player_top) || ball.overlaps(player_bottom))
{
ball_dy = -ball_dy;
}
else if (ball.overlaps(player))
{
ball_dx = -ball_dx;
ball.x = player.right();
ball_speed += 1.0f;
float angle = atan2f(ball_dy, ball_dx);
float angle_delta = (ball.middle_v() - player.middle_v()) / 30.0f;
ball_dx = cosf(angle + angle_delta);
ball_dy = sinf(angle + angle_delta);
}
if (ball.overlaps(ai))
const rect ai_top = { ai.left() + 2, ai.top(), ai.w - 2, 1 };
const rect ai_bottom = { ai.left() + 2, ai.bottom() - 1, ai.w - 2, 1 };
if (ball.overlaps(ai_top) || ball.overlaps(ai_bottom))
{
ball_dy = -ball_dy;
}
else if (ball.overlaps(ai))
{
ball_dx = -ball_dx;
ball.x = ai.left() - ball.w;
ball_speed += 1.0f;
float angle = atan2f(ball_dy, ball_dx);
// ball second since the paddle is facing the other way
float angle_delta = (ai.middle_v() - ball.middle_v()) / 30.0f;
ball_dx = cosf(angle + angle_delta);
ball_dy = sinf(angle + angle_delta);
}
if (ball.left() < 0)