diff --git a/src/corelib/physics.c b/src/corelib/physics.c index f9ee2b4..1c146f1 100644 --- a/src/corelib/physics.c +++ b/src/corelib/physics.c @@ -31,30 +31,37 @@ static inline int _rect_overlap(float aminx, float aminy, float amaxx, float amaxy, float bminx, float bminy, float bmaxx, float bmaxy) { return ( - (aminx < bmaxx && aminx > bmaxx) + (aminx < bmaxx && aminx > bminx) || - (amaxx > bminx && amaxx < bmaxx) + (bminx < amaxx && bminx > aminx) ) && ( - (aminy < bmaxy && aminy > bmaxy) + (aminy < bmaxy && aminy > bminy) || - (amaxy > bminy && amaxy < bmaxy) + (bminy < amaxy && bminy > aminy) ); } static inline short _collision_aabb_aabb(const object_t* a, const object_t* b) { - const float aminx = a->physics.aabb.x + a->sprite.x, aminy = a->physics.aabb.y + a->sprite.x; - const float amaxx = aminx + a->physics.aabb.w, amaxy = aminy + a->physics.aabb.h; - const float bminx = b->physics.aabb.x, bminy = b->physics.aabb.y; - const float bmaxx = b->physics.aabb.x + b->physics.aabb.w, bmaxy = b->physics.aabb.y + b->physics.aabb.h; + const float aminx = a->physics.aabb.x + a->sprite.x, + aminy = a->physics.aabb.y + a->sprite.y; + const float amaxx = aminx + a->physics.aabb.w, + amaxy = aminy + a->physics.aabb.h; + + const float bminx = b->physics.aabb.x + b->sprite.x, + bminy = b->physics.aabb.y + b->sprite.y; + const float bmaxx = bminx + b->physics.aabb.w, + bmaxy = bminy + b->physics.aabb.h; return _rect_overlap(aminx, aminy, amaxx, amaxy, bminx, bminy, bmaxx, bmaxy); } static inline short _collision_circle_circle(const object_t* a, const object_t* b) { - const float ax = a->sprite.x + a->physics.circle.x, ay = a->sprite.y + a->physics.circle.y, - bx = b->sprite.x + b->physics.circle.x, by = b->sprite.y + b->physics.circle.y; + const float ax = a->sprite.x + a->physics.circle.x, + ay = a->sprite.y + a->physics.circle.y, + bx = b->sprite.x + b->physics.circle.x, + by = b->sprite.y + b->physics.circle.y; const float dx = fabsf(ax-bx), dy = fabsf(ay-by); const float sqrdist = dx*dx+dy*dy; const float mindist = a->physics.circle.radius + b->physics.circle.radius; @@ -139,10 +146,20 @@ float _solve_circle_circle(const object_t* a, const object_t* b, float* out_px, static inline float _solve_aabb_aabb(const object_t* a, const object_t* b, float* out_px, float* out_py) { - float right = (a->physics.aabb.x + a->physics.aabb.w + a->sprite.x) - (b->physics.aabb.x + b->sprite.x); - float left = (a->physics.aabb.x + a->sprite.x) - (b->physics.aabb.x + b->physics.aabb.w + b->sprite.x); - float top = (a->physics.aabb.y + a->sprite.y) - (b->physics.aabb.y + b->physics.aabb.w + b->sprite.y); - float bottom = (a->physics.aabb.y + a->physics.aabb.h) - (b->physics.aabb.y + b->sprite.y); + float aminx = a->physics.aabb.x + a->sprite.x; + float amaxx = aminx + a->physics.aabb.w; + float bminx = b->physics.aabb.x + b->sprite.x; + float bmaxx = bminx + b->physics.aabb.w; + + float aminy = a->physics.aabb.y + a->sprite.y; + float amaxy = aminy + a->physics.aabb.h; + float bminy = b->physics.aabb.y + b->sprite.y; + float bmaxy = bminy + b->physics.aabb.h; + + float right = bmaxx - aminx; + float left = bminx - amaxx; + float top = bminy - amaxy; + float bottom = bmaxy - aminy; float ret = right; *out_px = right; @@ -183,7 +200,7 @@ float get_solve_force(const object_t* a, const object_t* b, float* out_px, float void solve_collision_slide(object_t* left, object_t* right) { float dx, dy; - const float d = get_solve_force(left, right, &dx, &dy); + get_solve_force(left, right, &dx, &dy); left->sprite.x += dx; left->sprite.y += dy; }