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);