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

Fix a rounding bug

This commit is contained in:
Miloslav Číž 2018-09-03 09:52:50 +02:00
parent 94bcb8e294
commit c61589d82b

View file

@ -219,6 +219,12 @@ inline Unit absVal(Unit value)
return value < 0 ? -1 * value : value; return value < 0 ? -1 * value : value;
} }
/// Performs division, rounding down, NOT towards zero.
inline Unit divRoundDown(Unit value, Unit divisor)
{
return value / divisor - (value < 0 ? 1 : 0);
}
// Bhaskara's cosine approximation formula // Bhaskara's cosine approximation formula
#define trigHelper(x) (((Unit) UNITS_PER_SQUARE) *\ #define trigHelper(x) (((Unit) UNITS_PER_SQUARE) *\
(UNITS_PER_SQUARE / 2 * UNITS_PER_SQUARE / 2 - 4 * (x) * (x)) /\ (UNITS_PER_SQUARE / 2 * UNITS_PER_SQUARE / 2 - 4 * (x) * (x)) /\
@ -228,6 +234,8 @@ Unit cosInt(Unit input)
{ {
profileCall(cosInt); profileCall(cosInt);
// TODO: could be optimized with LUT
input = input % UNITS_PER_SQUARE; input = input % UNITS_PER_SQUARE;
if (input < 0) if (input < 0)
@ -406,14 +414,8 @@ void castRayMultiHit(Ray ray, ArrayFunction arrayFunc, HitResult *hitResults,
Vector2D currentSquare; Vector2D currentSquare;
currentSquare.x = ray.start.x / UNITS_PER_SQUARE; currentSquare.x = divRoundDown(ray.start.x, UNITS_PER_SQUARE);
currentSquare.y = ray.start.y / UNITS_PER_SQUARE; currentSquare.y = divRoundDown(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; *hitResultsLen = 0;
@ -694,8 +696,8 @@ void render(Camera cam, ArrayFunction arrayFunc, PixelFunction pixelFunc,
_middleRow = cam.resolution.y / 2; _middleRow = cam.resolution.y / 2;
_startHeight = arrayFunc( _startHeight = arrayFunc(
cam.position.x / UNITS_PER_SQUARE, divRoundDown(cam.position.x,UNITS_PER_SQUARE),
cam.position.y / UNITS_PER_SQUARE) -1 * cam.height; divRoundDown(cam.position.y,UNITS_PER_SQUARE)) -1 * cam.height;
// TODO // TODO
_floorDepthStep = (12 * UNITS_PER_SQUARE) / cam.resolution.y; _floorDepthStep = (12 * UNITS_PER_SQUARE) / cam.resolution.y;