From 658dd23ba0e7114ceee0c78fb769f31f0aeeee0a Mon Sep 17 00:00:00 2001 From: esquizo Date: Fri, 24 Apr 2026 20:55:49 -0300 Subject: colisão do mapa MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- physics.c | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) (limited to 'physics.c') diff --git a/physics.c b/physics.c index f8d785b..5b32356 100644 --- a/physics.c +++ b/physics.c @@ -1,20 +1,49 @@ +#include + #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]; } - -- cgit v1.2.3