Skip to content

Commit bc865fb

Browse files
committed
refactor: Route all physics math through WWMath
Replaced direct systemic C-library calls (sqrt, atan2, sin, cos) with deterministic WWMath wrappers across GameLogic and Common components. Replaced pow(x, 2) patterns with deterministic hardware muls (x * x). Files updated: - Common/System (Trig.cpp, Geometry.cpp, BuildAssistant.cpp) - GameLogic/AI (AIGroup.cpp, AISkirmishPlayer.cpp, AIStates.cpp) - GameLogic/Object (Locomotor.cpp, PartitionManager.cpp, PhysicsUpdate.cpp, Weapon.cpp)
1 parent cd84aa7 commit bc865fb

10 files changed

Lines changed: 251 additions & 236 deletions

File tree

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
// USER INCLUDES //////////////////////////////////////////////////////////////////////////////////
3232
#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
3333

34+
#include "WWMath/wwmath.h"
35+
3436
#include "Common/BuildAssistant.h"
3537
#include "Common/GlobalData.h"
3638
#include "Common/Player.h"
@@ -806,8 +808,8 @@ LegalBuildCode BuildAssistant::isLocationClearOfObjects( const Coord3D *worldPos
806808
if (myFactoryExitWidth>0) {
807809
myExitPos = *worldPos;
808810
checkMyExit = true;
809-
Real c = (Real)cos(angle);
810-
Real s = (Real)sin(angle);
811+
Real c = WWMath::Cos(angle);
812+
Real s = WWMath::Sin(angle);
811813
Real offset = build->getTemplateGeometryInfo().getMajorRadius() + myFactoryExitWidth/2.0f;
812814
myExitPos.x += c*offset;
813815
myExitPos.y += s*offset;
@@ -854,8 +856,8 @@ LegalBuildCode BuildAssistant::isLocationClearOfObjects( const Coord3D *worldPos
854856
if (themFactoryExitWidth>0) {
855857
hisExitPos = *them->getPosition();
856858
checkHisExit = true;
857-
Real c = (Real)cos(them->getOrientation());
858-
Real s = (Real)sin(them->getOrientation());
859+
Real c = WWMath::Cos(them->getOrientation());
860+
Real s = WWMath::Sin(them->getOrientation());
859861
Real offset = them->getGeometryInfo().getMajorRadius() + themFactoryExitWidth/2.0f;
860862
hisExitPos.x += c*offset;
861863
hisExitPos.y += s*offset;
@@ -1435,7 +1437,7 @@ Bool BuildAssistant::moveObjectsForConstruction( const ThingTemplate *whatToBuil
14351437
Bool anyUnmovables = false;
14361438
MemoryPoolObjectHolder hold( iter );
14371439

1438-
Real radius = sqrt(pow(gi.getMajorRadius(), 2) + pow(gi.getMinorRadius(), 2));
1440+
Real radius = WWMath::Sqrt(gi.getMajorRadius() * gi.getMajorRadius() + gi.getMinorRadius() * gi.getMinorRadius());
14391441
radius *= 1.4f; // Fudge the distance,
14401442

14411443
for( Object *them = iter->first(); them; them = iter->next() )

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#define DEFINE_GEOMETRY_NAMES
3232

3333
// USER INCLUDES //////////////////////////////////////////////////////////////////////////////////
34+
#include "WWMath/wwmath.h"
3435
#include "Common/Geometry.h"
3536
#include "Common/INI.h"
3637
#include "Common/RandomValue.h"
@@ -173,17 +174,17 @@ void GeometryInfo::calcPitches(const Coord3D& thisPos, const GeometryInfo& that,
173174
Coord3D thisCenter;
174175
getCenterPosition(thisPos, thisCenter);
175176

176-
Real dxy = sqrt(sqr(thatPos.x - thisCenter.x) + sqr(thatPos.y - thisCenter.y));
177+
Real dxy = WWMath::Sqrt(sqr(thatPos.x - thisCenter.x) + sqr(thatPos.y - thisCenter.y));
177178

178179
Real dz;
179180

180181
/** @todo srj -- this could be better, by calcing it for all the corners, not just top-center
181182
and bottom-center... oh well */
182183
dz = (thatPos.z + that.getMaxHeightAbovePosition()) - thisCenter.z;
183-
maxPitch = atan2(dz, dxy);
184+
maxPitch = WWMath::Atan2(dz, dxy);
184185

185186
dz = (thatPos.z - that.getMaxHeightBelowPosition()) - thisCenter.z;
186-
minPitch = atan2(dz, dxy);
187+
minPitch = WWMath::Atan2(dz, dxy);
187188
}
188189

189190
//=============================================================================
@@ -279,8 +280,8 @@ void GeometryInfo::get2DBounds(const Coord3D& geomCenter, Real angle, Region2D&
279280

280281
case GEOMETRY_BOX:
281282
{
282-
Real c = (Real)cos(angle);
283-
Real s = (Real)sin(angle);
283+
Real c = WWMath::Cos(angle);
284+
Real s = WWMath::Sin(angle);
284285
Real exc = m_majorRadius*c;
285286
Real eyc = m_minorRadius*c;
286287
Real exs = m_majorRadius*s;
@@ -329,7 +330,7 @@ void GeometryInfo::clipPointToFootprint(const Coord3D& geomCenter, Coord3D& ptTo
329330
{
330331
Real dx = ptToClip.x - geomCenter.x;
331332
Real dy = ptToClip.y - geomCenter.y;
332-
Real radius = sqrt(sqr(dx) + sqr(dy));
333+
Real radius = WWMath::Sqrt(sqr(dx) + sqr(dy));
333334
if (radius > m_majorRadius)
334335
{
335336
Real ratio = m_majorRadius / radius;
@@ -361,7 +362,7 @@ Bool GeometryInfo::isPointInFootprint(const Coord3D& geomCenter, const Coord3D&
361362
{
362363
Real dx = pt.x - geomCenter.x;
363364
Real dy = pt.y - geomCenter.y;
364-
Real radius = sqrt(sqr(dx) + sqr(dy));
365+
Real radius = WWMath::Sqrt(sqr(dx) + sqr(dy));
365366
return (radius <= m_majorRadius);
366367
break;
367368
}
@@ -398,8 +399,8 @@ void GeometryInfo::makeRandomOffsetWithinFootprint(Coord3D& pt) const
398399
#else
399400
Real radius = GameLogicRandomValueReal(0.0f, m_boundingCircleRadius);
400401
Real angle = GameLogicRandomValueReal(-PI, PI);
401-
pt.x = radius * Cos(angle);
402-
pt.y = radius * Sin(angle);
402+
pt.x = radius * WWMath::Cos(angle);
403+
pt.y = radius * WWMath::Sin(angle);
403404
pt.z = 0.0f;
404405
#endif
405406
break;
@@ -506,8 +507,8 @@ void GeometryInfo::calcBoundingStuff()
506507

507508
case GEOMETRY_BOX:
508509
{
509-
m_boundingCircleRadius = sqrt(sqr(m_majorRadius) + sqr(m_minorRadius));
510-
m_boundingSphereRadius = sqrt(sqr(m_majorRadius) + sqr(m_minorRadius) + sqr(m_height*0.5));
510+
m_boundingCircleRadius = WWMath::Sqrt(sqr(m_majorRadius) + sqr(m_minorRadius));
511+
m_boundingSphereRadius = WWMath::Sqrt(sqr(m_majorRadius) + sqr(m_minorRadius) + sqr(m_height*0.5));
511512
break;
512513
}
513514
};

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#include "Lib/BaseType.h"
3636
#include "Lib/trig.h"
37+
#include "WWMath/wwmath.h"
3738

3839
#define TWOPI 6.28318530718f
3940
#define DEG2RAD 0.0174532925199f
@@ -48,27 +49,27 @@
4849

4950
Real Sin(Real x)
5051
{
51-
return sinf(x);
52+
return WWMath::Sin(x);
5253
}
5354

5455
Real Cos(Real x)
5556
{
56-
return cosf(x);
57+
return WWMath::Cos(x);
5758
}
5859

5960
Real Tan(Real x)
6061
{
61-
return tanf(x);
62+
return WWMath::Tan(x);
6263
}
6364

6465
Real ACos(Real x)
6566
{
66-
return acosf(x);
67+
return WWMath::Acos(x);
6768
}
6869

6970
Real ASin(Real x)
7071
{
71-
return asinf(x);
72+
return WWMath::Asin(x);
7273
}
7374

7475
#ifdef REGENERATE_TRIG_TABLES

GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
// Author: Michael S. Booth, January 2002
2828
#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
2929

30+
#include "WWMath/wwmath.h"
3031

3132
#include "Common/ActionManager.h"
3233
#include "Common/BuildAssistant.h"
@@ -1883,8 +1884,8 @@ void getHelicopterOffset( Coord3D& posOut, Int idx )
18831884
}
18841885

18851886
Coord3D tempCtr = posOut;
1886-
posOut.x = tempCtr.x + (sin(angle) * radius);
1887-
posOut.y = tempCtr.y + (cos(angle) * radius);
1887+
posOut.x = tempCtr.x + (WWMath::Sin(angle) * radius);
1888+
posOut.y = tempCtr.y + (WWMath::Cos(angle) * radius);
18881889

18891890
}
18901891

GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
3030

31+
#include "WWMath/wwmath.h"
3132

3233
#include "Common/GameMemory.h"
3334
#include "Common/GlobalData.h"
@@ -672,8 +673,8 @@ void AISkirmishPlayer::buildAIBaseDefenseStructure(const AsciiString &thingName,
672673
}
673674

674675
if (angle > PI/3) break;
675-
Real s = sin(angle);
676-
Real c = cos(angle);
676+
Real s = WWMath::Sin(angle);
677+
Real c = WWMath::Cos(angle);
677678

678679
// TheSuperHackers @info helmutbuhler 21/04/2025 This debug mutates the code to become CRC incompatible
679680
#if defined(RTS_DEBUG) || !RETAIL_COMPATIBLE_CRC
@@ -1038,8 +1039,8 @@ void AISkirmishPlayer::adjustBuildList(BuildListInfo *list)
10381039

10391040
angle += 3*PI/4;
10401041

1041-
Real s = sin(angle);
1042-
Real c = cos(angle);
1042+
Real s = WWMath::Sin(angle);
1043+
Real c = WWMath::Cos(angle);
10431044

10441045
cur = list;
10451046
while (cur) {

GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIStates.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
// Author: Michael S. Booth, January 2002
2828
#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
2929

30+
#include "WWMath/wwmath.h"
3031

3132
#include "Common/ActionManager.h"
3233
#include "Common/AudioHandleSpecialValues.h"
@@ -3907,16 +3908,16 @@ void AIFollowWaypointPathState::computeGoal(Bool useGroupOffsets)
39073908
if (m_priorWaypoint) {
39083909
dx = dest.x - m_priorWaypoint->getLocation()->x;
39093910
dy = dest.y - m_priorWaypoint->getLocation()->y;
3910-
angle = atan2(dy, dx);
3911+
angle = WWMath::Atan2(dy, dx);
39113912
Real deltaAngle = angle - m_angle;
3912-
Real s = sin(deltaAngle);
3913-
Real c = cos(deltaAngle);
3913+
Real s = WWMath::Sin(deltaAngle);
3914+
Real c = WWMath::Cos(deltaAngle);
39143915
Real x = m_groupOffset.x * c - m_groupOffset.y * s;
39153916
Real y = m_groupOffset.y * c + m_groupOffset.x * s;
39163917
m_groupOffset.x = x;
39173918
m_groupOffset.y = y;
39183919
} else {
3919-
angle = atan2(dy, dx);
3920+
angle = WWMath::Atan2(dy, dx);
39203921
}
39213922
m_angle = angle;
39223923
#endif

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#define DEFINE_LOCO_Z_NAMES
3636
#define DEFINE_LOCO_APPEARANCE_NAMES
3737

38+
#include "WWMath/wwmath.h"
39+
3840
#include "Common/INI.h"
3941
#include "GameLogic/GameLogic.h"
4042
#include "GameLogic/PartitionManager.h"
@@ -231,9 +233,9 @@ static void calcDirectionToApplyThrust(
231233

232234
Bool foundSolution = false;
233235
Real distToGoalSqr = vecToGoal.Length2();
234-
Real distToGoal = sqrt(distToGoalSqr);
236+
Real distToGoal = WWMath::Sqrt(distToGoalSqr);
235237
Real curVelMagSqr = curVel.Length2();
236-
Real curVelMag = sqrt(curVelMagSqr);
238+
Real curVelMag = WWMath::Sqrt(curVelMagSqr);
237239
Real maxAccelSqr = sqr(maxAccel);
238240

239241
Real denom = curVelMagSqr - maxAccelSqr;
@@ -995,7 +997,7 @@ void Locomotor::locoUpdate_moveTowardsPosition(Object* obj, const Coord3D& goalP
995997
Real dx = goalPos.x - obj->getPosition()->x;
996998
Real dy = goalPos.y - obj->getPosition()->y;
997999
Real dz = goalPos.z - obj->getPosition()->z;
998-
Real dist = sqrt(dx*dx+dy*dy);
1000+
Real dist = WWMath::Sqrt(dx*dx+dy*dy);
9991001
if (dist>onPathDistToGoal)
10001002
{
10011003
if (!obj->isKindOf(KINDOF_PROJECTILE) && dist>2*onPathDistToGoal)
@@ -1113,7 +1115,7 @@ void Locomotor::locoUpdate_moveTowardsPosition(Object* obj, const Coord3D& goalP
11131115
// Projectiles never stop braking once they start. jba.
11141116
obj->setStatus( MAKE_OBJECT_STATUS_MASK( OBJECT_STATUS_BRAKING ) );
11151117
// Projectiles cheat in 3 dimensions.
1116-
dist = sqrt(dx*dx+dy*dy+dz*dz);
1118+
dist = WWMath::Sqrt(dx*dx+dy*dy+dz*dz);
11171119
Real vel = physics->getVelocityMagnitude();
11181120
if (vel < MIN_VEL)
11191121
vel = MIN_VEL;
@@ -1288,7 +1290,7 @@ void Locomotor::moveTowardsPositionWheels(Object* obj, PhysicsBehavior *physics,
12881290
Real angle = obj->getOrientation();
12891291
// Real relAngle = ThePartitionManager->getRelativeAngle2D( obj, &goalPos );
12901292
// Real desiredAngle = angle + relAngle;
1291-
Real desiredAngle = atan2(goalPos.y - obj->getPosition()->y, goalPos.x - obj->getPosition()->x);
1293+
Real desiredAngle = WWMath::Atan2(goalPos.y - obj->getPosition()->y, goalPos.x - obj->getPosition()->x);
12921294
Real relAngle = stdAngleDiff(desiredAngle, angle);
12931295

12941296
Bool moveBackwards = false;
@@ -1563,7 +1565,7 @@ Bool Locomotor::fixInvalidPosition(Object* obj, PhysicsBehavior *physics)
15631565
//physics->clearAcceleration();
15641566

15651567
if (dot<0) {
1566-
dot = sqrt(-dot);
1568+
dot = WWMath::Sqrt(-dot);
15671569
correctionNormalized.x *= dot*physics->getMass();
15681570
correctionNormalized.y *= dot*physics->getMass();
15691571
physics->applyMotiveForce(&correctionNormalized);
@@ -1627,7 +1629,7 @@ void Locomotor::moveTowardsPositionLegs(Object* obj, PhysicsBehavior *physics, c
16271629
Real angle = obj->getOrientation();
16281630
// Real relAngle = ThePartitionManager->getRelativeAngle2D( obj, &goalPos );
16291631
// Real desiredAngle = angle + relAngle;
1630-
Real desiredAngle = atan2(goalPos.y - obj->getPosition()->y, goalPos.x - obj->getPosition()->x);
1632+
Real desiredAngle = WWMath::Atan2(goalPos.y - obj->getPosition()->y, goalPos.x - obj->getPosition()->x);
16311633

16321634
if (m_template->m_wanderWidthFactor != 0.0f) {
16331635
Real angleLimit = PI/8 * m_template->m_wanderWidthFactor;
@@ -1760,7 +1762,7 @@ void Locomotor::moveTowardsPositionClimb(Object* obj, PhysicsBehavior *physics,
17601762
Real angle = obj->getOrientation();
17611763
// Real relAngle = ThePartitionManager->getRelativeAngle2D( obj, &goalPos );
17621764
// Real desiredAngle = angle + relAngle;
1763-
Real desiredAngle = atan2(goalPos.y - obj->getPosition()->y, goalPos.x - obj->getPosition()->x);
1765+
Real desiredAngle = WWMath::Atan2(goalPos.y - obj->getPosition()->y, goalPos.x - obj->getPosition()->x);
17641766
Real relAngle = stdAngleDiff(desiredAngle, angle);
17651767

17661768
if (moveBackwards) {
@@ -1851,7 +1853,7 @@ void Locomotor::moveTowardsPositionWings(Object* obj, PhysicsBehavior *physics,
18511853
Real angleTowardPos =
18521854
(isNearlyZero(dx) && isNearlyZero(dy)) ?
18531855
obj->getOrientation() :
1854-
atan2(dy, dx);
1856+
WWMath::Atan2(dy, dx);
18551857

18561858
Real aimDir = (PI - PI/8);
18571859
angleTowardPos += aimDir;
@@ -2147,7 +2149,7 @@ PhysicsTurningType Locomotor::rotateObjAroundLocoPivot(Object* obj, const Coord3
21472149
Real dy = goalPos.y - turnPos.y;
21482150
// If we are very close to the goal, we twitch due to rounding error. So just return. jba.
21492151
if (fabs(dx)<0.1f && fabs(dy)<0.1f) return TURN_NONE;
2150-
Real desiredAngle = atan2(dy, dx);
2152+
Real desiredAngle = WWMath::Atan2(dy, dx);
21512153
Real amount = stdAngleDiff(desiredAngle, angle);
21522154
if (relAngle) *relAngle = amount;
21532155
if (amount>maxTurnRate) {
@@ -2169,7 +2171,7 @@ PhysicsTurningType Locomotor::rotateObjAroundLocoPivot(Object* obj, const Coord3
21692171
// so, the thing is, we want to rotate ourselves so that our *center* is rotated
21702172
// by the given amount, but the rotation must be around turnPos. so do a little
21712173
// back-calculation.
2172-
Real angleDesiredForTurnPos = atan2(desiredPos.y - turnPos.y, desiredPos.x - turnPos.x);
2174+
Real angleDesiredForTurnPos = WWMath::Atan2(desiredPos.y - turnPos.y, desiredPos.x - turnPos.x);
21732175
amount = angleDesiredForTurnPos - angle;
21742176
#endif
21752177
/// @todo srj -- there's probably a more efficient & more direct way to do this. find it.
@@ -2185,7 +2187,7 @@ PhysicsTurningType Locomotor::rotateObjAroundLocoPivot(Object* obj, const Coord3
21852187
}
21862188
else
21872189
{
2188-
Real desiredAngle = atan2(goalPos.y - obj->getPosition()->y, goalPos.x - obj->getPosition()->x);
2190+
Real desiredAngle = WWMath::Atan2(goalPos.y - obj->getPosition()->y, goalPos.x - obj->getPosition()->x);
21892191
Real amount = stdAngleDiff(desiredAngle, angle);
21902192
if (relAngle) *relAngle = amount;
21912193
if (amount>maxTurnRate) {
@@ -2519,7 +2521,7 @@ void Locomotor::maintainCurrentPositionWings(Object* obj, PhysicsBehavior *physi
25192521
Real angleTowardMaintainPos =
25202522
(isNearlyZero(dx) && isNearlyZero(dy)) ?
25212523
obj->getOrientation() :
2522-
atan2(dy, dx);
2524+
WWMath::Atan2(dy, dx);
25232525

25242526
Real aimDir = (PI - PI/8);
25252527
if (turnRadius < 0)

0 commit comments

Comments
 (0)