diff options
-rw-r--r-- | src/collision.c | 26 | ||||
-rw-r--r-- | src/collision.h | 6 |
2 files changed, 31 insertions, 1 deletions
diff --git a/src/collision.c b/src/collision.c index 32c5e57..2abd8a2 100644 --- a/src/collision.c +++ b/src/collision.c @@ -18,6 +18,7 @@ */ #include <SDL.h> +#include "collision.h" int db_pt_in_rect(int x, int y, SDL_Rect *rect) @@ -36,3 +37,28 @@ db_pt_in_rect(int x, int y, SDL_Rect *rect) } return 1; } + +int +db_col_cir_cir(int x1, int y1, int r1, int x2, int y2, int r2) +{ + return ((r1 + r2) * (r1 + r2)) >= + ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); +} + +/* + * This function will be called after moving the two balls being checked. A + * more accurate way to handle collisions would be to microstep through + * movements to find the exact point of collision. + */ +int +db_col_pt_cir_cir(int x1, int y1, int r1, int x2, int y2, int r2, + int *col_x, int *col_y) +{ + if (db_col_cir_cir(x1, y1, r1, x2, y2, r2)) { + *col_x = (x1 + x2) / 2; + *col_y = (y1 + y2) / 2; + return 1; + } else { + return 0; + } +} diff --git a/src/collision.h b/src/collision.h index 022cf78..58cd95e 100644 --- a/src/collision.h +++ b/src/collision.h @@ -22,6 +22,10 @@ #include <SDL.h> -int db_pt_in_rect(int x, int y, SDL_Rect *rect); +int db_pt_in_rect(int x, int y, SDL_Rect *rect) __attribute__((__pure__)); +int db_col_cir_cir(int x1, int y1, int r1, int x2, int y2, int r2) + __attribute__((__const__)); +int db_col_pt_cir_cir(int x1, int y1, int r1, int x2, int y2, int r2, + int *col_x, int *col_y); #endif /* DB_COLLISION_H_ */ |