summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--clld-bench.c171
-rw-r--r--clld.c167
-rw-r--r--test-aabb6
3 files changed, 137 insertions, 207 deletions
diff --git a/clld-bench.c b/clld-bench.c
index ab81176..12eb8ec 100644
--- a/clld-bench.c
+++ b/clld-bench.c
@@ -100,116 +100,90 @@ int clx(struct colis c, struct colis f, int xvel, uint8_t flt) {
}
int cly(struct colis c, struct colis f, int yvel, uint8_t flt) {
- int b = (!flt) ? (c.b2 > f.b1-yvel-1 && c.a2 > f.a1) : (c.d2 > f.d1-c.yvel-1 && c.c2 > f.c1);
- int t = (!flt) ? (c.b1 < f.b2-yvel+1 && c.a1 < f.a2) : (c.d1 < f.d2-c.yvel+1 && c.c1 < f.c2);
- return b && t;
+ int t = (!flt) ? (c.b2 > f.b1-yvel-1 && c.a2 > f.a1) : (c.d2 > f.d1-c.yvel-1 && c.c2 > f.c1);
+ int b = (!flt) ? (c.b1 < f.b2-yvel+1 && c.a1 < f.a2) : (c.d1 < f.d2-c.yvel+1 && c.c1 < f.c2);
+ return t && b;
}
/* Tile Heightmap Collision. */
int thm(uint8_t flt, int hmpeak, struct colis cursor, struct colis floor) {
int newy = 0;
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;
-
-
- cursor.clx = ((cursor.a2 > floor.a1-(int)cursor.xvel-1 && cursor.b2 > floor.b1) && (cursor.a1 < (floor.a2-cursor.w)-(int)cursor.xvel+1 && cursor.b1 < floor.b2)) ? 1 : 0;
- /* Check for a collision, on the y axis. */
- cursor.cly = ((cursor.b2 >= (floor.b1-hmpeak)-(int)cursor.yvel && cursor.a2 >= floor.a1) && (cursor.b1 <= (floor.b2+hmpeak)-(int)cursor.yvel && cursor.a1 <= floor.a2)) ? 1 : 0;
+ cursor.clx = (cursor.a2 > floor.a1-(int)cursor.xvel-1 && cursor.b2 > floor.b1) && (cursor.a1 < (floor.a2-cursor.w)-(int)cursor.xvel+1 && cursor.b1 < floor.b2);
+ cursor.cly = (cursor.b2 >= (floor.b1-hmpeak)-(int)cursor.yvel && cursor.a2 >= floor.a1) && (cursor.b1 <= (floor.b2+hmpeak)-(int)cursor.yvel && cursor.a1 <= floor.a2);
+ uint8_t l;
+ uint8_t r;
+ uint8_t t;
+ uint8_t b;
+ if (!floor.flp) {
+ l = (cursor.a2 > floor.a1-(int)cursor.xvel-1 && floor.b2-cursor.b2 < (int)floor.heightmap[0] && cursor.xvel > 0);
+ r = (cursor.a1 < (floor.a2-cursor.w)-(int)cursor.xvel+1 && floor.b2-cursor.b2 < (int)floor.heightmap[floor.w-1] && cursor.xvel < 0);
+ t = (cursor.b2 > (floor.b2-hmpeak)-(int)cursor.yvel && floor.b2-cursor.b2 >= 0);
+ b = (cursor.b1 < floor.b2-(int)cursor.yvel+1 && floor.b2-cursor.b1 <= 0);
+ } else {
+ l = (cursor.a2 > floor.a1-(int)cursor.xvel-1 && cursor.b1-floor.b1 < 0 && cursor.xvel > 0);
+ r = (cursor.a1 < (floor.a2-cursor.w)-(int)cursor.xvel+1 && cursor.b1-floor.b1 < 0 && cursor.xvel < 0);
+ t = (cursor.b2 > floor.b1-(int)cursor.yvel-1 && cursor.b2-floor.b1 < 0 && cursor.yvel > 0);
+ b = (cursor.b1 < (floor.b1+hmpeak)-(int)cursor.yvel && cursor.b1-floor.b1 >= 0);
+ }
if (cursor.clx) {
if (cursor.a2-floor.a1 < 0 || cursor.a2-floor.a1 > 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)
- 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)
- 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)
- cursor.pos.x = (floor.a2-cursor.w)-(int)cursor.xvel+1;
- else if (cursor.a2 > floor.a1-(int)cursor.xvel-1 && cursor.b1-floor.b1 < 0 && floor.flp && cursor.xvel > 0)
- cursor.pos.x = (floor.pos.x-(int)cursor.xvel)-cursor.w-1;
+ if (l)
+ (!floor.flp) ? (cursor.pos.x = (floor.pos.x-(int)cursor.xvel)-cursor.w-1) : (cursor.pos.x = (floor.a2-cursor.w)-(int)cursor.xvel+1);
+ else if (r)
+ (!floor.flp) ? (cursor.pos.x = (floor.a2-cursor.w)-(int)cursor.xvel+1) : (cursor.pos.x = (floor.pos.x-(int)cursor.xvel)-cursor.w-1);
cursor.xvel = 0;
}
}
if (cursor.cly) {
if (cursor.a2-floor.a1 < floor.w) {
- cursor.yvel = 0;
newy = (int)floor.heightmap[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;
- else if (cursor.b1 < floor.b2-(int)cursor.yvel+1 && floor.b2-cursor.b1 <= 0 && !floor.flp)
- cursor.pos.y = (floor.pos.y+floor.h)-(int)cursor.yvel+1;
- /* Is the Floor, actually a ceiling? */
- if (cursor.b1 < (floor.b1+hmpeak)-(int)cursor.yvel && cursor.b1-floor.b1 >= 0 && floor.flp)
- cursor.pos.y = (floor.pos.y+newy)-(int)cursor.yvel;
- else if (cursor.b2 > floor.b1-(int)cursor.yvel-1 && cursor.b2-floor.b1 < 0 && cursor.yvel > 0 && floor.flp)
- cursor.pos.y = (floor.pos.y-(int)cursor.yvel-1)-cursor.h;
+ if (t)
+ (!floor.flp) ? (cursor.pos.y = ((floor.b2-cursor.h)-newy)-(int)cursor.yvel) : (cursor.pos.y = (floor.pos.y+newy)-(int)cursor.yvel);
+ else if (b)
+ (!floor.flp) ? (cursor.pos.y = (floor.pos.y+floor.h)-(int)cursor.yvel+1) : (cursor.pos.y = (floor.pos.y+newy)-(int)cursor.yvel);
+ cursor.yvel = 0;
}
}
cursor.gnded = (cursor.pos.y == ((floor.b2-cursor.h)-newy)-(int)cursor.yvel) ? 1 : 0;
- if (verbose >= 1) {
- printf(
- "cursor.a1-(floor.a2-(floor.w/2)): %i\n"
- "cursor.a2-(floor.a2-(floor.w/2)): %i\n"
- "(cursor.a2-(cursor.w/2))-(floor.a2-(floor.w/2)): %i\n"
- "(cursor.a2-(cursor.w/2)): %i\n"
- , cursor.a1-(floor.a2-(floor.w/2))
- , cursor.a2-(floor.a2-(floor.w/2))
- , (cursor.a2-(cursor.w/2))-(floor.a2-(floor.w/2))
- , (cursor.a2-(cursor.w/2)));
- }
printf("%u, %u\n%u\n%i\n%i, %i\n%07.06f, %07.06f\n", cursor.clx, 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;
- 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.clx = ((cursor.c2 > floor.c1-cursor.xvel-1 && cursor.d2 > floor.d1) && (cursor.c1 < (floor.c2-cursor.w)-cursor.xvel+1 && cursor.d1 < floor.d2)) ? 1 : 0;
- /* Check for a collision, on the y axis. */
- cursor.cly = ((cursor.d2 >= (floor.d1-hmpeak)-cursor.yvel && cursor.c2 >= floor.c1) && (cursor.d1 <= (floor.d2+hmpeak)-cursor.yvel && cursor.c1 <= floor.c2)) ? 1 : 0;
+ cursor.clx = (cursor.c2 > floor.c1-cursor.xvel-1 && cursor.d2 > floor.d1) && (cursor.c1 < (floor.c2-cursor.w)-cursor.xvel+1 && cursor.d1 < floor.d2);
+ cursor.cly = (cursor.d2 >= (floor.d1-hmpeak)-cursor.yvel && cursor.c2 >= floor.c1) && (cursor.d1 <= (floor.d2+hmpeak)-cursor.yvel && cursor.c1 <= floor.c2);
+ uint8_t l;
+ uint8_t r;
+ uint8_t t;
+ uint8_t b;
+ if (!floor.flp) {
+ l = (cursor.c2 > floor.c1-cursor.xvel-1 && floor.d2-cursor.d2 < (int)floor.heightmap[0] && cursor.xvel > 0);
+ r = (cursor.c1 < (floor.c2-cursor.w)-cursor.xvel+1 && floor.d2-cursor.d2 < (int)floor.heightmap[floor.w-1] && cursor.xvel < 0);
+ t = (cursor.d2 > (floor.d2-hmpeak)-cursor.yvel && floor.d2-cursor.d2 >= 0);
+ b = (cursor.d1 < floor.d2-cursor.yvel+1 && floor.d2-cursor.d1 <= 0);
+ } else {
+ l = (cursor.c2 > floor.c1-cursor.xvel-1 && cursor.d1-floor.d1 < 0 && cursor.xvel > 0);
+ r = (cursor.c1 < (floor.c2-cursor.w)-cursor.xvel+1 && cursor.d1-floor.d1 < 0 && cursor.xvel < 0);
+ t = (cursor.d2 > floor.d1-cursor.yvel-1 && cursor.d2-floor.d1 < 0 && cursor.yvel > 0);
+ b = (cursor.d1 < (floor.d1+hmpeak)-cursor.yvel && cursor.d1-floor.d1 >= 0);
+ }
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)
- 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)
- 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)
- cursor.pos.fx = (floor.c2-cursor.w)-cursor.xvel+1;
- else if (cursor.c2 > floor.c1-cursor.xvel-1 && cursor.d1-floor.d1 < 0 && floor.flp && cursor.xvel > 0)
- cursor.pos.fx = (floor.pos.x-cursor.xvel)-cursor.w-1;
+ if (cursor.c2-floor.c1 < 0 || cursor.c2-floor.c1 > floor.w) {
+ if (l)
+ (!floor.flp) ? (cursor.pos.fx = (floor.pos.fx-cursor.xvel)-cursor.w-1) : (cursor.pos.fx = (floor.c2-cursor.w)-cursor.xvel+1);
+ else if (r)
+ (!floor.flp) ? (cursor.pos.fx = (floor.c2-cursor.w)-cursor.xvel+1) : (cursor.pos.fx = (floor.pos.fx-cursor.xvel)-cursor.w-1);
cursor.xvel = 0;
}
}
if (cursor.cly) {
- if (cursor.c2-floor.c1 < floor.w) {
- cursor.yvel = 0;
+ if (cursor.a2-floor.a1 < floor.w) {
newy = (int)floor.heightmap[(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;
- else if (cursor.d1 < floor.d2-cursor.yvel+1 && floor.d2-cursor.d1 <= 0 && !floor.flp)
- cursor.pos.fy = (floor.d2)-cursor.yvel+1;
- /* Is the Floor, actually a ceiling? */
- if (cursor.d1 < (floor.d1+hmpeak)-cursor.yvel && cursor.d1-floor.d1 >= 0 && floor.flp)
- cursor.pos.fy = (floor.pos.fy+newy)-cursor.yvel;
- else if (cursor.d2 > floor.d1-cursor.yvel-1 && cursor.d2-floor.d1 < 0 && cursor.yvel > 0 && floor.flp)
- cursor.pos.fy = (floor.pos.fy-cursor.yvel-1)-cursor.h;
+ if (t)
+ (!floor.flp) ? (cursor.pos.fy = ((floor.d2-cursor.h)-newy)-cursor.yvel) : (cursor.pos.fy = (floor.pos.fy+newy)-cursor.yvel);
+ else if (b)
+ (!floor.flp) ? (cursor.pos.fy = (floor.pos.fy+floor.h)-cursor.yvel+1) : (cursor.pos.fy = (floor.pos.fy+newy)-cursor.yvel);
+ cursor.yvel = 0;
}
}
cursor.gnded = (cursor.pos.fy == ((floor.d2-cursor.h)-newy)-cursor.yvel) ? 1 : 0;
@@ -221,14 +195,6 @@ int thm(uint8_t flt, int hmpeak, struct colis cursor, struct colis floor) {
/* Axis Aligned Bounding Box Collision. */
int aabb(uint8_t flt, struct colis cursor, struct colis floor) {
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;
@@ -256,15 +222,6 @@ int aabb(uint8_t flt, struct colis cursor, struct colis floor) {
}
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 = clx(cursor, floor, 0, flt);
cursor.cly = cly(cursor, floor, 0, flt);
@@ -339,10 +296,10 @@ int main(int argc, char **argv) {
colltype = AABB;
cursor.h = 16;
cursor.w = 16;
- cursor.pos.x = 24;
- cursor.pos.y = 15;
+ cursor.pos.x = 17;
+ cursor.pos.y = 32;
cursor.xvel = 0;
- cursor.yvel = 1;
+ cursor.yvel = 0;
floor.w = 16;
floor.h = 16;
floor.flp = 0;
@@ -356,6 +313,14 @@ int main(int argc, char **argv) {
uint8_t flt = 0;
floor.pos.x = 32;
floor.pos.y = 32;
+ (!flt) ? (cursor.a1 = cursor.pos.x) : (cursor.c1 = cursor.pos.fx);
+ (!flt) ? (cursor.a2 = cursor.pos.x+cursor.w) : (cursor.c2 = cursor.pos.fx+cursor.w);
+ (!flt) ? (cursor.b1 = cursor.pos.y) : (cursor.d1 = cursor.pos.fy);
+ (!flt) ? (cursor.b2 = cursor.pos.y+cursor.h) : (cursor.d2 = cursor.pos.fy+cursor.h);
+ (!flt) ? (floor.a1 = floor.pos.x) : (floor.c1 = floor.pos.fx);
+ (!flt) ? (floor.a2 = floor.pos.x+floor.w) : (floor.c2 = floor.pos.fx+floor.w);
+ (!flt) ? (floor.b1 = floor.pos.y) : (floor.d1 = floor.pos.fy);
+ (!flt) ? (floor.b2 = floor.pos.y+floor.h) : (floor.d2 = floor.pos.fy+floor.h);
if (colltype == THM)
thm(flt, hmpeak, cursor, floor);
diff --git a/clld.c b/clld.c
index 73043a5..4c21090 100644
--- a/clld.c
+++ b/clld.c
@@ -100,104 +100,90 @@ int clx(struct colis c, struct colis f, int xvel, uint8_t flt) {
}
int cly(struct colis c, struct colis f, int yvel, uint8_t flt) {
- int b = (!flt) ? (c.b2 > f.b1-yvel-1 && c.a2 > f.a1) : (c.d2 > f.d1-c.yvel-1 && c.c2 > f.c1);
- int t = (!flt) ? (c.b1 < f.b2-yvel+1 && c.a1 < f.a2) : (c.d1 < f.d2-c.yvel+1 && c.c1 < f.c2);
- return b && t;
+ int t = (!flt) ? (c.b2 > f.b1-yvel-1 && c.a2 > f.a1) : (c.d2 > f.d1-c.yvel-1 && c.c2 > f.c1);
+ int b = (!flt) ? (c.b1 < f.b2-yvel+1 && c.a1 < f.a2) : (c.d1 < f.d2-c.yvel+1 && c.c1 < f.c2);
+ return t && b;
}
/* Tile Heightmap Collision. */
int thm(uint8_t flt, int hmpeak, struct colis cursor, struct colis floor) {
int newy = 0;
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;
-
- cursor.clx = ((cursor.a2 > floor.a1-(int)cursor.xvel-1 && cursor.b2 > floor.b1) && (cursor.a1 < (floor.a2-cursor.w)-(int)cursor.xvel+1 && cursor.b1 < floor.b2)) ? 1 : 0;
- /* Check for a collision, on the y axis. */
- cursor.cly = ((cursor.b2 >= (floor.b1-hmpeak)-(int)cursor.yvel && cursor.a2 >= floor.a1) && (cursor.b1 <= (floor.b2+hmpeak)-(int)cursor.yvel && cursor.a1 <= floor.a2)) ? 1 : 0;
+ cursor.clx = (cursor.a2 > floor.a1-(int)cursor.xvel-1 && cursor.b2 > floor.b1) && (cursor.a1 < (floor.a2-cursor.w)-(int)cursor.xvel+1 && cursor.b1 < floor.b2);
+ cursor.cly = (cursor.b2 >= (floor.b1-hmpeak)-(int)cursor.yvel && cursor.a2 >= floor.a1) && (cursor.b1 <= (floor.b2+hmpeak)-(int)cursor.yvel && cursor.a1 <= floor.a2);
+ uint8_t l;
+ uint8_t r;
+ uint8_t t;
+ uint8_t b;
+ if (!floor.flp) {
+ l = (cursor.a2 > floor.a1-(int)cursor.xvel-1 && floor.b2-cursor.b2 < (int)floor.heightmap[0] && cursor.xvel > 0);
+ r = (cursor.a1 < (floor.a2-cursor.w)-(int)cursor.xvel+1 && floor.b2-cursor.b2 < (int)floor.heightmap[floor.w-1] && cursor.xvel < 0);
+ t = (cursor.b2 > (floor.b2-hmpeak)-(int)cursor.yvel && floor.b2-cursor.b2 >= 0);
+ b = (cursor.b1 < floor.b2-(int)cursor.yvel+1 && floor.b2-cursor.b1 <= 0);
+ } else {
+ l = (cursor.a2 > floor.a1-(int)cursor.xvel-1 && cursor.b1-floor.b1 < 0 && cursor.xvel > 0);
+ r = (cursor.a1 < (floor.a2-cursor.w)-(int)cursor.xvel+1 && cursor.b1-floor.b1 < 0 && cursor.xvel < 0);
+ t = (cursor.b2 > floor.b1-(int)cursor.yvel-1 && cursor.b2-floor.b1 < 0 && cursor.yvel > 0);
+ b = (cursor.b1 < (floor.b1+hmpeak)-(int)cursor.yvel && cursor.b1-floor.b1 >= 0);
+ }
if (cursor.clx) {
if (cursor.a2-floor.a1 < 0 || cursor.a2-floor.a1 > 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)
- 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)
- 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)
- cursor.pos.x = (floor.a2-cursor.w)-(int)cursor.xvel+1;
- else if (cursor.a2 > floor.a1-(int)cursor.xvel-1 && cursor.b1-floor.b1 < 0 && floor.flp && cursor.xvel > 0)
- cursor.pos.x = (floor.pos.x-(int)cursor.xvel)-cursor.w-1;
+ if (l)
+ (!floor.flp) ? (cursor.pos.x = (floor.pos.x-(int)cursor.xvel)-cursor.w-1) : (cursor.pos.x = (floor.a2-cursor.w)-(int)cursor.xvel+1);
+ else if (r)
+ (!floor.flp) ? (cursor.pos.x = (floor.a2-cursor.w)-(int)cursor.xvel+1) : (cursor.pos.x = (floor.pos.x-(int)cursor.xvel)-cursor.w-1);
cursor.xvel = 0;
}
}
if (cursor.cly) {
if (cursor.a2-floor.a1 < floor.w) {
- cursor.yvel = 0;
newy = (int)floor.heightmap[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;
- else if (cursor.b1 < floor.b2-(int)cursor.yvel+1 && floor.b2-cursor.b1 <= 0 && !floor.flp)
- cursor.pos.y = (floor.pos.y+floor.h)-(int)cursor.yvel+1;
- /* Is the Floor, actually a ceiling? */
- if (cursor.b1 < (floor.b1+hmpeak)-(int)cursor.yvel && cursor.b1-floor.b1 >= 0 && floor.flp)
- cursor.pos.y = (floor.pos.y+newy)-(int)cursor.yvel;
- else if (cursor.b2 > floor.b1-(int)cursor.yvel-1 && cursor.b2-floor.b1 < 0 && cursor.yvel > 0 && floor.flp)
- cursor.pos.y = (floor.pos.y-(int)cursor.yvel-1)-cursor.h;
+ if (t)
+ (!floor.flp) ? (cursor.pos.y = ((floor.b2-cursor.h)-newy)-(int)cursor.yvel) : (cursor.pos.y = (floor.pos.y+newy)-(int)cursor.yvel);
+ else if (b)
+ (!floor.flp) ? (cursor.pos.y = (floor.pos.y+floor.h)-(int)cursor.yvel+1) : (cursor.pos.y = (floor.pos.y+newy)-(int)cursor.yvel);
+ cursor.yvel = 0;
}
}
cursor.gnded = (cursor.pos.y == ((floor.b2-cursor.h)-newy)-(int)cursor.yvel) ? 1 : 0;
printf("%u, %u\n%u\n%i\n%i, %i\n%07.06f, %07.06f\n", cursor.clx, 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;
- 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.clx = ((cursor.c2 > floor.c1-cursor.xvel-1 && cursor.d2 > floor.d1) && (cursor.c1 < (floor.c2-cursor.w)-cursor.xvel+1 && cursor.d1 < floor.d2)) ? 1 : 0;
- /* Check for a collision, on the y axis. */
- cursor.cly = ((cursor.d2 >= (floor.d1-hmpeak)-cursor.yvel && cursor.c2 >= floor.c1) && (cursor.d1 <= (floor.d2+hmpeak)-cursor.yvel && cursor.c1 <= floor.c2)) ? 1 : 0;
+ cursor.clx = (cursor.c2 > floor.c1-cursor.xvel-1 && cursor.d2 > floor.d1) && (cursor.c1 < (floor.c2-cursor.w)-cursor.xvel+1 && cursor.d1 < floor.d2);
+ cursor.cly = (cursor.d2 >= (floor.d1-hmpeak)-cursor.yvel && cursor.c2 >= floor.c1) && (cursor.d1 <= (floor.d2+hmpeak)-cursor.yvel && cursor.c1 <= floor.c2);
+ uint8_t l;
+ uint8_t r;
+ uint8_t t;
+ uint8_t b;
+ if (!floor.flp) {
+ l = (cursor.c2 > floor.c1-cursor.xvel-1 && floor.d2-cursor.d2 < (int)floor.heightmap[0] && cursor.xvel > 0);
+ r = (cursor.c1 < (floor.c2-cursor.w)-cursor.xvel+1 && floor.d2-cursor.d2 < (int)floor.heightmap[floor.w-1] && cursor.xvel < 0);
+ t = (cursor.d2 > (floor.d2-hmpeak)-cursor.yvel && floor.d2-cursor.d2 >= 0);
+ b = (cursor.d1 < floor.d2-cursor.yvel+1 && floor.d2-cursor.d1 <= 0);
+ } else {
+ l = (cursor.c2 > floor.c1-cursor.xvel-1 && cursor.d1-floor.d1 < 0 && cursor.xvel > 0);
+ r = (cursor.c1 < (floor.c2-cursor.w)-cursor.xvel+1 && cursor.d1-floor.d1 < 0 && cursor.xvel < 0);
+ t = (cursor.d2 > floor.d1-cursor.yvel-1 && cursor.d2-floor.d1 < 0 && cursor.yvel > 0);
+ b = (cursor.d1 < (floor.d1+hmpeak)-cursor.yvel && cursor.d1-floor.d1 >= 0);
+ }
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)
- 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)
- 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)
- cursor.pos.fx = (floor.c2-cursor.w)-cursor.xvel+1;
- else if (cursor.c2 > floor.c1-cursor.xvel-1 && cursor.d1-floor.d1 < 0 && floor.flp && cursor.xvel > 0)
- cursor.pos.fx = (floor.pos.x-cursor.xvel)-cursor.w-1;
+ if (cursor.c2-floor.c1 < 0 || cursor.c2-floor.c1 > floor.w) {
+ if (l)
+ (!floor.flp) ? (cursor.pos.fx = (floor.pos.fx-cursor.xvel)-cursor.w-1) : (cursor.pos.fx = (floor.c2-cursor.w)-cursor.xvel+1);
+ else if (r)
+ (!floor.flp) ? (cursor.pos.fx = (floor.c2-cursor.w)-cursor.xvel+1) : (cursor.pos.fx = (floor.pos.fx-cursor.xvel)-cursor.w-1);
cursor.xvel = 0;
}
}
if (cursor.cly) {
- if (cursor.c2-floor.c1 < floor.w) {
- cursor.yvel = 0;
+ if (cursor.a2-floor.a1 < floor.w) {
newy = (int)floor.heightmap[(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;
- else if (cursor.d1 < floor.d2-cursor.yvel+1 && floor.d2-cursor.d1 <= 0 && !floor.flp)
- cursor.pos.fy = (floor.d2)-cursor.yvel+1;
- /* Is the Floor, actually a ceiling? */
- if (cursor.d1 < (floor.d1+hmpeak)-cursor.yvel && cursor.d1-floor.d1 >= 0 && floor.flp)
- cursor.pos.fy = (floor.pos.fy+newy)-cursor.yvel;
- else if (cursor.d2 > floor.d1-cursor.yvel-1 && cursor.d2-floor.d1 < 0 && cursor.yvel > 0 && floor.flp)
- cursor.pos.fy = (floor.pos.fy-cursor.yvel-1)-cursor.h;
+ if (t)
+ (!floor.flp) ? (cursor.pos.fy = ((floor.d2-cursor.h)-newy)-cursor.yvel) : (cursor.pos.fy = (floor.pos.fy+newy)-cursor.yvel);
+ else if (b)
+ (!floor.flp) ? (cursor.pos.fy = (floor.pos.fy+floor.h)-cursor.yvel+1) : (cursor.pos.fy = (floor.pos.fy+newy)-cursor.yvel);
+ cursor.yvel = 0;
}
}
cursor.gnded = (cursor.pos.fy == ((floor.d2-cursor.h)-newy)-cursor.yvel) ? 1 : 0;
@@ -209,14 +195,6 @@ int thm(uint8_t flt, int hmpeak, struct colis cursor, struct colis floor) {
/* Axis Aligned Bounding Box Collision. */
int aabb(uint8_t flt, struct colis cursor, struct colis floor) {
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;
@@ -242,25 +220,8 @@ int aabb(uint8_t flt, struct colis cursor, struct colis floor) {
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("cursor.w: %i, cursor.h: %i\n", cursor.w, cursor.h);*/
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 = clx(cursor, floor, 0, flt);
cursor.cly = cly(cursor, floor, 0, flt);
@@ -279,11 +240,6 @@ int aabb(uint8_t flt, struct colis cursor, struct colis floor) {
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%07.06f, %07.06f\n", cursor.clx, cursor.cly, cursor.gnded, cursor.pos.fx, cursor.pos.fy, cursor.xvel, cursor.yvel);
}
return 0;
@@ -452,6 +408,15 @@ int main(int argc, char **argv) {
cursor.yvel = atof(strtok(NULL, "\n"));
}
+ (!flt) ? (cursor.a1 = cursor.pos.x) : (cursor.c1 = cursor.pos.fx);
+ (!flt) ? (cursor.a2 = cursor.pos.x+cursor.w) : (cursor.c2 = cursor.pos.fx+cursor.w);
+ (!flt) ? (cursor.b1 = cursor.pos.y) : (cursor.d1 = cursor.pos.fy);
+ (!flt) ? (cursor.b2 = cursor.pos.y+cursor.h) : (cursor.d2 = cursor.pos.fy+cursor.h);
+ (!flt) ? (floor.a1 = floor.pos.x) : (floor.c1 = floor.pos.fx);
+ (!flt) ? (floor.a2 = floor.pos.x+floor.w) : (floor.c2 = floor.pos.fx+floor.w);
+ (!flt) ? (floor.b1 = floor.pos.y) : (floor.d1 = floor.pos.fy);
+ (!flt) ? (floor.b2 = floor.pos.y+floor.h) : (floor.d2 = floor.pos.fy+floor.h);
+
if (colltype == THM && coll)
thm(flt, hmpeak, cursor, floor);
diff --git a/test-aabb b/test-aabb
index f07d7d5..899c7ad 100644
--- a/test-aabb
+++ b/test-aabb
@@ -1,6 +1,6 @@
1
16,16
-36,8
-0,3
+15,18
+1,1
16,16
-32,16
+32,32