summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2021-03-27 17:00:25 (EDT)
committer P. J. McDermott <pj@pehjota.net>2021-03-27 17:00:25 (EDT)
commit34d686e43ad4fc5ebe080df2019ad88bdaf750db (patch)
tree26bf1eb93c99840d38907b083aeec54854f11858 /src
parentb46d474f2a81e11a3e97cb9685c676e29967b32f (diff)
downloaddodge-balls-34d686e43ad4fc5ebe080df2019ad88bdaf750db.zip
dodge-balls-34d686e43ad4fc5ebe080df2019ad88bdaf750db.tar.gz
dodge-balls-34d686e43ad4fc5ebe080df2019ad88bdaf750db.tar.bz2
ball: Bounce balls off each other
Diffstat (limited to 'src')
-rw-r--r--src/ball.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/src/ball.c b/src/ball.c
index e37615b..a7ed27e 100644
--- a/src/ball.c
+++ b/src/ball.c
@@ -95,6 +95,15 @@ db_balls_move(struct db_ball *ball)
}
}
+static void
+_db_ball_bounce(struct db_ball *ball, double col_x, double col_y)
+{
+ double col_a;
+
+ col_a = atan2(col_y - ball->y, col_x - ball->x) * 180 / M_PI;
+ ball->a = (2 * col_a - ball->a - 180) % 360;
+}
+
void
db_balls_collisions(struct db_ball *ball)
{
@@ -109,7 +118,8 @@ db_balls_collisions(struct db_ball *ball)
ball->x, ball->y, ball->r,
other->x, other->y, other->r,
&col_x, &col_y)) {
- /* TODO: Reverse direction */
+ _db_ball_bounce(ball , col_x, col_y);
+ _db_ball_bounce(other, col_x, col_y);
}
}
}