From 1dc3f2f7637e05bef3c74ec4130c1acaae708970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Mon, 3 Sep 2018 16:38:49 +0200 Subject: [PATCH] Add vertical FOV --- raycastlib.h | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/raycastlib.h b/raycastlib.h index 0c4bfa8..1491795 100644 --- a/raycastlib.h +++ b/raycastlib.h @@ -17,8 +17,6 @@ clockwise, a full angle has UNITS_PER_SQUARE Units. */ -#include - #include #ifndef RAYCAST_TINY @@ -39,6 +37,8 @@ typedef uint16_t uint_maybe32_t; #endif +#define VERTICAL_FOV (UNITS_PER_SQUARE / 2) + #define logVector2D(v)\ printf("[%d,%d]\n",v.x,v.y); @@ -81,6 +81,8 @@ typedef struct uint8_t direction; ///< Direction of hit. } HitResult; +// TODO: things like FOV could be constants to make them precomp. and faster? + typedef struct { Vector2D position; @@ -151,7 +153,7 @@ Unit len(Vector2D v); Unit degreesToUnitsAngle(int16_t degrees); ///< Computes the change in size of an object due to perspective. -Unit perspectiveScale(Unit originalSize, Unit distance, Unit fov); +Unit perspectiveScale(Unit originalSize, Unit distance); /** Casts rays for given camera view and for each hit calls a user provided @@ -582,28 +584,28 @@ void _columnFunction(HitResult *hits, uint16_t hitCount, uint16_t x, Ray ray) Unit worldZ2Ceil = (UNITS_PER_SQUARE * 5 - wallHeight) - _camera.height; int_maybe32_t z1Screen = _middleRow - perspectiveScale( - (worldZPrev * _camera.resolution.y) / UNITS_PER_SQUARE,dist,1); + (worldZPrev * _camera.resolution.y) / UNITS_PER_SQUARE,dist); int_maybe32_t z1ScreenNoClamp = z1Screen; z1Screen = clamp(z1Screen,0,_camResYLimit); int_maybe32_t z1ScreenCeil = _middleRow - perspectiveScale( - (worldZPrevCeil * _camera.resolution.y) / UNITS_PER_SQUARE,dist,1); + (worldZPrevCeil * _camera.resolution.y) / UNITS_PER_SQUARE,dist); int_maybe32_t z1ScreenCeilNoClamp = z1ScreenCeil; z1ScreenCeil = clamp(z1ScreenCeil,0,_camResYLimit); int_maybe32_t z2Screen = _middleRow - perspectiveScale( - (worldZ2 * _camera.resolution.y) / UNITS_PER_SQUARE,dist,1); + (worldZ2 * _camera.resolution.y) / UNITS_PER_SQUARE,dist); int_maybe32_t wallScreenHeightNoClamp = z2Screen - z1ScreenNoClamp; z2Screen = clamp(z2Screen,0,_camResYLimit); int_maybe32_t z2ScreenCeil = _middleRow - perspectiveScale( - (worldZ2Ceil * _camera.resolution.y) / UNITS_PER_SQUARE,dist,1); + (worldZ2Ceil * _camera.resolution.y) / UNITS_PER_SQUARE,dist); int_maybe32_t wallScreenHeightCeilNoClamp = z2ScreenCeil - z1ScreenCeilNoClamp; @@ -757,14 +759,11 @@ Unit degreesToUnitsAngle(int16_t degrees) return (degrees * UNITS_PER_SQUARE) / 360; } -Unit perspectiveScale(Unit originalSize, Unit distance, Unit fov) +Unit perspectiveScale(Unit originalSize, Unit distance) { -return (originalSize * UNITS_PER_SQUARE) / distance; - - distance *= fov; - distance = distance == 0 ? 1 : distance; // prevent division by zero - - return originalSize / distance; + return (originalSize * UNITS_PER_SQUARE) / + ((VERTICAL_FOV * 2 * distance) / UNITS_PER_SQUARE); + // ^ approximation of tan function } #endif