diff options
author | P. J. McDermott <pj@pehjota.net> | 2021-03-27 17:48:12 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2021-03-27 17:48:12 (EDT) |
commit | 9d7f111b7d5d882a10f919fcc861efc48ceedff9 (patch) | |
tree | 41f009e499a2a7dc21b8393cc55d2a5b23e41306 | |
parent | f12dcd37e062f752e7a61569569b05956a37360d (diff) | |
download | dodge-balls-9d7f111b7d5d882a10f919fcc861efc48ceedff9.zip dodge-balls-9d7f111b7d5d882a10f919fcc861efc48ceedff9.tar.gz dodge-balls-9d7f111b7d5d882a10f919fcc861efc48ceedff9.tar.bz2 |
ball: Collide with walls
-rw-r--r-- | src/ball.c | 28 |
1 files changed, 28 insertions, 0 deletions
@@ -111,10 +111,38 @@ _db_ball_bounce(struct db_ball *ball, double col_x, double col_y) void db_balls_collisions(struct db_ball *ball) { + SDL_Rect wall; + int map_tw; + int map_th; + int x_min; + int x_max; + int y_min; + int y_max; + int x; + int y; struct db_ball *other; double col_x; double col_y; + wall.w = map_tw = db_map_get_tilewidth (ball->map); + wall.h = map_th = db_map_get_tileheight(ball->map); + x_min = floor((ball->x - ball->r) / map_tw); + 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); + 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)) { + wall.x = x * map_tw; + wall.y = y * map_th; + if (db_col_cir_rect(ball->x, ball->y, ball->r, + &wall, &col_x, &col_y)){ + _db_ball_bounce(ball, col_x, col_y); + } + } + } + } + /* Spinning balls shouldn't collide with any other balls */ if (ball->sr == 0) { for (other = ball->next; other != NULL; other = other->next) { |