Skip to content

Commit 3f0e975

Browse files
committed
Removed recent Hypot function + Adjusted "UseAccurateSphereToRectCollision" to use a fast approx Hypotenuse calculation
1 parent d587f67 commit 3f0e975

File tree

7 files changed

+21
-42
lines changed

7 files changed

+21
-42
lines changed

Core/Libraries/Source/WWVegas/WWMath/vector2.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,7 @@ WWINLINE Vector2 Normalize(const Vector2 & vec)
356356
*========================================================================*/
357357
WWINLINE float Vector2::Length() const
358358
{
359-
return (float)WWMath::Hypot(X, Y);
360-
//return (float)WWMath::Sqrt(Length2());
359+
return (float)WWMath::Sqrt(Length2());
361360
}
362361

363362
/**************************************************************************

Core/Libraries/Source/WWVegas/WWMath/wwmath.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,6 @@ static WWINLINE float Acos(float val);
132132
static WWINLINE float Fast_Asin(float val);
133133
static WWINLINE float Asin(float val);
134134

135-
static WWINLINE float Hypot(float x, float y);
136-
static WWINLINE float Hypot(float x, float y, float z);
137-
static WWINLINE float Hypot(float x, float y, float z, float w);
138-
139135

140136
static WWINLINE float Atan(float x) { return static_cast<float>(atan(x)); }
141137
static WWINLINE float Atan2(float y,float x) { return static_cast<float>(atan2(y,x)); }
@@ -544,15 +540,6 @@ WWINLINE float WWMath::Asin(float val)
544540
return (float)asin(val);
545541
}
546542

547-
// ----------------------------------------------------------------------------
548-
// Hypothenus
549-
// ----------------------------------------------------------------------------
550-
551-
WWINLINE float WWMath::Hypot(float x, float y)
552-
{
553-
return (float)hypot(x, y);
554-
}
555-
556543
// ----------------------------------------------------------------------------
557544
// Sqrt
558545
// ----------------------------------------------------------------------------

Core/Tools/WW3D/pluglib/vector2.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,7 @@ inline Vector2 Normalize(const Vector2 & vec)
344344
*========================================================================*/
345345
inline float Vector2::Length() const
346346
{
347-
return (float)WWMath::Hypot(X, Y);
348-
//return (float)WWMath::Sqrt(Length2());
347+
return (float)WWMath::Sqrt(Length2());
349348
}
350349

351350
/**************************************************************************

GeneralsMD/Code/GameEngine/Source/Common/System/Trig.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,6 @@ Real ASin(Real x)
7171
return asinf(x);
7272
}
7373

74-
Real Hypot(Real x, Real y)
75-
{
76-
return hypot(x, y);
77-
}
78-
7974
#ifdef REGENERATE_TRIG_TABLES
8075
void initTrig( void )
8176
{

GeneralsMD/Code/GameEngine/Source/GameLogic/Object/PartitionManager.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ static void testRotatedPointsAgainstRect(
569569
}*/
570570

571571
//-----------------------------------------------------------------------------
572-
static Real fast_getBoundaryLength(Real x1, Real x2, Real y1, Real y2)
572+
static Real fast_hypot(Real x1, Real x2, Real y1, Real y2)
573573
{
574574
// Fast approximation of Boundary length, generally if one line has the length of only 10% or less of the other line, we take the longest line as the boundary.
575575
// Has an error rate of approx 0.5%.
@@ -578,18 +578,20 @@ static Real fast_getBoundaryLength(Real x1, Real x2, Real y1, Real y2)
578578
Real dy = fabs(y1 - y2);
579579

580580
// Longest line and shortest between x and y
581-
Real dmax = max(dx, dy);
582-
Real dmin = min(dx, dy);
583-
Real maxTolerance = 0.1f * dmax;
584-
Real distTolerance = max(20.0f, 0.5f * maxTolerance);
581+
Real a = max(dx, dy);
582+
Real b = min(dx, dy);
583+
//Real maxTolerance = 0.1f * dmax;
584+
//Real distTolerance = max(20.0f, 0.5f * maxTolerance);
585585

586586
// Two conditions:
587587
// - the difference must be less than a maximum of 20 units, or 5% of the maximum length (to be not more than 1 whole unit)
588588
// - the min length must be 10% or less than the max length
589-
if(dmin < distTolerance && dmin <= maxTolerance)
590-
return dmax;
591-
else
592-
return sqrtf(dx*dx + dy*dy);
589+
//if(dmin < distTolerance && dmin <= maxTolerance)
590+
// return dmax;
591+
//else
592+
593+
// max error ≈ 1.04 %
594+
return a * (1 + 0.428 * pow((b/a), 2));
593595
}
594596

595597
//-----------------------------------------------------------------------------
@@ -687,17 +689,17 @@ static void testSphereAgainstRect(
687689
DEBUG_ASSERTCRASH(minIdx <= 3, ("Hmm, this should not be possible."));
688690

689691
// Get the Triangle length of all 3 points
690-
//Real boundary_h = fast_getBoundaryLength(x1, x2, y1, y2);
691-
//Real boundary_1 = fast_getBoundaryLength(x1, a->position.x, y1, a->position.y);
692-
//Real boundary_2 = fast_getBoundaryLength(x2, a->position.x, y2, a->position.y);
692+
Real boundary_h = fast_hypot(x1, x2, y1, y2);
693+
Real boundary_1 = fast_hypot(x1, a->position.x, y1, a->position.y);
694+
Real boundary_2 = fast_hypot(x2, a->position.x, y2, a->position.y);
693695

694696
//Real boundary_h = sqrtf(sqr(x1 - x2) + sqr(y1 - y2));
695697
//Real boundary_1 = sqrtf(sqr(x1 - a->position.x) + sqr(y1 - a->position.y));
696698
//Real boundary_2 = sqrtf(sqr(x2 - a->position.x) + sqr(y2 - a->position.y));
697699

698-
Real boundary_h = Hypot(fabs(x1-x2), fabs(y1-y2));
699-
Real boundary_1 = Hypot(fabs(x1 - a->position.x), fabs(y1 - a->position.y));
700-
Real boundary_2 = Hypot(fabs(x2 - a->position.x), fabs(y2 - a->position.y));
700+
//Real boundary_h = Hypot(fabs(x1-x2), fabs(y1-y2));
701+
//Real boundary_1 = Hypot(fabs(x1 - a->position.x), fabs(y1 - a->position.y));
702+
//Real boundary_2 = Hypot(fabs(x2 - a->position.x), fabs(y2 - a->position.y));
701703

702704
// Heron's formula
703705
Real semiPeri = (boundary_h + boundary_1 + boundary_2) * 0.5;

GeneralsMD/Code/Libraries/Include/Lib/BaseType.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,7 @@ struct Coord2D
183183
{
184184
Real x, y;
185185

186-
//Real length( void ) const { return (Real)sqrt( x*x + y*y ); }
187-
Real length( void ) const { return (Real)hypot( x, y ); }
186+
Real length( void ) const { return (Real)sqrt( x*x + y*y ); }
188187

189188
void normalize( void )
190189
{
@@ -233,8 +232,7 @@ struct ICoord2D
233232
{
234233
Int x, y;
235234

236-
//Int length( void ) const { return (Int)sqrt( (double)(x*x + y*y) ); }
237-
Int length( void ) const { return (Int)hypot( (double)x, (double)y ); }
235+
Int length( void ) const { return (Int)sqrt( (double)(x*x + y*y) ); }
238236
};
239237

240238
struct Region2D

GeneralsMD/Code/Libraries/Include/Lib/trig.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,3 @@ Real Cos(Real);
2828
Real Tan(Real);
2929
Real ACos(Real);
3030
Real ASin(Real x);
31-
Real Hypot(Real x, Real y);

0 commit comments

Comments
 (0)