Use R_PointToDist2 instead

Apparently overflows less often

Actually, lets just fix FixedHypot instead.

Now FixedHypot uses the code from R_PointToDist2, and R_PointToDist2 just calls FixedHypot.

Ultimately, this branch was intended to get rid of a redundant way to retrieve distance and replace it with the one that was actually good at its job. So consolidating FixedHypot and R_PointToDist2 together is just an extension of that.
This commit is contained in:
Sally Coolatta 2020-11-07 23:56:46 -05:00 committed by James R
parent c5474436af
commit e19196a86e
2 changed files with 29 additions and 35 deletions

View File

@ -18,8 +18,10 @@
#define HAVE_SQRTF #define HAVE_SQRTF
#endif #endif
#endif #endif
#include "doomdef.h" #include "doomdef.h"
#include "m_fixed.h" #include "m_fixed.h"
#include "tables.h" // ANGLETOFINESHIFT
#ifdef __USE_C_FIXEDMUL__ #ifdef __USE_C_FIXEDMUL__
@ -105,20 +107,34 @@ fixed_t FixedSqrt(fixed_t x)
fixed_t FixedHypot(fixed_t x, fixed_t y) fixed_t FixedHypot(fixed_t x, fixed_t y)
{ {
fixed_t ax, yx, yx2, yx1; // Moved the code from R_PointToDist2 to here,
if (abs(y) > abs(x)) // |y|>|x| // since R_PointToDist2 did the same thing,
// except less prone to overflowing.
angle_t angle;
fixed_t dist;
x = abs(x);
y = abs(y);
if (y > x)
{ {
ax = abs(y); // |y| => ax fixed_t temp;
yx = FixedDiv(x, y); // (x/y)
temp = x;
x = y;
y = temp;
} }
else // |x|>|y|
{ if (!y)
ax = abs(x); // |x| => ax return x;
yx = FixedDiv(y, x); // (x/y)
} angle = (tantoangle[FixedDiv(y, x)>>DBITS] + ANGLE_90) >> ANGLETOFINESHIFT;
yx2 = FixedMul(yx, yx); // (x/y)^2
yx1 = FixedSqrt(1 * FRACUNIT + yx2); // (1 + (x/y)^2)^1/2 // use as cosine
return FixedMul(ax, yx1); // |x|*((1 + (x/y)^2)^1/2) dist = FixedDiv(x, FINESINE(angle));
return dist;
} }
vector2_t *FV2_Load(vector2_t *vec, fixed_t x, fixed_t y) vector2_t *FV2_Load(vector2_t *vec, fixed_t x, fixed_t y)

View File

@ -357,29 +357,7 @@ angle_t R_PointToAngle2(fixed_t pviewx, fixed_t pviewy, fixed_t x, fixed_t y)
fixed_t R_PointToDist2(fixed_t px2, fixed_t py2, fixed_t px1, fixed_t py1) fixed_t R_PointToDist2(fixed_t px2, fixed_t py2, fixed_t px1, fixed_t py1)
{ {
angle_t angle; return FixedHypot(px1 - px2, py1 - py2);
fixed_t dx, dy, dist;
dx = abs(px1 - px2);
dy = abs(py1 - py2);
if (dy > dx)
{
fixed_t temp;
temp = dx;
dx = dy;
dy = temp;
}
if (!dy)
return dx;
angle = (tantoangle[FixedDiv(dy, dx)>>DBITS] + ANGLE_90) >> ANGLETOFINESHIFT;
// use as cosine
dist = FixedDiv(dx, FINESINE(angle));
return dist;
} }
// Little extra utility. Works in the same way as R_PointToAngle2 // Little extra utility. Works in the same way as R_PointToAngle2