diff options
author | mrb0nk500 <b0nk@b0nk.xyz> | 2019-10-05 19:40:03 -0400 |
---|---|---|
committer | mrb0nk500 <b0nk@b0nk.xyz> | 2019-10-05 19:40:03 -0400 |
commit | 4891aed55e569ac1f78f854be7fb9ca15970e810 (patch) | |
tree | e16f6cab4fe63bc94b0497431b533310f97b3c0f | |
parent | ac0e65bc5b4067aa7bcaec29d7dad4fab03516d4 (diff) |
Added Tile Heightmap Collision, for real this time.
Unlike AABB, Tile Heightmap was very easy to add, which
really supprised me.
I also revamped AABB to not have as many if statements.
-rw-r--r-- | clld.c | 184 | ||||
-rwxr-xr-x | test-mvmt.sh | 26 | ||||
-rwxr-xr-x | test-thm-mvmt.sh | 36 | ||||
-rw-r--r-- | test-tile | 8 |
4 files changed, 196 insertions, 58 deletions
@@ -32,7 +32,7 @@ static int usage() { "Usage: clld [options]\n" "Options:\n" " -c<type> Force the collision type to be\n" - " the specified type (0-2).\n" + " the specified type (THM=0, AABB=1, PPX=2).\n" " -v[level] Sets the verbosity level (0-3).\n" ); return 0; @@ -49,28 +49,55 @@ int main(int argc, char **argv) { }; struct colis { + /* Cursor/Floor's width, and height */ int w; int h; + + /* Cursor's X, and Y velocity. */ double xvel; double yvel; + + /* Cursor/Floor's coordinates. */ struct pos pos; + + /* The four corners of either the Cursor, or the Floor. */ int a1; int a2; int b1; int b2; + + /* The four floating point corners of either the Cursor, or the Floor. */ double c1; double c2; double d1; double d2; + + /* Bitmask used in Per Pixel, and Tile Heightmap collision. */ uint8_t **bitmask; + + /* X axis collision. */ unsigned int clx : 1; + + /* Y axis collision. */ unsigned int cly : 1; + + /* Cursor is on the ground. */ unsigned int gnded : 1; + + /* Vertical flip flag. */ + unsigned int flp : 1; }; + /* Create the Cursor, and the Floor */ struct colis cursor; struct colis floor; + + /* + * Create a temporary Cursor/Floor, because it simplifies + * the process of getting collision data, to one for loop. + */ struct colis tmp; + /* Get collision type. */ char *buf = NULL; size_t size; @@ -79,12 +106,14 @@ int main(int argc, char **argv) { if (colltype == -1) colltype = atoi(strtok(buf, "\n")); - if (verbose >= 1) - printf("%i\n", colltype); for (int coll = 0; coll <= 1; coll++) { buf = NULL; getline(&buf, &size, stdin); - /* <w>, <h> */ + /* + * Format is: + * + * <w>, <h> + */ tmp.w = atoi(strtok(buf, ",")); tmp.h = atoi(strtok(NULL, "\n")); /* Initialize y axis of bitmask. */ @@ -94,7 +123,7 @@ int main(int argc, char **argv) { if (colltype == PPX) for(unsigned int i = 0; i<tmp.h; i++) tmp.bitmask[i] = malloc(sizeof(uint8_t)*tmp.w); - /* Per Pixel Colision. */ + /* Get Per Pixel data. */ if (colltype == PPX) { for (unsigned int j = 0; j<tmp.h; j++) { buf = NULL; @@ -113,9 +142,13 @@ int main(int argc, char **argv) { tmp.bitmask[j][i] = buf[i] - '0'; } } - - /* Tile Heightmap Collision. */ + /* Get Tile Heightmap data. */ if (colltype == THM && coll) { + /* Get vertical flip flag */ + buf = NULL; + getline(&buf, &size, stdin); + floor.flp = buf[0] - '0'; + /* Get Heightmap */ buf = NULL; size_t newheight = getline(&buf, &size, stdin); /* @@ -129,18 +162,21 @@ int main(int argc, char **argv) { tmptok = (tmptok == NULL) ? strtok(NULL,"\n") : tmptok; tmp.bitmask[i] = (uint8_t *)((atoi(tmptok) <= tmp.h) ? atoi(tmptok) : tmp.h); } - } - - + } + /* + * 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); (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.h; i++) - printf((i<floor.h-1) ? "%u, " : ("%u\n"), floor.bitmask[i]); + printf((i < floor.h-1 && i % 16 != 0 || i == 0) ? "%u, " : ("%u\n"), floor.bitmask[i]); if (colltype == PPX) { @@ -167,8 +203,6 @@ int main(int argc, char **argv) { /* Check for floating point coordinates. */ uint8_t flt; flt = (strchr(buf, '.') != NULL) ? 1 : 0; - if (verbose >= 1) - printf("flt: %u\n", flt); if (flt) { tmp.pos.fx = atof(strtok(buf,",")); if(postype) @@ -183,6 +217,7 @@ int main(int argc, char **argv) { } } else { tmp.pos.x = atoi(strtok(buf,",")); + /* Are we using 3D coordinates, or 2D coordinats? */ if(postype) tmp.pos.y = atoi(strtok(NULL,",")); tmp2 = strtok(NULL, "\n"); @@ -201,18 +236,37 @@ int main(int argc, char **argv) { cursor.xvel = atof(strtok(buf, ",")); cursor.yvel = atof(strtok(NULL, "\n")); } - /* Axis Aligned Bounding Box Collision. */ - if (colltype == AABB && coll) { + + /* Tile Heightmap Collision. */ + if (colltype == THM && coll) { if (!flt) { cursor.a1 = cursor.pos.x; cursor.a2 = cursor.pos.x+cursor.w; cursor.b1 = cursor.pos.y; cursor.b2 = cursor.pos.y+cursor.h; floor.a1 = floor.pos.x; - floor.a2 = floor.pos.x+floor.w; - floor.b1 = floor.pos.y; - floor.b2 = floor.pos.y+floor.h; + floor.a2 = floor.pos.x+floor.w; + floor.b1 = floor.pos.y; + floor.b2 = floor.pos.y+floor.h; + + /* Check for a collision, on the y axis. */ + cursor.cly = ((cursor.b2 >= floor.b1-(int)cursor.yvel && cursor.a2 >= floor.a1) && (cursor.b1 <= floor.b2-(int)cursor.yvel && cursor.a1 <= floor.a2)) ? 1 : 0; + + int newy = 0; + if (cursor.cly) { + if (cursor.a2-floor.a1 < floor.h) { + newy = (int)floor.bitmask[cursor.a2-floor.a1]; + /* Is the Floor, really a floor? */ + if (cursor.b2 > floor.b1-(int)cursor.yvel-1 && !floor.flp) + cursor.pos.y = floor.pos.y-(cursor.h-newy)-(int)cursor.yvel; + /* Is the Floor, actually a ceiling? */ + else if (cursor.b1 < floor.b2-(int)cursor.yvel+1 && floor.flp) + cursor.pos.y = (floor.pos.y+newy)-(int)cursor.yvel; + } + } + cursor.gnded = (cursor.pos.y == floor.pos.y-(cursor.h-newy)) ? 1 : 0; + printf("%u\n%u\n%i\n%i, %i\n%07.06f, %07.06f\n", cursor.cly, cursor.gnded, newy, cursor.pos.x, cursor.pos.y, cursor.xvel, cursor.yvel); } else { cursor.c1 = cursor.pos.fx; cursor.c2 = cursor.pos.fx+cursor.w; @@ -223,65 +277,105 @@ int main(int argc, char **argv) { floor.d1 = floor.pos.fy; floor.d2 = floor.pos.fy+floor.h; - } - if (!flt) { - cursor.gnded = (cursor.b2 == floor.b1-(int)cursor.yvel+1) ? 1 : 0; - 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; - } else { - cursor.gnded = (cursor.d2 == floor.d1) ? 1 : 0; - cursor.clx = ((cursor.c2 >= floor.c1 && cursor.c1 <= floor.c2) && (cursor.d1+(cursor.h/2) >= floor.b1 && cursor.d1 <= floor.d2)) ? 1 : 0; - cursor.cly = ((cursor.d2 >= floor.d1 && cursor.d1 <= floor.d2) && (cursor.c1+(cursor.w/2) >= floor.c1 && cursor.c1 <= floor.c2)) ? 1 : 0; - } + /* Check for a collision, on the y axis. */ + 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; + int newy = 0; + if (cursor.cly) { + if (cursor.c2-floor.c1 < floor.h) { + newy = (int)floor.bitmask[(int)cursor.c2-(int)floor.c1]; + /* Is the Floor, really a floor? */ + if (cursor.d2 > floor.d1-cursor.yvel-1 && !floor.flp) + cursor.pos.fy = floor.pos.fy-(cursor.h-newy)-cursor.yvel+1; + /* Is the Floor, actually a ceiling? */ + else if (cursor.d1 < floor.d2-cursor.yvel+1 && floor.flp) + cursor.pos.fy = (floor.pos.fy+newy)-cursor.yvel-1; + } + } + cursor.gnded = (cursor.pos.fy == floor.pos.fy-(cursor.h-newy)) ? 1 : 0; + printf("%u\n%u\n%i\n%07.06f, %07.06f\n%07.06f, %07.06f\n", cursor.cly, cursor.gnded, newy, cursor.pos.fx, cursor.pos.fy, cursor.xvel, cursor.yvel); + } + } + + /* Axis Aligned Bounding Box Collision. */ + if (colltype == AABB && coll) { + if (!flt) { + cursor.a1 = cursor.pos.x; + cursor.a2 = cursor.pos.x+cursor.w; + cursor.b1 = cursor.pos.y; + cursor.b2 = cursor.pos.y+cursor.h; + floor.a1 = floor.pos.x; + floor.a2 = floor.pos.x+floor.w; + floor.b1 = floor.pos.y; + floor.b2 = floor.pos.y+floor.h; + /* Is the Cursor is on the ground? */ + 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; - if (verbose >= 1) - printf("cursor.pos.x: %i\ncursor.w: %u, cursor.h: %u\nfloor.pos.x: %i\nfloor.w: %u, floor.h: %u\n", cursor.pos.x, cursor.w, cursor.h, floor.pos.x, floor.w, floor.h); - if(!flt) { if (cursor.clx) { + /* Is the Cursor coming from the left? */ if (cursor.a2 > floor.a1-(int)cursor.xvel-1 && cursor.xvel > 0) cursor.pos.x = (floor.pos.x-(int)cursor.xvel)-cursor.w-1; + /* Is the Cursor coming from the right? */ else if (cursor.a1 < floor.a2-(int)cursor.xvel+1 && cursor.xvel < 0) cursor.pos.x = (floor.pos.x+floor.w)-(int)cursor.xvel+1; cursor.xvel = 0; } if (cursor.cly) { + /* Is the Cursor coming from the top? */ if (cursor.b2 > floor.b1-(int)cursor.yvel-1 && cursor.yvel > 0) cursor.pos.y = (floor.pos.y-(int)cursor.yvel)-cursor.h-1; + /* Is the Cursor coming from the bottom? */ else if (cursor.b1 < floor.b2-(int)cursor.yvel+1 && cursor.yvel < 0) 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 ("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", 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; + cursor.d1 = cursor.pos.fy; + cursor.d2 = cursor.pos.fy+cursor.h; + floor.c1 = floor.pos.fx; + floor.c2 = floor.pos.fx+floor.w; + floor.d1 = floor.pos.fy; + 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; + if (cursor.clx) { - if (cursor.c2 >= floor.c1) - cursor.pos.fx = floor.pos.fx-cursor.w; - else if (cursor.c1 <= floor.c2) - cursor.pos.fx = floor.pos.fx+floor.w; - } else if (cursor.cly) { - if (cursor.d2 >= floor.d1) - cursor.pos.fy = floor.pos.fy-cursor.h; - else if (cursor.d1 <= floor.d2) - cursor.pos.fy = floor.pos.fy+floor.h; + if (cursor.c2 > floor.c1-cursor.xvel-1 && cursor.xvel > 0) + cursor.pos.fx = (floor.pos.fx-cursor.xvel)-(double)cursor.w-1; + else if (cursor.c1 < floor.c2-cursor.xvel+1 && cursor.xvel < 0) + cursor.pos.fx = (floor.pos.fx+(double)floor.w)-cursor.xvel+1; + cursor.xvel = 0; + } + if (cursor.cly) { + if (cursor.d2 > floor.d1-cursor.yvel-1 && cursor.yvel > 0) + cursor.pos.fy = (floor.pos.fy-cursor.yvel)-(double)cursor.h-1; + else if (cursor.d1 < floor.d2-cursor.yvel+1 && cursor.yvel < 0) + 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", cursor.clx, cursor.cly, cursor.gnded, cursor.pos.fx, cursor.pos.fy); + 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); } } } diff --git a/test-mvmt.sh b/test-mvmt.sh index 0f5ec67..b53ed56 100755 --- a/test-mvmt.sh +++ b/test-mvmt.sh @@ -7,20 +7,26 @@ yvel=1 yspd=$yvel x2=256 y2=256 -w=16 -h=16 +w1=16 +h1=16 +w2=128 +h2=128 +u=0 +d=0 +l=0 +r=0 clear actkbd -Ps | \ while read frame key held; do yspd=0 && xspd=0 && negx="" && negy=""; - [ "${key#*KEY_W}" != "$key" -o "${key#*KEY_S}" != "$key" ] && yspd=$yvel - [ "${key#*KEY_A}" != "$key" -o "${key#*KEY_D}" != "$key" ] && xspd=$xvel - [ "${key#*KEY_W}" != "$key" ] && negy="-" && y1=$(expr $y1 - $yspd) - [ "${key#*KEY_S}" != "$key" ] && negy="" && y1=$(expr $y1 + $yspd) - [ "${key#*KEY_A}" != "$key" ] && negx="-" && x1=$(expr $x1 - $xspd) - [ "${key#*KEY_D}" != "$key" ] && negx="" && x1=$(expr $x1 + $xspd) - xy=$(printf "1\n$w,$h\n$x1,$y1\n$negx$xspd,$negy$yspd\n128,128\n$x2,$y2" | ./clld | tail -n2 | tr -d ',' | head -n1) + [ "${key#*KEY_W}" != "$key" -o "${key#*KEY_S}" != "$key" ] && yspd=$yvel + [ "${key#*KEY_A}" != "$key" -o "${key#*KEY_D}" != "$key" ] && xspd=$xvel + [ "${key#*KEY_W}" != "$key" ] && negy="-" && y1=$(echo "$y1-$yspd" | bc) + [ "${key#*KEY_S}" != "$key" ] && negy="" && y1=$(echo "$y1+$yspd" | bc) + [ "${key#*KEY_A}" != "$key" ] && negx="-" && x1=$(echo "$x1-$xspd" | bc) + [ "${key#*KEY_D}" != "$key" ] && negx="" && x1=$(echo "$x1+$xspd" | bc) + xy=$(printf "1\n$w1,$h1\n$x1,$y1\n$negx$xspd,$negy$yspd\n$w2,$h2\n$x2,$y2" | ./clld | tail -n2 | tr -d ',' | head -n1) x1=$(echo "$xy" | awk '{print $1}') y1=$(echo "$xy" | awk '{print $2}') - printf "\33[1;1H$x1 $y1 \33[2;1H$negx$xspd $negy$yspd \b\b" + printf "\33[1;1H$x1 $y1 \33[2;1H$negx$xspd $negy$yspd \33[3;1H \b\b" done diff --git a/test-thm-mvmt.sh b/test-thm-mvmt.sh new file mode 100755 index 0000000..ab3678e --- /dev/null +++ b/test-thm-mvmt.sh @@ -0,0 +1,36 @@ +#!/bin/sh +x1=234 +y1=234 +xvel=1 +xspd=$xvel +yvel=1 +yspd=$yvel +x2=256 +y2=256 +flip=0 +heightmap="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1" +#heightmap="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16" +#heightmap="16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1" +w1=16 +w2=64 +h1=16 +h2=64 +u=0 +d=0 +l=0 +r=0 +clear +actkbd -Ps | \ +while read frame key held; do + yspd=0 && xspd=0 && negx="" && negy=""; + [ "${key#*KEY_W}" != "$key" -o "${key#*KEY_S}" != "$key" ] && yspd=$yvel + [ "${key#*KEY_A}" != "$key" -o "${key#*KEY_D}" != "$key" ] && xspd=$xvel + [ "${key#*KEY_W}" != "$key" ] && negy="-" && y1=$(echo "$y1-$yspd" | bc) + [ "${key#*KEY_S}" != "$key" ] && negy="" && y1=$(echo "$y1+$yspd" | bc) + [ "${key#*KEY_A}" != "$key" ] && negx="-" && x1=$(echo "$x1-$xspd" | bc) + [ "${key#*KEY_D}" != "$key" ] && negx="" && x1=$(echo "$x1+$xspd" | bc) + xy=$(printf "0\n$w1,$h1\n$x1,$y1\n$negx$xspd,$negy$yspd\n$w2,$h2\n$flip\n$heightmap\n$x2,$y2" | ./clld | tail -n2 | tr -d ',' | head -n1) + x1=$(echo "$xy" | awk '{print $1}') + y1=$(echo "$xy" | awk '{print $2}') + printf "\33[1;1H$x1 $y1 \33[2;1H$negx$xspd $negy$yspd \33[3;1H \b\b" +done @@ -1,6 +1,8 @@ 0 -1,1 -1,2,3 16,16 +17,32 +1,0 +16,16 +0 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 -4,5,6 +32,32 |