summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2021-03-25 07:07:55 (EDT)
committer P. J. McDermott <pj@pehjota.net>2021-03-25 07:07:55 (EDT)
commit379ec224fbe7259cc67dbf7842c293f92e3bac9a (patch)
treed9a2ae75d3edfc4b6f75a85640c13771246602f3 /src
parent2dc013c782ce461b68484a49bd0cd0d2e1e3e8c9 (diff)
downloaddodge-balls-379ec224fbe7259cc67dbf7842c293f92e3bac9a.zip
dodge-balls-379ec224fbe7259cc67dbf7842c293f92e3bac9a.tar.gz
dodge-balls-379ec224fbe7259cc67dbf7842c293f92e3bac9a.tar.bz2
collision: Add circle-circle functions
Also fix missing #include and mark db_pt_in_rect() pure.
Diffstat (limited to 'src')
-rw-r--r--src/collision.c26
-rw-r--r--src/collision.h6
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_ */