clld.c | 185 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------- test-ppx | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 221 insertions(+), 20 deletions(-) diff --git a/clld.c b/clld.c index 6d6d3a0..5a9372e 100644 --- a/clld.c +++ b/clld.c @@ -16,7 +16,7 @@ #define max(a, b) (abs(a-b)+(a+b))/2 #define min(a, b) -max(-a, -b) -enum {THM, AABB}; +enum {THM, AABB, PPX }; /* Collision type */ int colltype = -1; @@ -74,8 +74,8 @@ int main(int argc, char **argv) { double d1; double d2; - /* Heightmap, used in Tile Heightmap collision. */ - uint8_t *heightmap; + /* Bitmask used in Per Pixel, and Tile Heightmap collision. */ + uint8_t **bitmask; /* X axis collision. */ unsigned int clx : 1; @@ -120,10 +120,34 @@ int main(int argc, char **argv) { tmp.w = atoi(strtok(buf, ",")); tmp.h = atoi(strtok(NULL, "\n")); /* - * Initialize heightmap. + * Initialize y axis of bitmask. + * If Tile Heightmap is used, Initialize it using the width. */ - if (colltype == THM && coll) - tmp.heightmap = malloc(sizeof(uint8_t *)*tmp.w); + if (colltype == THM || colltype == PPX) + tmp.bitmask = (colltype == PPX) ? malloc(sizeof(uint8_t *)*tmp.h) : malloc(sizeof(uint8_t *)*tmp.w); + /* Initialize x axis of bitmask. */ + if (colltype == PPX) + for(unsigned int i = 0; i= tmp.w) ? tmp.w : newidth; + for (unsigned int i = 0; i = hmpeak) ? atoi(tmptok)+1 : hmpeak; } } /* - * Copy width, and height to either - * the Cursor, or the Floor. + * Copy width, height, and bitmask over + * to either the Cursor, or the Floor. */ (coll) ? (floor.w = tmp.w) : (cursor.w = tmp.w); (coll) ? (floor.h = tmp.h) : (cursor.h = tmp.h); - if (coll) - floor.heightmap = tmp.heightmap; + (coll) ? (floor.bitmask = tmp.bitmask) : (cursor.bitmask = tmp.bitmask); /* Debugging stuff. */ if (verbose >= 1) { if (colltype == THM && coll) for (unsigned int i = 0; i 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.gnded = 0; + + if (cursor.clx || cursor.cly) { + int rw1 = (cursor.a2-cursor.a1)/2, rh1 = (cursor.b2-cursor.b1)/2; + int rw2 = (floor.a2-floor.a1)/2, rh2 = (floor.b2-floor.b1)/2; + uint8_t t = 0, b = 0, l = 0, r = 0; + int olxab = min(cursor.a2, floor.a2)-max(cursor.a1, floor.a1); + int olyab = min(cursor.b2, floor.b2)-max(cursor.b1, floor.b1); + int olxl = cursor.a2-floor.a1; + int olxr = floor.a2-cursor.a1; + int olyt = cursor.b2-floor.b1; + int olyb = floor.b2-cursor.b1; + t = (cursor.b2 > rh2 && olyt <= floor.h+1) ? 1 : 0; + b = (cursor.b1 > rh2 && olyb <= floor.h+1) ? 1 : 0; + l = (cursor.a2 > rw2 && olxl <= floor.w+1) ? 1 : 0; + r = (cursor.a1 > rw2 && olxr <= floor.w+1) ? 1 : 0; + if (t && b && cursor.xvel == 0) { + t = (cursor.yvel > 0) ? 1 : 0; + b = (cursor.yvel < 0) ? 1 : 0; + } + if (l && r && cursor.yvel == 0) { + l = (cursor.xvel > 0) ? 1 : 0; + r = (cursor.xvel < 0) ? 1 : 0; + } + + int x = (l) ? olxl-1 : (r ? olxr-1 : -1); + int y = (t) ? olyt-1 : (b ? olyb-1 : -1); + /* X coordinate of overlap between the Cursor, and Floor. */ + /*if (cursor.a2 > floor.a1-(int)cursor.xvel-1) + else if (cursor.a1 < floor.a2-(int)cursor.xvel+1) + olx = cursor.a1-(floor.a2-(int)cursor.xvel+1);*/ + /*int olw = (cursor.a1+rw1)-(floor.a1+rw2);*/ + + /* Y coordinate of overlap between the Cursor, and Floor. */ + /*if (cursor.b2 > floor.b1-(int)cursor.yvel-1) + oly = cursor.b2-(floor.b1-(int)cursor.yvel-1); + else if (cursor.b1 < floor.b2-(int)cursor.yvel+1) + oly = cursor.b1-(floor.b2-(int)cursor.yvel+1);*/ + /*int olh = (cursor.b1+rh1)-(floor.b1+rh2);*/ + + printf( + "olxab: %i, olyab: %i\n" + "olxl: %i, olyt: %i\n" + "olxr: %i, olyb: %i\n" + "cursor.a2: %i, cursor.b2: %i\n" + "floor.a2: %i, floor.b2: %i\n" + "x: %i, y: %i\n" + "min(cursor.b2, floor.b2)-max(cursor.b1, floor.b1): %i\n" + "min(cursor.b2, floor.b2): %i\n" + "max(cursor.b1, floor.b1): %i\n" + "t: %u, b: %u, l: %u, r: %u\n" + , olxab, olyab + , olxl, olyt + , olxr, olyb + , cursor.a2, cursor.b2 + , floor.a2, floor.b2 + , x, y + , min(cursor.b2, floor.b2)-max(cursor.b1, floor.b1) + , min(cursor.b2, floor.b2) + , max(cursor.b1, floor.b1) + , t, b, l, r); + if((x >= 0 && x < floor.w) && (y >= 0 && y <= floor.h)) + for (y; y floor.w) { /* Is the Cursor coming from the left? */ - if (cursor.a2 > floor.a1-(int)cursor.xvel-1 && floor.b2-cursor.b2 < (int)floor.heightmap[0] && !floor.flp && cursor.xvel > 0) + if (cursor.a2 > floor.a1-(int)cursor.xvel-1 && floor.b2-cursor.b2 < (int)floor.bitmask[0] && !floor.flp && cursor.xvel > 0) cursor.pos.x = (floor.pos.x-(int)cursor.xvel)-cursor.w-1; - else if (cursor.a1 < (floor.a2-cursor.w)-(int)cursor.xvel+1 && floor.b2-cursor.b2 < (int)floor.heightmap[floor.w-1] && !floor.flp && cursor.xvel < 0) + else if (cursor.a1 < (floor.a2-cursor.w)-(int)cursor.xvel+1 && floor.b2-cursor.b2 < (int)floor.bitmask[floor.w-1] && !floor.flp && cursor.xvel < 0) cursor.pos.x = (floor.a2-cursor.w)-(int)cursor.xvel+1; /* Is the Cursor coming from the right? */ if (cursor.a1 < (floor.a2-cursor.w)-(int)cursor.xvel+1 && cursor.b1-floor.b1 < 0 && floor.flp && cursor.xvel < 0) @@ -244,7 +386,7 @@ int main(int argc, char **argv) { if (cursor.cly) { if (cursor.a2-floor.a1 < floor.w) { cursor.yvel = 0; - newy = (int)floor.heightmap[cursor.a2-floor.a1]; + newy = (int)floor.bitmask[cursor.a2-floor.a1]; /* Is the Floor, really a floor? */ if (cursor.b2 > (floor.b2-hmpeak)-(int)cursor.yvel && floor.b2-cursor.b2 >= 0 && !floor.flp) cursor.pos.y = ((floor.b2-cursor.h)-newy)-(int)cursor.yvel; @@ -288,9 +430,9 @@ int main(int argc, char **argv) { if (cursor.clx) { if (cursor.c2-floor.c1 < -1 || cursor.c2-floor.c1 > floor.w) { /* Is the Cursor coming from the left? */ - if (cursor.c2 > floor.c1-cursor.xvel-1 && floor.d2-cursor.d2 < (int)floor.heightmap[0] && !floor.flp && cursor.xvel > 0) + if (cursor.c2 > floor.c1-cursor.xvel-1 && floor.d2-cursor.d2 < (int)floor.bitmask[0] && !floor.flp && cursor.xvel > 0) cursor.pos.fx = (floor.pos.x-cursor.xvel)-cursor.w-1; - else if (cursor.c1 < (floor.c2-cursor.w)-cursor.xvel+1 && floor.d2-cursor.d2 < (int)floor.heightmap[floor.w-1] && !floor.flp && cursor.xvel < 0) + else if (cursor.c1 < (floor.c2-cursor.w)-cursor.xvel+1 && floor.d2-cursor.d2 < (int)floor.bitmask[floor.w-1] && !floor.flp && cursor.xvel < 0) cursor.pos.fx = (floor.c2-cursor.w)-cursor.xvel+1; /* Is the Cursor coming from the right? */ if (cursor.c1 < (floor.c2-cursor.w)-cursor.xvel+1 && cursor.d1-floor.d1 < 0 && floor.flp && cursor.xvel < 0) @@ -303,7 +445,7 @@ int main(int argc, char **argv) { if (cursor.cly) { if (cursor.c2-floor.c1 < floor.w) { cursor.yvel = 0; - newy = (int)floor.heightmap[(int)cursor.c2-(int)floor.c1]; + newy = (int)floor.bitmask[(int)cursor.c2-(int)floor.c1]; /* Is the Floor, really a floor? */ if (cursor.d2 > (floor.d2-hmpeak)-cursor.yvel && floor.d2-cursor.d2 >= 0 && !floor.flp) cursor.pos.fy = ((floor.d2-cursor.h)-newy)-cursor.yvel; @@ -406,8 +548,11 @@ int main(int argc, char **argv) { uint8_t a = 0; unsigned int i = 0; free(buf); - if(colltype == THM) { - free(floor.heightmap); + if(colltype == THM || colltype == PPX) { + while (!a && colltype == PPX) + (i