From 97702f11ea016ca2e395f71f03c3fc3a2ee929ca Mon Sep 17 00:00:00 2001 From: esquizo Date: Sun, 17 May 2026 12:02:56 -0300 Subject: adicionado libphysics --- libphysics/aabb.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 libphysics/aabb.c (limited to 'libphysics/aabb.c') 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 +#include +#include + +#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]; + } +} -- cgit v1.2.3