1
0
Fork 0
mirror of https://git.coom.tech/drummyfish/raycastlib.git synced 2024-11-23 20:49:57 +01:00

Add distance approximation

This commit is contained in:
Miloslav Číž 2018-09-14 12:25:59 +02:00
parent 62fc4ab7c7
commit 60abe97adb

View file

@ -6,6 +6,9 @@
performance computers, such as Arduino. Only uses integer math and stdint performance computers, such as Arduino. Only uses integer math and stdint
standard library. standard library.
Check the defines below to fine-tune accuracy vs performance! Don't forget
to compile with optimizations.
author: Miloslav "drummyfish" Ciz author: Miloslav "drummyfish" Ciz
license: CC0 license: CC0
version: 0.1 version: 0.1
@ -21,7 +24,8 @@
#include <stdint.h> #include <stdint.h>
#ifndef RAYCAST_TINY #ifndef RAYCAST_TINY /** Turns on super efficient version of this library. Only
use if neccesarry, looks ugly. */
#define UNITS_PER_SQUARE 1024 ///< N. of Units in a side of a spatial square. #define UNITS_PER_SQUARE 1024 ///< N. of Units in a side of a spatial square.
typedef int32_t Unit; /**< Smallest spatial unit, there is UNITS_PER_SQUARE typedef int32_t Unit; /**< Smallest spatial unit, there is UNITS_PER_SQUARE
units in a square's length. This effectively units in a square's length. This effectively
@ -31,6 +35,7 @@
#define UNITS_PER_SQUARE 128 #define UNITS_PER_SQUARE 128
typedef int16_t Unit; typedef int16_t Unit;
#define UNIT_INFINITY 32767; #define UNIT_INFINITY 32767;
#define USE_DIST_APPROX 1
#endif #endif
#ifndef USE_COS_LUT #ifndef USE_COS_LUT
@ -41,6 +46,11 @@
*/ */
#endif #endif
#ifndef USE_DIST_APPROX
#define USE_DIST_APPROX 0 /** Whether to use distance approximation (1) or
real Euclidean distance (0, slower). */
#endif
#ifndef VERTICAL_FOV #ifndef VERTICAL_FOV
#define VERTICAL_FOV (UNITS_PER_SQUARE / 2) #define VERTICAL_FOV (UNITS_PER_SQUARE / 2)
#endif #endif
@ -478,13 +488,31 @@ Unit dist(Vector2D p1, Vector2D p2)
Unit dx = p2.x - p1.x; Unit dx = p2.x - p1.x;
Unit dy = p2.y - p1.y; Unit dy = p2.y - p1.y;
#ifdef RAYCAST_TINY #if USE_DIST_APPROX == 1
// octagonal approximation of Euclidean distance // Euclidean distance approximation
dx = absVal(dx); Unit a, b, result;
dy = absVal(dy);
return dy > dx ? dx / 2 + dy : dy / 2 + dx; dx = dx < 0 ? -1 * dx : dx;
dy = dy < 0 ? -1 * dy : dy;
if (dx < dy)
{
a = dy;
b = dx;
}
else
{
a = dx;
b = dy;
}
result = (a * 1007) + (b * 441);
if (a < (b << 4))
result -= (a * 40);
return ((result + 512 ) >> 10);
#else #else
dx = dx * dx; dx = dx * dx;
dy = dy * dy; dy = dy * dy;