summaryrefslogtreecommitdiff
path: root/libphysics/aabb.c
diff options
context:
space:
mode:
authoresquizo <esquizo+noreply@esquizo.net>2026-05-17 12:02:56 -0300
committeresquizo <esquizo+noreply@esquizo.net>2026-05-17 12:02:56 -0300
commit97702f11ea016ca2e395f71f03c3fc3a2ee929ca (patch)
tree20380f895ee422c48b970c7da5b921e046e35da4 /libphysics/aabb.c
parent5c6b3c19172948cf2e1800a9333cfcd858ee829c (diff)
adicionado libphysics
Diffstat (limited to 'libphysics/aabb.c')
-rw-r--r--libphysics/aabb.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/libphysics/aabb.c b/libphysics/aabb.c
new file mode 100644
index 0000000..59648c8
--- /dev/null
+++ b/libphysics/aabb.c
@@ -0,0 +1,52 @@
+#include <vecmath.h>
+#include <physics.h>
+#include <stdlib.h>
+
+#include "dat.h"
+
+static void mink(Body *a, Body *b, vec2 min, vec2 max);
+
+int
+phxaabbcheck(Body *a, Body *b)
+{
+ vec2 min, max;
+ mink(a, b, min, max);
+ return min[0] < 0 && max[0] > 0 && min[1] < 0 && max[1] > 0;
+}
+
+void
+phxaabbresolv(Body *a, Body *b, vec2 p, vec2 n)
+{
+ vec2 min, max;
+ mink(a, b, min, max);
+
+ float hx = fabsf(min[0]) < fabsf(max[0]) ? fabsf(min[0]) : fabsf(max[0]);
+ float hy = fabsf(min[1]) < fabsf(max[1]) ? fabsf(min[1]) : fabsf(max[1]);
+
+ if(hx < hy) {
+ n[1] = 0.0;
+ n[0] = (fabsf(min[0]) < fabsf(max[0]) ? -1.0 : 1.0);
+ } else {
+ n[0] = 0;
+ n[1] = (fabsf(min[1]) < fabsf(max[1]) ? -1.0 : 1.0);
+ }
+
+ vec2_mul(p, (vec2){ hx, hy }, n);
+}
+
+
+void
+mink(Body *a, Body *b, vec2 min, vec2 max)
+{
+ 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];
+ }
+
+ for(int i = 0; i < 2; i++) {
+ min[i] = p[i] - s[i];
+ max[i] = p[i] + s[i];
+ }
+}