mirror of
https://git.coom.tech/drummyfish/raycastlib.git
synced 2024-11-20 20:19:57 +01:00
Fix a rounding bug
This commit is contained in:
parent
94bcb8e294
commit
c61589d82b
1 changed files with 12 additions and 10 deletions
22
raycastlib.h
22
raycastlib.h
|
@ -219,6 +219,12 @@ inline Unit absVal(Unit 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
|
||||
#define trigHelper(x) (((Unit) UNITS_PER_SQUARE) *\
|
||||
(UNITS_PER_SQUARE / 2 * UNITS_PER_SQUARE / 2 - 4 * (x) * (x)) /\
|
||||
|
@ -228,6 +234,8 @@ Unit cosInt(Unit input)
|
|||
{
|
||||
profileCall(cosInt);
|
||||
|
||||
// TODO: could be optimized with LUT
|
||||
|
||||
input = input % UNITS_PER_SQUARE;
|
||||
|
||||
if (input < 0)
|
||||
|
@ -406,14 +414,8 @@ void castRayMultiHit(Ray ray, ArrayFunction arrayFunc, HitResult *hitResults,
|
|||
|
||||
Vector2D currentSquare;
|
||||
|
||||
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--;
|
||||
currentSquare.x = divRoundDown(ray.start.x, UNITS_PER_SQUARE);
|
||||
currentSquare.y = divRoundDown(ray.start.y,UNITS_PER_SQUARE);
|
||||
|
||||
*hitResultsLen = 0;
|
||||
|
||||
|
@ -694,8 +696,8 @@ void render(Camera cam, ArrayFunction arrayFunc, PixelFunction pixelFunc,
|
|||
_middleRow = cam.resolution.y / 2;
|
||||
|
||||
_startHeight = arrayFunc(
|
||||
cam.position.x / UNITS_PER_SQUARE,
|
||||
cam.position.y / UNITS_PER_SQUARE) -1 * cam.height;
|
||||
divRoundDown(cam.position.x,UNITS_PER_SQUARE),
|
||||
divRoundDown(cam.position.y,UNITS_PER_SQUARE)) -1 * cam.height;
|
||||
|
||||
// TODO
|
||||
_floorDepthStep = (12 * UNITS_PER_SQUARE) / cam.resolution.y;
|
||||
|
|
Loading…
Reference in a new issue