summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2019-10-04 22:22:20 -0400
committermrb0nk500 <b0nk@b0nk.xyz>2019-10-04 22:22:20 -0400
commitac0e65bc5b4067aa7bcaec29d7dad4fab03516d4 (patch)
tree025398651b1329eb2181fdd4f28e2d84e250a341
parenta70c25e33f0b994a3fbb93c6fee5340b7abac1d6 (diff)
Finally fixed the bugs in Axis Aligned Bounding Box collision.
It was hard for me to figure out what went wrong, but, I was able fix all of those bugs in AABB, also there is now a shell script that I made for testing AABB, called test-mvmt.sh. It requires my fork of actkbd, in order to get keyboard inputs.
-rw-r--r--clld.c45
-rwxr-xr-xtest-mvmt.sh22
2 files changed, 33 insertions, 34 deletions
diff --git a/clld.c b/clld.c
index 700f01e..f32c4b3 100644
--- a/clld.c
+++ b/clld.c
@@ -49,8 +49,8 @@ int main(int argc, char **argv) {
};
struct colis {
- unsigned int w;
- unsigned int h;
+ int w;
+ int h;
double xvel;
double yvel;
struct pos pos;
@@ -74,11 +74,6 @@ int main(int argc, char **argv) {
char *buf = NULL;
size_t size;
- /*getline(&buf, &size, stdin);*/
- /*if (postype == -1)
- postype = atoi(strtok(buf, "\n"));*/
-
- /*buf = NULL;*/
getline(&buf, &size, stdin);
if (colltype == -1)
@@ -230,9 +225,9 @@ int main(int argc, char **argv) {
}
if (!flt) {
- cursor.gnded = (cursor.b2 == floor.b1 && cursor.yvel == 0) ? 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;
+ 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;
@@ -245,25 +240,29 @@ int main(int argc, char **argv) {
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 && cursor.xvel != 0) {
- if (cursor.a2 >= floor.a1 && cursor.a2 < floor.a2 && cursor.xvel > 0)
- cursor.pos.x = floor.pos.x-cursor.w;
- else if (cursor.a1 <= floor.a2 && cursor.a1 > floor.a1 && cursor.xvel < 0)
- cursor.pos.x = floor.pos.x+floor.w;
- } else if (cursor.cly && cursor.yvel !=0) {
- if (cursor.b2 >= floor.b1 && cursor.b2 < floor.b2 && cursor.yvel > 0)
- cursor.pos.y = floor.pos.y-cursor.h;
- else if (cursor.b1 <= floor.b2 && cursor.b1 > floor.b1 && cursor.yvel < 0)
- cursor.pos.y = floor.pos.y+floor.h;
+ if (cursor.clx) {
+ if (cursor.a2 > floor.a1-(int)cursor.xvel-1 && cursor.xvel > 0)
+ cursor.pos.x = (floor.pos.x-(int)cursor.xvel)-cursor.w-1;
+ 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) {
+ if (cursor.b2 > floor.b1-(int)cursor.yvel-1 && cursor.yvel > 0)
+ cursor.pos.y = (floor.pos.y-(int)cursor.yvel)-cursor.h-1;
+ 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 ((cursor.clx && cursor.xvel == 0) && (cursor.cly && cursor.yvel == 0))
- cursor.pos.y = floor.pos.y-cursor.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%07.06f, %07.06f\n", cursor.clx, cursor.cly, cursor.gnded, cursor.pos.x, cursor.pos.y, cursor.xvel, cursor.yvel);
+ 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);
} else {
if (cursor.clx) {
diff --git a/test-mvmt.sh b/test-mvmt.sh
index 8ecef47..0f5ec67 100755
--- a/test-mvmt.sh
+++ b/test-mvmt.sh
@@ -1,6 +1,6 @@
#!/bin/sh
-x1=128
-y1=128
+x1=234
+y1=234
xvel=1
xspd=$xvel
yvel=1
@@ -12,15 +12,15 @@ h=16
clear
actkbd -Ps | \
while read frame key held; do
- printf "\33[1;1H$x1 $y1 \b\b"
- #key1=$(echo "$key" | awk '{split($0, a, "+"); print a[1];}')
- #key2=$(echo "$key" | awk '{split($0, a, "+"); print a[2];}')
- [ "$key" = "KEY_NONE" ] && yspd=0 && xspd=0 && negx="" && negy=""
- [ "${key#*KEY_W}" != "$key" ] && negx="-" && xspd=$xvel && x1=$(expr $x1 - $xspd)
- [ "${key#*KEY_S}" != "$key" ] && negx="" && xspd=$xvel && x1=$(expr $x1 + $xspd)
- [ "${key#*KEY_A}" != "$key" ] && negy="-" && yspd=$yvel && y1=$(expr $y1 - $yspd)
- [ "${key#*KEY_D}" != "$key" ] && negy="" && yspd=$yvel && y1=$(expr $y1 + $yspd)
- xy=$(printf "1\n$w,$h\n$x1,$y1\n$negx$xspd,$negy$yspd\n$w,$h\n$x2,$y2" | ./clld | tail -n2 | tr -d ',' | sed '$d');
+ 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)
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"
done