From 0dc35bd9d9e771350512318b991af5fe9384bb2a Mon Sep 17 00:00:00 2001 From: Sara Date: Sun, 16 Jul 2023 20:53:41 +0200 Subject: [PATCH 1/4] fixed typos and mismatched statements in aabb-aabb collision --- src/corelib/physics.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/corelib/physics.c b/src/corelib/physics.c index f9ee2b4..bd41676 100644 --- a/src/corelib/physics.c +++ b/src/corelib/physics.c @@ -43,10 +43,15 @@ int _rect_overlap(float aminx, float aminy, float amaxx, float amaxy, float bmin 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); } -- 2.34.1 From 7950e4563257f944dcb12e3deb205785b03df410 Mon Sep 17 00:00:00 2001 From: Sara Date: Sun, 16 Jul 2023 21:14:44 +0200 Subject: [PATCH 2/4] reordered terms in _rect_overlap --- src/corelib/physics.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/corelib/physics.c b/src/corelib/physics.c index bd41676..dab0b33 100644 --- a/src/corelib/physics.c +++ b/src/corelib/physics.c @@ -31,13 +31,13 @@ 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) ); } @@ -58,8 +58,10 @@ short _collision_aabb_aabb(const object_t* a, const object_t* b) { 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; -- 2.34.1 From 5185f5062bd965a22f7880e3eb5381eb23177b94 Mon Sep 17 00:00:00 2001 From: Sara Date: Sun, 16 Jul 2023 21:23:59 +0200 Subject: [PATCH 3/4] solve_aabb_aabb now incorporates sprite position in all calculations --- src/corelib/physics.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/corelib/physics.c b/src/corelib/physics.c index dab0b33..8940cf9 100644 --- a/src/corelib/physics.c +++ b/src/corelib/physics.c @@ -146,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 = amaxx - bminx; + float left = aminx - bmaxx; + float top = aminy - bmaxy; + float bottom = amaxy - bminy; float ret = right; *out_px = right; @@ -190,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; } -- 2.34.1 From 41f84bb52cb002cfe9c6b4dfc94a80ad0a4e4314 Mon Sep 17 00:00:00 2001 From: Sara Date: Sun, 16 Jul 2023 21:32:33 +0200 Subject: [PATCH 4/4] reversed direction of aabb-aabb collision solver terms --- src/corelib/physics.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/corelib/physics.c b/src/corelib/physics.c index 8940cf9..1c146f1 100644 --- a/src/corelib/physics.c +++ b/src/corelib/physics.c @@ -156,10 +156,10 @@ float _solve_aabb_aabb(const object_t* a, const object_t* b, float* out_px, floa float bminy = b->physics.aabb.y + b->sprite.y; float bmaxy = bminy + b->physics.aabb.h; - float right = amaxx - bminx; - float left = aminx - bmaxx; - float top = aminy - bmaxy; - float bottom = amaxy - bminy; + float right = bmaxx - aminx; + float left = bminx - amaxx; + float top = bminy - amaxy; + float bottom = bmaxy - aminy; float ret = right; *out_px = right; -- 2.34.1