1
0
Fork 0
mirror of https://git.coom.tech/drummyfish/raycastlib.git synced 2024-11-21 20:29:59 +01:00

Improve distance computations

This commit is contained in:
Miloslav Číž 2019-08-16 20:41:03 +02:00
parent 79f360474b
commit 57c2bb34e4

View file

@ -26,7 +26,7 @@
author: Miloslav "drummyfish" Ciz author: Miloslav "drummyfish" Ciz
license: CC0 1.0 license: CC0 1.0
version: 0.8 version: 0.81
*/ */
#include <stdint.h> #include <stdint.h>
@ -78,6 +78,11 @@
2: octagonal approximation (LQ) */ 2: octagonal approximation (LQ) */
#endif #endif
#ifndef RCL_EQUILINEAR
#define RCL_EQUILINEAR 1 /**< Whether to use equilinear perspective (normally
used), or curvilinear perspective (fish eye). */
#endif
#ifndef RCL_TEXTURE_VERTICAL_STRETCH #ifndef RCL_TEXTURE_VERTICAL_STRETCH
#define RCL_TEXTURE_VERTICAL_STRETCH 1 /**< Whether textures should be #define RCL_TEXTURE_VERTICAL_STRETCH 1 /**< Whether textures should be
stretched to wall height (possibly stretched to wall height (possibly
@ -755,20 +760,16 @@ void RCL_castRayMultiHit(RCL_Ray ray, RCL_ArrayFunction arrayFunc,
// DDA variables // DDA variables
RCL_Vector2D nextSideDist; // dist. from start to the next side in given axis RCL_Vector2D nextSideDist; // dist. from start to the next side in given axis
RCL_Vector2D delta; RCL_Vector2D delta;
RCL_Unit currentDist = 0;
RCL_Vector2D step; // -1 or 1 for each axis RCL_Vector2D step; // -1 or 1 for each axis
int8_t stepHorizontal = 0; // whether the last step was hor. or vert. int8_t stepHorizontal = 0; // whether the last step was hor. or vert.
nextSideDist.x = 0; nextSideDist.x = 0;
nextSideDist.y = 0; nextSideDist.y = 0;
RCL_Unit dirVecLength = RCL_len(ray.direction); RCL_Unit dirVecLengthNorm = RCL_len(ray.direction) * RCL_UNITS_PER_SQUARE;
delta.x = RCL_absVal((RCL_UNITS_PER_SQUARE * dirVecLength) / delta.x = RCL_absVal(dirVecLengthNorm / RCL_nonZero(ray.direction.x));
RCL_nonZero(ray.direction.x)); delta.y = RCL_absVal(dirVecLengthNorm / RCL_nonZero(ray.direction.y));
delta.y = RCL_absVal((RCL_UNITS_PER_SQUARE * dirVecLength) /
RCL_nonZero(ray.direction.y));
// init DDA // init DDA
@ -816,7 +817,6 @@ void RCL_castRayMultiHit(RCL_Ray ray, RCL_ArrayFunction arrayFunc,
h.doorRoll = 0; h.doorRoll = 0;
h.position = currentPos; h.position = currentPos;
h.square = currentSquare; h.square = currentSquare;
h.distance = currentDist;
if (stepHorizontal) if (stepHorizontal)
{ {
@ -833,9 +833,11 @@ void RCL_castRayMultiHit(RCL_Ray ray, RCL_ArrayFunction arrayFunc,
h.position.y = ray.start.y + ((ray.direction.y * diff) / h.position.y = ray.start.y + ((ray.direction.y * diff) /
RCL_nonZero(ray.direction.x)); RCL_nonZero(ray.direction.x));
#if RCL_EQUILINEAR
h.distance = h.distance =
((h.position.x - ray.start.x) * RCL_UNITS_PER_SQUARE) / ((h.position.x - ray.start.x) * RCL_UNITS_PER_SQUARE) /
RCL_nonZero(ray.direction.x); RCL_nonZero(ray.direction.x);
#endif
} }
else else
{ {
@ -852,11 +854,17 @@ void RCL_castRayMultiHit(RCL_Ray ray, RCL_ArrayFunction arrayFunc,
h.position.x = ray.start.x + ((ray.direction.x * diff) / h.position.x = ray.start.x + ((ray.direction.x * diff) /
RCL_nonZero(ray.direction.y)); RCL_nonZero(ray.direction.y));
#if RCL_EQUILINEAR
h.distance = h.distance =
((h.position.y - ray.start.y) * RCL_UNITS_PER_SQUARE) / ((h.position.y - ray.start.y) * RCL_UNITS_PER_SQUARE) /
RCL_nonZero(ray.direction.y); RCL_nonZero(ray.direction.y);
#endif
} }
#if !RCL_EQUILINEAR
h.distance = RCL_dist(h.position,ray.start);
#endif
if (typeFunc != 0) if (typeFunc != 0)
h.type = typeFunc(currentSquare.x,currentSquare.y); h.type = typeFunc(currentSquare.x,currentSquare.y);
@ -904,14 +912,12 @@ void RCL_castRayMultiHit(RCL_Ray ray, RCL_ArrayFunction arrayFunc,
if (nextSideDist.x < nextSideDist.y) if (nextSideDist.x < nextSideDist.y)
{ {
currentDist = nextSideDist.x;
nextSideDist.x += delta.x; nextSideDist.x += delta.x;
currentSquare.x += step.x; currentSquare.x += step.x;
stepHorizontal = 1; stepHorizontal = 1;
} }
else else
{ {
currentDist = nextSideDist.y;
nextSideDist.y += delta.y; nextSideDist.y += delta.y;
currentSquare.y += step.y; currentSquare.y += step.y;
stepHorizontal = 0; stepHorizontal = 0;