summaryrefslogtreecommitdiff
path: root/physics.c
diff options
context:
space:
mode:
authoresquizo <esquizo+noreply@esquizo.net>2026-04-24 20:55:49 -0300
committeresquizo <esquizo+noreply@esquizo.net>2026-04-24 20:55:49 -0300
commit658dd23ba0e7114ceee0c78fb769f31f0aeeee0a (patch)
tree0d7d9f72f6f9eae075baefcb7dc6ea6a73299241 /physics.c
parent15a8758f6a99d18ab481e971c7cdfa0c10b71ff6 (diff)
colisão do mapa
Diffstat (limited to 'physics.c')
-rw-r--r--physics.c45
1 files changed, 37 insertions, 8 deletions
diff --git a/physics.c b/physics.c
index f8d785b..5b32356 100644
--- a/physics.c
+++ b/physics.c
@@ -1,20 +1,49 @@
+#include <math.h>
+
#include "dat.h"
+static void mink(Body *a, Body *b, float *minx, float *miny, float *maxx, float *maxy);
+
int
checkcollision(Body *a, Body *b)
{
- float p[2], s[2], minx, miny, maxx, maxy;
+ float minx, miny, maxx, maxy;
+
+ mink(a, b, &minx, &miny, &maxx, &maxy);
+
+ return minx < 0 && maxx > 0 && miny < 0 && maxy > 0;
+}
+
+void
+resolvecoll(Body *a, Body *b, float p[2])
+{
+ float minx, miny, maxx, maxy;
+ mink(a, b, &minx, &miny, &maxx, &maxy);
+
+ float hx = fabsf(minx) < fabsf(maxx) ? fabsf(minx) : fabsf(maxx);
+ float hy = fabsf(miny) < fabsf(maxy) ? fabsf(miny) : fabsf(maxy);
+
+ if(hx < hy) {
+ p[1] = 0;
+ p[0] = hx * (fabsf(minx) < fabsf(maxx) ? -1.0 : 1.0);
+ } else {
+ p[0] = 0;
+ p[1] = hy * (fabsf(miny) < fabsf(maxy) ? -1.0 : 1.0);
+ }
+}
+
+void
+mink(Body *a, Body *b, float *minx, float *miny, float *maxx, float *maxy)
+{
+ float p[2], s[2];
for(int i = 0; i < 2; i++) {
s[i] = a->size[i] + b->size[i];
p[i] = a->pos[i] - b->pos[i];
}
- minx = p[0] - s[0];
- maxx = p[0] + s[0];
- miny = p[1] - s[1];
- maxy = p[1] + s[1];
-
- return minx < 0 && maxx > 0 && miny < 0 && maxy > 0;
+ *minx = p[0] - s[0];
+ *maxx = p[0] + s[0];
+ *miny = p[1] - s[1];
+ *maxy = p[1] + s[1];
}
-