summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2019-10-01 18:03:37 -0400
committermrb0nk500 <b0nk@b0nk.xyz>2019-10-01 18:03:37 -0400
commit2637626b7b1a9010d04ae547ab4b03b7541bfb81 (patch)
treedecc9df1207b2f721b84617c0e72fec1c4f7ad83
parented304ec3db325f6e500fd8454f383f0d6f2ab167 (diff)
Added Axis Aligned Bounding Box collision.
-rw-r--r--clld.c141
-rw-r--r--test-aabb5
-rwxr-xr-xtest-mvmt.sh20
3 files changed, 144 insertions, 22 deletions
diff --git a/clld.c b/clld.c
index 446b775..160c250 100644
--- a/clld.c
+++ b/clld.c
@@ -52,8 +52,17 @@ int main(int argc, char **argv) {
unsigned int w;
unsigned int h;
struct pos pos;
+ int a1;
+ int a2;
+ int b1;
+ int b2;
+ double c1;
+ double c2;
+ double d1;
+ double d2;
uint8_t **bitmask;
- unsigned int clds : 1;
+ unsigned int clx : 1;
+ unsigned int cly : 1;
unsigned int gnded : 1;
};
@@ -119,30 +128,32 @@ int main(int argc, char **argv) {
tmp.h = (newheight >= tmp.h) ? tmp.h : newheight;
for (unsigned int i = 0; i <tmp.h; i++) {
char *tmptok = strtok((i == 0) ? buf : NULL,",");
- if(atoi(tmptok) <= tmp.h)
- tmp.bitmask[i] = (uint8_t *)atoi((tmptok == NULL ? strtok(NULL,"\n") : tmptok));
+ tmptok = (tmptok == NULL) ? strtok(NULL,"\n") : tmptok;
+ tmp.bitmask[i] = (uint8_t *)((atoi(tmptok) <= tmp.h) ? atoi(tmptok) : tmp.h);
}
}
+
(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);
-
- if (colltype == THM && coll)
- for (unsigned int i = 0; i<floor.h; i++)
- printf((i<floor.h-1) ? "%u, " : ("%u\n"), floor.bitmask[i]);
+ 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]);
- if (colltype == PPX) {
- for (unsigned int j = 0; j<((coll) ? floor.h : cursor.h); j++) {
- for (unsigned int i = 0; i<((coll) ? floor.w : cursor.w); i++)
- if((coll) ? floor.bitmask[j][i] : cursor.bitmask[j][i])
- printf("%u", (coll) ? floor.bitmask[j][i] : cursor.bitmask[j][i]);
- else
- printf(" ");
- printf("\n");
+ if (colltype == PPX) {
+ for (unsigned int j = 0; j<((coll) ? floor.h : cursor.h); j++) {
+ for (unsigned int i = 0; i<((coll) ? floor.w : cursor.w); i++)
+ if((coll) ? floor.bitmask[j][i] : cursor.bitmask[j][i])
+ printf("%u", (coll) ? floor.bitmask[j][i] : cursor.bitmask[j][i]);
+ else
+ printf(" ");
+ printf("\n");
+ }
}
}
@@ -156,16 +167,22 @@ int main(int argc, char **argv) {
delms++;
postype = (delms == 2) ? 1 : 0;
/* Check for floating point coordinates. */
- if (strchr(buf, '.') != NULL) {
+ 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)
tmp.pos.fy = atof(strtok(NULL,","));
tmp2 = strtok(NULL, "\n");
TERASGN(postype, tmp.pos.fz, tmp.pos.fy, atof((tmp2 == NULL ? strtok(NULL,"\0") : tmp2)));
(coll) ? (floor.pos = tmp.pos) : (cursor.pos = tmp.pos);
- printf("%07.06f, %07.06f%s", (!coll) ? cursor.pos.fx : floor.pos.fx, (!coll) ? cursor.pos.fy : floor.pos.fy, (postype) ? ", " : "\n");
- if(postype)
- printf("%07.06f\n", (!coll) ? cursor.pos.fz : floor.pos.fz);
+ if (verbose >= 1) {
+ printf("%07.06f, %07.06f%s", (!coll) ? cursor.pos.fx : floor.pos.fx, (!coll) ? cursor.pos.fy : floor.pos.fy, (postype) ? ", " : "\n");
+ if(postype)
+ printf("%07.06f\n", (!coll) ? cursor.pos.fz : floor.pos.fz);
+ }
} else {
tmp.pos.x = atoi(strtok(buf,","));
if(postype)
@@ -173,9 +190,89 @@ int main(int argc, char **argv) {
tmp2 = strtok(NULL, "\n");
TERASGN(postype, tmp.pos.z, tmp.pos.y, atoi((tmp2 == NULL ? strtok(NULL,"\0") : tmp2)));
(coll) ? (floor.pos = tmp.pos) : (cursor.pos = tmp.pos);
- printf("%i, %i%s", (!coll) ? cursor.pos.x : floor.pos.x, (!coll) ? cursor.pos.y : floor.pos.y, (postype) ? ", " : "\n");
- if(postype)
- printf("%i\n", (!coll) ? cursor.pos.z : floor.pos.z);
+ if (verbose >= 1) {
+ printf("%i, %i%s", (!coll) ? cursor.pos.x : floor.pos.x, (!coll) ? cursor.pos.y : floor.pos.y, (postype) ? ", " : "\n");
+ if(postype)
+ printf("%i\n", (!coll) ? cursor.pos.z : floor.pos.z);
+ }
+ }
+ 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;
+
+ } 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;
+
+ }
+ if (!flt) {
+ cursor.gnded = (cursor.b2 == floor.b1) ? 1 : 0;
+ cursor.clx = ((cursor.a2 >= floor.a1 && cursor.a1 <= floor.a2) && (cursor.b1+(cursor.h/2) >= floor.b1 && cursor.b2-(cursor.h/2) <= floor.b2)) ? 1 : 0;
+ cursor.cly = ((cursor.b2 >= floor.b1 && cursor.b1 <= floor.b2) && (cursor.a1+(cursor.w/2) >= floor.a1 && cursor.a2-(cursor.w/2) <= 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;
+ }
+
+
+
+
+ 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) {
+ if (cursor.a2 >= floor.a1 && cursor.a2 < floor.a2)
+ cursor.pos.x = floor.pos.x-cursor.w;
+ else if (cursor.a1 <= floor.a2 && cursor.a1 > floor.a1)
+ cursor.pos.x = floor.pos.x+floor.w;
+ }
+ if (cursor.cly) {
+ if (cursor.b2 >= floor.b1 && cursor.b2 < floor.b2)
+ cursor.pos.y = floor.pos.y-cursor.h;
+ else if (cursor.b1 <= floor.b2 && cursor.b1 > floor.b1)
+ cursor.pos.y = floor.pos.y+floor.h;
+ }
+ 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("%u, %u\n%u\n%i, %i\n", cursor.clx, cursor.cly, cursor.gnded, cursor.pos.x, cursor.pos.y);
+
+ } else {
+ 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;
+ }
+ 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 (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);
+ }
}
}
uint8_t a = 0;
diff --git a/test-aabb b/test-aabb
new file mode 100644
index 0000000..eca39e4
--- /dev/null
+++ b/test-aabb
@@ -0,0 +1,5 @@
+1
+16,16
+36,7
+16,16
+32,16
diff --git a/test-mvmt.sh b/test-mvmt.sh
new file mode 100755
index 0000000..4f60333
--- /dev/null
+++ b/test-mvmt.sh
@@ -0,0 +1,20 @@
+#!/bin/sh
+x1=128
+y1=128
+x2=256
+y2=256
+w=16
+h=16
+actkbd -Ps | \
+while read frame key held; do
+ echo "$x1 $y1"
+ #key1=$(echo "$key" | awk '{split($0, a, "+"); print a[1];}')
+ #key2=$(echo "$key" | awk '{split($0, a, "+"); print a[2];}')
+ [ "$key" = "KEY_W" ] && x1=$(expr $x1 - 1)
+ [ "$key" = "KEY_S" ] && x1=$(expr $x1 + 1)
+ [ "$key" = "KEY_A" ] && y1=$(expr $y1 - 1)
+ [ "$key" = "KEY_D" ] && y1=$(expr $y1 + 1)
+ xy=$(printf "1\n$w,$h\n$x1,$y1\n$w,$h\n$x2,$y2" | ./clld | tail -n1 | tr -d ',');
+ x1=$(echo "$xy" | awk '{print $1}');
+ y1=$(echo "$xy" | awk '{print $2}');
+done