diff options
author | P. 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) |
commit | 34d686e43ad4fc5ebe080df2019ad88bdaf750db (patch) | |
tree | 26bf1eb93c99840d38907b083aeec54854f11858 | |
parent | b46d474f2a81e11a3e97cb9685c676e29967b32f (diff) | |
download | dodge-balls-34d686e43ad4fc5ebe080df2019ad88bdaf750db.zip dodge-balls-34d686e43ad4fc5ebe080df2019ad88bdaf750db.tar.gz dodge-balls-34d686e43ad4fc5ebe080df2019ad88bdaf750db.tar.bz2 |
ball: Bounce balls off each other
-rw-r--r-- | src/ball.c | 12 |
1 files changed, 11 insertions, 1 deletions
@@ -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); } } } |