From fe344c40e81d81cd9c1c0832248c1c49b2ce715a Mon Sep 17 00:00:00 2001 From: Quinn <99677023+thepigeongenerator@users.noreply.github.com> Date: Wed, 14 Aug 2024 16:08:15 +0200 Subject: [PATCH] fix ball collisions to be more accurate --- src/level.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/level.c b/src/level.c index a716d34..c8a3578 100644 --- a/src/level.c +++ b/src/level.c @@ -102,6 +102,7 @@ void level_update(Level* level, const Uint8* keys) { } // check brick collisions + bool collided = false; for (int x = 0; x < BRICK_COLUMNS; x++) { for (int y = 0; y < BRICK_ROWS; y++) { const Brick* brick = &level->bricks[x][y]; @@ -115,19 +116,29 @@ void level_update(Level* level, const Uint8* keys) { if (ball->pos.x < max_brick_x && (ball->pos.x + ball->size) > brick->pos.x && ball->pos.y < max_brick_y && (ball->pos.y + ball->size) > brick->pos.y) { - // manage ball bounce direction - if (ball->direction.x > ball->direction.y) { - ball->direction.x *= -1; + float ball_abs_dir_x = abs(ball->direction.x); + float ball_abs_dir_y = abs(ball->direction.y); + + level->bricks[x][y].destroyed = true; + + // skip changing direction of we already did + if (collided == true) { + continue; } - if (ball->direction.x < ball->direction.y) { + + // manage ball bounce direction + if (ball_abs_dir_x < ball_abs_dir_y) { ball->direction.y *= -1; } + else if (ball_abs_dir_x > ball_abs_dir_y) { + ball->direction.x *= -1; + } else { ball->direction.x *= -1; ball->direction.y *= -1; } - level->bricks[x][y].destroyed = true; + collided = true; } } }