diff options
Diffstat (limited to 'physics.c')
| -rw-r--r-- | physics.c | 45 |
1 files changed, 37 insertions, 8 deletions
@@ -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]; } - |
