From a70c25e33f0b994a3fbb93c6fee5340b7abac1d6 Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Wed, 2 Oct 2019 15:31:24 -0400 Subject: Add x, and y velocity variables, and fixed some bugs in AABB collision. --- clld.c | 35 +++++++++++++++++++++++------------ test-aabb | 3 ++- test-mvmt.sh | 22 ++++++++++++++-------- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/clld.c b/clld.c index 160c250..700f01e 100644 --- a/clld.c +++ b/clld.c @@ -51,6 +51,8 @@ int main(int argc, char **argv) { struct colis { unsigned int w; unsigned int h; + double xvel; + double yvel; struct pos pos; int a1; int a2; @@ -82,7 +84,8 @@ int main(int argc, char **argv) { if (colltype == -1) colltype = atoi(strtok(buf, "\n")); - printf("%i\n", colltype); + if (verbose >= 1) + printf("%i\n", colltype); for (int coll = 0; coll <= 1; coll++) { buf = NULL; getline(&buf, &size, stdin); @@ -196,6 +199,14 @@ int main(int argc, char **argv) { printf("%i\n", (!coll) ? cursor.pos.z : floor.pos.z); } } + /* Get X, and Y velocity. */ + if(!coll) { + buf = NULL; + getline(&buf, &size, stdin); + cursor.xvel = atof(strtok(buf, ",")); + cursor.yvel = atof(strtok(NULL, "\n")); + } + /* Axis Aligned Bounding Box Collision. */ if (colltype == AABB && coll) { if (!flt) { cursor.a1 = cursor.pos.x; @@ -219,7 +230,7 @@ int main(int argc, char **argv) { } if (!flt) { - cursor.gnded = (cursor.b2 == floor.b1) ? 1 : 0; + 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; } else { @@ -234,24 +245,25 @@ 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) { - if (cursor.a2 >= floor.a1 && cursor.a2 < floor.a2) + 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) + else if (cursor.a1 <= floor.a2 && cursor.a1 > floor.a1 && cursor.xvel < 0) cursor.pos.x = floor.pos.x+floor.w; - } - if (cursor.cly) { - if (cursor.b2 >= floor.b1 && cursor.b2 < floor.b2) + } 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) + else if (cursor.b1 <= floor.b2 && cursor.b1 > floor.b1 && cursor.yvel < 0) cursor.pos.y = floor.pos.y+floor.h; } + 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", cursor.clx, cursor.cly, cursor.gnded, cursor.pos.x, cursor.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); } else { if (cursor.clx) { @@ -259,8 +271,7 @@ int main(int argc, char **argv) { 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 ) { + } else if (cursor.cly) { if (cursor.d2 >= floor.d1) cursor.pos.fy = floor.pos.fy-cursor.h; else if (cursor.d1 <= floor.d2) diff --git a/test-aabb b/test-aabb index eca39e4..f07d7d5 100644 --- a/test-aabb +++ b/test-aabb @@ -1,5 +1,6 @@ 1 16,16 -36,7 +36,8 +0,3 16,16 32,16 diff --git a/test-mvmt.sh b/test-mvmt.sh index 4f60333..8ecef47 100755 --- a/test-mvmt.sh +++ b/test-mvmt.sh @@ -1,20 +1,26 @@ #!/bin/sh x1=128 y1=128 +xvel=1 +xspd=$xvel +yvel=1 +yspd=$yvel x2=256 y2=256 w=16 h=16 +clear actkbd -Ps | \ while read frame key held; do - echo "$x1 $y1" + 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_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}'); + [ "$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'); + x1=$(echo "$xy" | awk '{print $1}') + y1=$(echo "$xy" | awk '{print $2}') done -- cgit v1.2.3-13-gbd6f