From 06f0a4962640e577bec5897c852ad2dcd8d82e03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Sun, 2 Sep 2018 21:43:44 +0200 Subject: [PATCH] Allow negative coordinates --- raycastlib.h | 15 +++++++++++++-- test.c | 9 +++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/raycastlib.h b/raycastlib.h index 26fc3c0..f524fb1 100644 --- a/raycastlib.h +++ b/raycastlib.h @@ -404,6 +404,12 @@ void castRayMultiHit(Ray ray, ArrayFunction arrayFunc, HitResult *hitResults, currentSquare.x = ray.start.x / UNITS_PER_SQUARE; currentSquare.y = ray.start.y / UNITS_PER_SQUARE; + if (ray.start.x < 0) // round down, not toward zero + currentSquare.x--; + + if (ray.start.y < 0) + currentSquare.y--; + *hitResultsLen = 0; int16_t squareType = arrayFunc(currentSquare.x,currentSquare.y); @@ -446,8 +452,13 @@ void castRayMultiHit(Ray ray, ArrayFunction arrayFunc, HitResult *hitResults, break; } - ray.start.x = currentPos.x % UNITS_PER_SQUARE; - ray.start.y = currentPos.y % UNITS_PER_SQUARE; + ray.start.x = currentPos.x < 0 ? + (UNITS_PER_SQUARE + currentPos.x % UNITS_PER_SQUARE - 1) : + (currentPos.x % UNITS_PER_SQUARE); + + ray.start.y = currentPos.y < 0 ? + (UNITS_PER_SQUARE + currentPos.y % UNITS_PER_SQUARE - 1) : + (currentPos.y % UNITS_PER_SQUARE); castRaySquare(ray,&no,&co); diff --git a/test.c b/test.c index ad3ae49..1154f9a 100644 --- a/test.c +++ b/test.c @@ -124,6 +124,15 @@ int main() 16)) return 1; + if (!testSingleRay( + -4 * UNITS_PER_SQUARE - UNITS_PER_SQUARE / 2, + 7 * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 3, + 100,-100, + 0, 2, + 1, 2900, + 16)) + return 1; + for (Unit i = -UNITS_PER_SQUARE; i <= UNITS_PER_SQUARE; i += 64) { Unit v = sinInt(i);