summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2021-03-27 18:14:52 (EDT)
committer P. J. McDermott <pj@pehjota.net>2021-03-27 18:24:50 (EDT)
commit864e053487967faa7dd51709df1869d87544b64f (patch)
tree42f94b077a3587579861f5cc80e2384dd52bf2e6
parent4a539a2210b90f4580adb46f9f8df7da2a391c29 (diff)
downloaddodge-balls-864e053487967faa7dd51709df1869d87544b64f.zip
dodge-balls-864e053487967faa7dd51709df1869d87544b64f.tar.gz
dodge-balls-864e053487967faa7dd51709df1869d87544b64f.tar.bz2
ball: Avoid colliding with multiple coaxial walls
-rw-r--r--src/ball.c27
1 files changed, 26 insertions, 1 deletions
diff --git a/src/ball.c b/src/ball.c
index 14ee8de..d4c27a4 100644
--- a/src/ball.c
+++ b/src/ball.c
@@ -118,11 +118,13 @@ 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];
int x;
int y;
- struct db_ball *other;
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);
@@ -130,6 +132,10 @@ 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;
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)) {
@@ -137,7 +143,26 @@ 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;
+ }
}
}
}