summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2021-03-28 10:58:21 (EDT)
committer P. J. McDermott <pj@pehjota.net>2021-03-29 23:30:32 (EDT)
commit6fa5445fc076d7af82318c738dbd15a20f75ae56 (patch)
tree4877be875def2680aeaf054e34bf79c3176f514c
parent864e053487967faa7dd51709df1869d87544b64f (diff)
downloaddodge-balls-6fa5445fc076d7af82318c738dbd15a20f75ae56.zip
dodge-balls-6fa5445fc076d7af82318c738dbd15a20f75ae56.tar.gz
dodge-balls-6fa5445fc076d7af82318c738dbd15a20f75ae56.tar.bz2
ball: Bounce off one wall per frame
-rw-r--r--src/ball.c32
1 files changed, 6 insertions, 26 deletions
diff --git a/src/ball.c b/src/ball.c
index d4c27a4..552aadd 100644
--- a/src/ball.c
+++ b/src/ball.c
@@ -118,8 +118,7 @@ db_balls_collisions(struct db_ball *ball)
int x_max;
int y_min;
int y_max;
- int prev_col_x[2];
- int prev_col_y[2];
+ SDL_bool bounce;
int x;
int y;
double col_x;
@@ -132,10 +131,7 @@ db_balls_collisions(struct db_ball *ball)
x_max = ceil ((ball->x + ball->r) / map_tw);
y_min = floor((ball->y - ball->r) / map_th);
y_max = ceil ((ball->y + ball->r) / map_th);
- prev_col_x[0] = -1;
- prev_col_x[1] = -1;
- prev_col_y[0] = -1;
- prev_col_y[1] = -1;
+ bounce = SDL_FALSE;
for (y = y_min; y < y_max; ++y) {
for (x = x_min; x < x_max; ++x) {
if (db_map_tile_ball_collides(ball->map, x, y)) {
@@ -143,30 +139,14 @@ db_balls_collisions(struct db_ball *ball)
wall.y = y * map_th;
if (db_col_cir_rect(ball->x, ball->y, ball->r,
&wall, &col_x, &col_y)){
- if (x == prev_col_x[0]) {
- continue;
- } else if (x == prev_col_x[1]) {
- continue;
- } else if (y == prev_col_y[0]) {
- continue;
- } else if (y == prev_col_y[1]) {
- continue;
- }
- _db_ball_bounce(ball, col_x, col_y);
- if (prev_col_x[0] >= 0) {
- prev_col_x[0] = x;
- } else {
- prev_col_x[1] = x;
- }
- if (prev_col_y[0] >= 0) {
- prev_col_y[0] = y;
- } else {
- prev_col_y[1] = y;
- }
+ bounce = SDL_TRUE;
}
}
}
}
+ if (bounce) {
+ _db_ball_bounce(ball, col_x, col_y);
+ }
/* Spinning balls shouldn't collide with any other balls */
if (ball->sr == 0) {