diff options
author | mrb0nk500 <b0nk@b0nk.xyz> | 2019-10-14 23:15:45 -0400 |
---|---|---|
committer | mrb0nk500 <b0nk@b0nk.xyz> | 2019-10-14 23:22:07 -0400 |
commit | f29f1f97ab09e6fbe209ca7ee55bf7be2bd580ee (patch) | |
tree | 35e95d3f084136693720da9c5c54e78bf755b42f | |
parent | 0d4f1353c9e6277b9cf0fe781e75eada0d701213 (diff) |
Move AABB's clx, and cly checks to separate functions.
This is to simplify the program, and to possibly find
ways to make it run faster.
-rw-r--r-- | clld-bench.c | 51 | ||||
-rw-r--r-- | clld.c | 35 |
2 files changed, 40 insertions, 46 deletions
diff --git a/clld-bench.c b/clld-bench.c index fbcaec6..ab81176 100644 --- a/clld-bench.c +++ b/clld-bench.c @@ -14,8 +14,6 @@ #include <string.h> #include <unistd.h> #define TERASGN(a, b, c, d) (a) ? (b = d) : (c = d) -#define max(a, b) (abs(a-b)+(a+b))/2 -#define min(a, b) -max(-a, -b) enum {THM, AABB}; @@ -95,6 +93,18 @@ static int usage() { return 0; } +int clx(struct colis c, struct colis f, int xvel, uint8_t flt) { + int l = (!flt) ? (c.a2 > f.a1-xvel-1 && c.b2 > f.b1) : (c.c2 > f.c1-c.xvel-1 && c.d2 > f.d1); + int r = (!flt) ? (c.a1 < f.a2-xvel+1 && c.b1 < f.b2) : (c.c1 < f.c2-c.xvel+1 && c.d1 < f.d2); + return l && r; +} + +int cly(struct colis c, struct colis f, int yvel, uint8_t flt) { + int b = (!flt) ? (c.b2 > f.b1-yvel-1 && c.a2 > f.a1) : (c.d2 > f.d1-c.yvel-1 && c.c2 > f.c1); + int t = (!flt) ? (c.b1 < f.b2-yvel+1 && c.a1 < f.a2) : (c.d1 < f.d2-c.yvel+1 && c.c1 < f.c2); + return b && t; +} + /* Tile Heightmap Collision. */ int thm(uint8_t flt, int hmpeak, struct colis cursor, struct colis floor) { int newy = 0; @@ -108,6 +118,7 @@ int thm(uint8_t flt, int hmpeak, struct colis cursor, struct colis floor) { floor.b1 = floor.pos.y; floor.b2 = floor.pos.y+floor.h; + cursor.clx = ((cursor.a2 > floor.a1-(int)cursor.xvel-1 && cursor.b2 > floor.b1) && (cursor.a1 < (floor.a2-cursor.w)-(int)cursor.xvel+1 && cursor.b1 < floor.b2)) ? 1 : 0; /* Check for a collision, on the y axis. */ cursor.cly = ((cursor.b2 >= (floor.b1-hmpeak)-(int)cursor.yvel && cursor.a2 >= floor.a1) && (cursor.b1 <= (floor.b2+hmpeak)-(int)cursor.yvel && cursor.a1 <= floor.a2)) ? 1 : 0; @@ -155,7 +166,6 @@ int thm(uint8_t flt, int hmpeak, struct colis cursor, struct colis floor) { , (cursor.a2-(cursor.w/2))-(floor.a2-(floor.w/2)) , (cursor.a2-(cursor.w/2))); } - printf("cursor.w: %i, cursor.h: %i\n", cursor.w, cursor.h); printf("%u, %u\n%u\n%i\n%i, %i\n%07.06f, %07.06f\n", cursor.clx, cursor.cly, cursor.gnded, newy, cursor.pos.x, cursor.pos.y, cursor.xvel, cursor.yvel); } else { cursor.c1 = cursor.pos.fx; @@ -203,7 +213,6 @@ int thm(uint8_t flt, int hmpeak, struct colis cursor, struct colis floor) { } } cursor.gnded = (cursor.pos.fy == ((floor.d2-cursor.h)-newy)-cursor.yvel) ? 1 : 0; - printf("cursor.c2-floor.c1: %07.06f\n", cursor.c2-floor.c1); printf("%u, %u\n%u\n%i\n%07.06f, %07.06f\n%07.06f, %07.06f\n", cursor.clx, cursor.cly, cursor.gnded, newy, cursor.pos.fx, cursor.pos.fy, cursor.xvel, cursor.yvel); } return 0; @@ -224,8 +233,8 @@ int aabb(uint8_t flt, struct colis cursor, struct colis floor) { cursor.gnded = (cursor.b2 == floor.b1-(int)cursor.yvel-1) ? 1 : 0; /* Check for collisions, on both axies. */ - cursor.clx = ((cursor.a2 > floor.a1-(int)cursor.xvel-1 && cursor.b2 > floor.b1) && (cursor.a1 < floor.a2-(int)cursor.xvel+1 && cursor.b1 < floor.b2)) ? 1 : 0; - cursor.cly = ((cursor.b2 > floor.b1-(int)cursor.yvel-1 && cursor.a2 > floor.a1) && (cursor.b1 < floor.b2-(int)cursor.yvel+1 && cursor.a1 < floor.a2)) ? 1 : 0; + cursor.clx = clx(cursor, floor, (int)cursor.xvel, flt); + cursor.cly = cly(cursor, floor, (int)cursor.yvel, flt); if (cursor.clx) { /* Is the Cursor coming from the left? */ @@ -245,14 +254,7 @@ int aabb(uint8_t flt, struct colis cursor, struct colis floor) { cursor.pos.y = (floor.pos.y+floor.h)-(int)cursor.yvel+1; cursor.yvel = 0; } - - if (verbose >= 1) { - printf("cursor.gnded: %u\nclx: %u, cly: %u\n", cursor.gnded, cursor.clx, cursor.cly); - printf("x: %i, y: %i\n", cursor.pos.x, cursor.pos.y); - printf("floor.x: %i, floor.y: %i\n", floor.pos.x, floor.pos.y); - printf ("r: %i\n", (floor.pos.x+floor.w)-(int)cursor.xvel+1); - } - printf("%u, %u\n%u\n%i, %i\n%07.06f, %07.06f\n\0", cursor.clx, cursor.cly, cursor.gnded, cursor.pos.x, cursor.pos.y, cursor.xvel, cursor.yvel); + printf("%u, %u\n%u\n%i, %i\n%07.06f, %07.06f\n", cursor.clx, cursor.cly, cursor.gnded, cursor.pos.x, cursor.pos.y, cursor.xvel, cursor.yvel); } else { cursor.c1 = cursor.pos.fx; cursor.c2 = cursor.pos.fx+cursor.w; @@ -264,8 +266,8 @@ int aabb(uint8_t flt, struct colis cursor, struct colis floor) { floor.d2 = floor.pos.fy+floor.h; cursor.gnded = (cursor.d2 == floor.d1-cursor.yvel-1) ? 1 : 0; - cursor.clx = ((cursor.c2 > floor.c1-cursor.xvel-1 && cursor.d2 > floor.d1) && (cursor.c1 < floor.c2-cursor.xvel+1 && cursor.d1 < floor.d2)) ? 1 : 0; - cursor.cly = ((cursor.d2 > floor.d1-cursor.yvel-1 && cursor.c2 > floor.c1) && (cursor.d1 < floor.d2-cursor.yvel+1 && cursor.c1 < floor.c2)) ? 1 : 0; + cursor.clx = clx(cursor, floor, 0, flt); + cursor.cly = cly(cursor, floor, 0, flt); if (cursor.clx) { if (cursor.c2 > floor.c1-cursor.xvel-1 && cursor.xvel > 0) @@ -281,12 +283,7 @@ int aabb(uint8_t flt, struct colis cursor, struct colis floor) { cursor.pos.fy = (floor.pos.fy+(double)floor.h)-cursor.yvel+1; cursor.yvel = 0; } - if (verbose >= 1) { - printf("cursor.gnded: %u\nclx: %u, cly: %u\n", cursor.gnded, cursor.clx, cursor.cly); - printf("fx: %07.06f, fy: %07.06f\n", cursor.pos.fx, cursor.pos.fy); - printf("floor.fx: %07.06f, floor.fy: %07.06f\n", floor.pos.fx, floor.pos.fy); - } - printf("%u, %u\n%u\n%07.06f, %07.06f\n%07.06f, %07.06f\n\0", cursor.clx, cursor.cly, cursor.gnded, cursor.pos.fx, cursor.pos.fy, cursor.xvel, cursor.yvel); + printf("%u, %u\n%u\n%07.06f, %07.06f\n%07.06f, %07.06f\n", cursor.clx, cursor.cly, cursor.gnded, cursor.pos.fx, cursor.pos.fy, cursor.xvel, cursor.yvel); } return 0; } @@ -339,13 +336,13 @@ int main(int argc, char **argv) { /* Create the Cursor, and the Floor */ struct colis cursor; struct colis floor; - colltype = THM; + colltype = AABB; cursor.h = 16; cursor.w = 16; - cursor.pos.x = 17; - cursor.pos.y = 32; - cursor.xvel = 1; - cursor.yvel = 0; + cursor.pos.x = 24; + cursor.pos.y = 15; + cursor.xvel = 0; + cursor.yvel = 1; floor.w = 16; floor.h = 16; floor.flp = 0; @@ -14,8 +14,6 @@ #include <string.h> #include <unistd.h> #define TERASGN(a, b, c, d) (a) ? (b = d) : (c = d) -#define max(a, b) (abs(a-b)+(a+b))/2 -#define min(a, b) -max(-a, -b) enum {THM, AABB}; @@ -95,6 +93,18 @@ static int usage() { return 0; } +int clx(struct colis c, struct colis f, int xvel, uint8_t flt) { + int l = (!flt) ? (c.a2 > f.a1-xvel-1 && c.b2 > f.b1) : (c.c2 > f.c1-c.xvel-1 && c.d2 > f.d1); + int r = (!flt) ? (c.a1 < f.a2-xvel+1 && c.b1 < f.b2) : (c.c1 < f.c2-c.xvel+1 && c.d1 < f.d2); + return l && r; +} + +int cly(struct colis c, struct colis f, int yvel, uint8_t flt) { + int b = (!flt) ? (c.b2 > f.b1-yvel-1 && c.a2 > f.a1) : (c.d2 > f.d1-c.yvel-1 && c.c2 > f.c1); + int t = (!flt) ? (c.b1 < f.b2-yvel+1 && c.a1 < f.a2) : (c.d1 < f.d2-c.yvel+1 && c.c1 < f.c2); + return b && t; +} + /* Tile Heightmap Collision. */ int thm(uint8_t flt, int hmpeak, struct colis cursor, struct colis floor) { int newy = 0; @@ -144,18 +154,6 @@ int thm(uint8_t flt, int hmpeak, struct colis cursor, struct colis floor) { } } cursor.gnded = (cursor.pos.y == ((floor.b2-cursor.h)-newy)-(int)cursor.yvel) ? 1 : 0; - if (verbose >= 1) { - printf( - "cursor.a1-(floor.a2-(floor.w/2)): %i\n" - "cursor.a2-(floor.a2-(floor.w/2)): %i\n" - "(cursor.a2-(cursor.w/2))-(floor.a2-(floor.w/2)): %i\n" - "(cursor.a2-(cursor.w/2)): %i\n" - , cursor.a1-(floor.a2-(floor.w/2)) - , cursor.a2-(floor.a2-(floor.w/2)) - , (cursor.a2-(cursor.w/2))-(floor.a2-(floor.w/2)) - , (cursor.a2-(cursor.w/2))); - } - printf("cursor.w: %i, cursor.h: %i\n", cursor.w, cursor.h); printf("%u, %u\n%u\n%i\n%i, %i\n%07.06f, %07.06f\n", cursor.clx, cursor.cly, cursor.gnded, newy, cursor.pos.x, cursor.pos.y, cursor.xvel, cursor.yvel); } else { cursor.c1 = cursor.pos.fx; @@ -203,7 +201,6 @@ int thm(uint8_t flt, int hmpeak, struct colis cursor, struct colis floor) { } } cursor.gnded = (cursor.pos.fy == ((floor.d2-cursor.h)-newy)-cursor.yvel) ? 1 : 0; - printf("cursor.c2-floor.c1: %07.06f\n", cursor.c2-floor.c1); printf("%u, %u\n%u\n%i\n%07.06f, %07.06f\n%07.06f, %07.06f\n", cursor.clx, cursor.cly, cursor.gnded, newy, cursor.pos.fx, cursor.pos.fy, cursor.xvel, cursor.yvel); } return 0; @@ -224,8 +221,8 @@ int aabb(uint8_t flt, struct colis cursor, struct colis floor) { cursor.gnded = (cursor.b2 == floor.b1-(int)cursor.yvel-1) ? 1 : 0; /* Check for collisions, on both axies. */ - cursor.clx = ((cursor.a2 > floor.a1-(int)cursor.xvel-1 && cursor.b2 > floor.b1) && (cursor.a1 < floor.a2-(int)cursor.xvel+1 && cursor.b1 < floor.b2)) ? 1 : 0; - cursor.cly = ((cursor.b2 > floor.b1-(int)cursor.yvel-1 && cursor.a2 > floor.a1) && (cursor.b1 < floor.b2-(int)cursor.yvel+1 && cursor.a1 < floor.a2)) ? 1 : 0; + cursor.clx = clx(cursor, floor, (int)cursor.xvel, flt); + cursor.cly = cly(cursor, floor, (int)cursor.yvel, flt); if (cursor.clx) { /* Is the Cursor coming from the left? */ @@ -265,8 +262,8 @@ int aabb(uint8_t flt, struct colis cursor, struct colis floor) { floor.d2 = floor.pos.fy+floor.h; cursor.gnded = (cursor.d2 == floor.d1-cursor.yvel-1) ? 1 : 0; - cursor.clx = ((cursor.c2 > floor.c1-cursor.xvel-1 && cursor.d2 > floor.d1) && (cursor.c1 < floor.c2-cursor.xvel+1 && cursor.d1 < floor.d2)) ? 1 : 0; - cursor.cly = ((cursor.d2 > floor.d1-cursor.yvel-1 && cursor.c2 > floor.c1) && (cursor.d1 < floor.d2-cursor.yvel+1 && cursor.c1 < floor.c2)) ? 1 : 0; + cursor.clx = clx(cursor, floor, 0, flt); + cursor.cly = cly(cursor, floor, 0, flt); if (cursor.clx) { if (cursor.c2 > floor.c1-cursor.xvel-1 && cursor.xvel > 0) |