summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2021-03-30 03:16:19 (EDT)
committer P. J. McDermott <pj@pehjota.net>2021-03-30 03:16:19 (EDT)
commit2b5fd6f394ee99fcd33384467cfe427bd737bd2d (patch)
tree400404a48e65a3628928ec11580b56f41feaa329
parent8062ca04f1c007359badf63352c320067a608252 (diff)
downloaddodge-balls-2b5fd6f394ee99fcd33384467cfe427bd737bd2d.zip
dodge-balls-2b5fd6f394ee99fcd33384467cfe427bd737bd2d.tar.gz
dodge-balls-2b5fd6f394ee99fcd33384467cfe427bd737bd2d.tar.bz2
ball: Use collision lines instead of tiles
-rw-r--r--src/ball.c49
1 files changed, 21 insertions, 28 deletions
diff --git a/src/ball.c b/src/ball.c
index 5c0497d..7f8aa3d 100644
--- a/src/ball.c
+++ b/src/ball.c
@@ -111,34 +111,27 @@ _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;
- double col_x;
- double col_y;
- struct db_ball *other;
-
- 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);
- }
+ struct db_map_line *line;
+ int line_x1;
+ int line_y1;
+ int line_x2;
+ int line_y2;
+ double col_x;
+ double col_y;
+ struct db_ball *other;
+
+ /* Spinning balls shouldn't bounce off walls */
+ if (ball->sr == 0) {
+ for (line = db_map_get_lines(ball->map); line != NULL;
+ line = db_map_line_get_next(line)) {
+ db_map_line_get_coords(line, &line_x1, &line_y1,
+ &line_x2, &line_y2);
+ col_x = col_y = 0; /* Shut up GCC */
+ if (db_col_cir_line(ball->x, ball->y, ball->r,
+ line_x1, line_y1,
+ line_x2, line_y2,
+ &col_x, &col_y)){
+ _db_ball_bounce(ball, col_x, col_y);
}
}
}