From 34d686e43ad4fc5ebe080df2019ad88bdaf750db Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Sat, 27 Mar 2021 17:00:25 -0400 Subject: ball: Bounce balls off each other --- 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); } } } -- cgit v0.9.1