From 296868005d86579369c0804acd69ba988820cc4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Sat, 22 Aug 2020 20:00:28 +0200 Subject: [PATCH] Fix render bug --- raycastlib.h | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/raycastlib.h b/raycastlib.h index 7683080..4c32116 100644 --- a/raycastlib.h +++ b/raycastlib.h @@ -26,7 +26,7 @@ author: Miloslav "drummyfish" Ciz license: CC0 1.0 - version: 0.907 + version: 0.908 */ #include @@ -825,18 +825,21 @@ void RCL_castRayMultiHit(RCL_Ray ray, RCL_ArrayFunction arrayFunc, #if RCL_RECTILINEAR /* Here we compute the fish eye corrected distance (perpendicular to - the projection plane) as the Euclidean distance divided by the length - of the ray direction vector. This can be computed without actually - computing Euclidean distances as a hypothenuse A (distance) divided - by hypothenuse B (length) is equal to leg A (distance along one axis) - divided by leg B (length along the same axis). */ + the projection plane) as the Euclidean distance (of hit from camera + position) divided by the length of the ray direction vector. This can + be computed without actually computing Euclidean distances as a + hypothenuse A (distance) divided by hypothenuse B (length) is equal to + leg A (distance along principal axis) divided by leg B (length along + the same principal axis). */ - h.distance = - (((h.position.x - ray.start.x) / 4) * - RCL_UNITS_PER_SQUARE * rayDirXRecip) - / (RECIP_SCALE / 4); +#define CORRECT(dir1,dir2)\ + RCL_Unit tmp = diff / 4; /* 4 to prevent overflow */ \ + h.distance = ((tmp / 8) != 0) ? /* prevent a bug with small dists */ \ + ((tmp * RCL_UNITS_PER_SQUARE * rayDir ## dir1 ## Recip) / (RECIP_SCALE / 4)):\ + RCL_abs(h.position.dir2 - ray.start.dir2); + + CORRECT(X,y) - // ^ / 4 is here to prevent overflow #endif } else @@ -856,10 +859,10 @@ void RCL_castRayMultiHit(RCL_Ray ray, RCL_ArrayFunction arrayFunc, ray.start.x + (ray.direction.x * diff * rayDirYRecip) / RECIP_SCALE; #if RCL_RECTILINEAR - h.distance = - (((h.position.y - ray.start.y) / 4) * - RCL_UNITS_PER_SQUARE * rayDirYRecip) - / (RECIP_SCALE / 4); + + CORRECT(Y,x) // same as above but for different axis + +#undef CORRECT // ^ / 4 is here to prevent overflow #endif