make ball more reliable

This commit is contained in:
Quinn
2024-08-14 12:56:08 +02:00
parent b25db9ff0c
commit c8baf5dcc3
2 changed files with 4 additions and 4 deletions

View File

@@ -3,5 +3,4 @@ I wanted a project to learn C and the SDL framework in, and settled upon this ga
### TODO: ### TODO:
- make a better input system (one that doesn't crash the game) - make a better input system (one that doesn't crash the game)
- make the bouncer collisions more reliable
- make beeping sound effects - make beeping sound effects

View File

@@ -91,10 +91,11 @@ void level_update(Level* level, bool* keys) {
// check bouncer collisions // check bouncer collisions
if ((ball->pos.x + ball->size) > bouncer->pos.x && ball->pos.x < (bouncer->pos.x + bouncer->width) && if ((ball->pos.x + ball->size) > bouncer->pos.x && ball->pos.x < (bouncer->pos.x + bouncer->width) &&
(ball->pos.y + ball->size) > bouncer->pos.y && ball->pos.y < (bouncer->pos.y + BOUNCER_HEIGHT)) { (ball->pos.y + ball->size) > bouncer->pos.y && ball->pos.y < (bouncer->pos.y + BOUNCER_HEIGHT)) {
float x = ball->pos.x - bouncer->pos.x + (ball->size / 2); float x = ball->pos.x - bouncer->pos.x + (ball->size / 2); // get the X axis relative to the bouncer
unsigned max = bouncer->width; unsigned max = bouncer->width + 2; // get the maxiumum of this X axis (add 2 to make it feel more accurate)
float angle = (x - (0.5F * max)) / max * PI; float angle = (x - (max / 2.0F)) / max * PI; // calculate the angle in radians where the bouncer X axis falls on -(pi/2) to pi/2
// change the ball direction
ball->direction.x = SDL_sinf(angle) * BALL_SPEED; ball->direction.x = SDL_sinf(angle) * BALL_SPEED;
ball->direction.y = -SDL_cosf(angle) * BALL_SPEED; ball->direction.y = -SDL_cosf(angle) * BALL_SPEED;
} }