Skip to content

Commit 15933f7

Browse files
authored
fix(draw): Fix wheel rotation math (#1507)
1 parent e022dc5 commit 15933f7

File tree

9 files changed

+38
-41
lines changed

9 files changed

+38
-41
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
*/
5353
#define WWMATH_EPSILON 0.0001f
5454
#define WWMATH_EPSILON2 WWMATH_EPSILON * WWMATH_EPSILON
55-
#define WWMATH_PI 3.141592654f
55+
#define WWMATH_PI 3.141592654f
56+
#define WWMATH_TWO_PI 6.283185308f
5657
#define WWMATH_FLOAT_MAX (FLT_MAX)
5758
#define WWMATH_FLOAT_MIN (FLT_MIN)
5859
#define WWMATH_SQRT2 1.414213562f
@@ -171,6 +172,8 @@ static WWINLINE float Byte_To_Unit_Float(unsigned char byte) { return ((float)
171172
static WWINLINE bool Is_Valid_Float(float x);
172173
static WWINLINE bool Is_Valid_Double(double x);
173174

175+
static WWINLINE float Normalize_Angle(float angle); // Normalizes the angle to the range -PI..PI
176+
174177
};
175178

176179
WWINLINE float WWMath::Sign(float val)
@@ -653,5 +656,9 @@ WWINLINE float WWMath::Inv_Sqrt(float val)
653656
}
654657
#endif
655658

659+
WWINLINE float WWMath::Normalize_Angle(float angle)
660+
{
661+
return angle - (WWMATH_TWO_PI * Floor((angle + WWMATH_PI) / WWMATH_TWO_PI));
662+
}
656663

657664
#endif

Generals/Code/GameEngine/Include/Common/GameCommon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ enum WhichTurretType CPP_11(: Int)
468468

469469
// ------------------------------------------------------------------------
470470
// this normalizes an angle to the range -PI...PI.
471+
// TheSuperHackers @todo DO NOT USE THIS FUNCTION! Use WWMath::Normalize_Angle instead. Delete this.
471472
extern Real normalizeAngle(Real angle);
472473

473474
// ------------------------------------------------------------------------

Generals/Code/GameEngine/Source/Common/System/GameCommon.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const char *TheRelationshipNames[] =
4848
};
4949

5050
//-------------------------------------------------------------------------------------------------
51+
// TheSuperHackers @todo DO NOT USE THIS FUNCTION! Use WWMath::Normalize_Angle instead. Delete this.
5152
Real normalizeAngle(Real angle)
5253
{
5354
DEBUG_ASSERTCRASH(!_isnan(angle), ("Angle is NAN in normalizeAngle!"));

Generals/Code/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DTankTruckDraw.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -540,21 +540,17 @@ void W3DTankTruckDraw::doDrawModule(const Matrix3D* transformMtx)
540540
const Coord3D *vel = physics->getVelocity();
541541
Real speed = physics->getVelocityMagnitude();
542542

543-
544543
const TWheelInfo *wheelInfo = getDrawable()->getWheelInfo(); // note, can return null!
545544
if (wheelInfo && (m_frontLeftTireBone || m_rearLeftTireBone))
546545
{
547-
static Real rotation = 0;
548546
const Real rotationFactor = getW3DTankTruckDrawModuleData()->m_rotationSpeedMultiplier;
547+
const Real powerslideRotationAddition = getW3DTankTruckDrawModuleData()->m_powerslideRotationAddition * m_isPowersliding;
548+
549549
m_frontWheelRotation += rotationFactor*speed;
550-
if (m_isPowersliding)
551-
{
552-
m_rearWheelRotation += rotationFactor*(speed+getW3DTankTruckDrawModuleData()->m_powerslideRotationAddition);
553-
}
554-
else
555-
{
556-
m_rearWheelRotation += rotationFactor*speed;
557-
}
550+
m_rearWheelRotation += rotationFactor*(speed+powerslideRotationAddition);
551+
m_frontWheelRotation = WWMath::Normalize_Angle(m_frontWheelRotation);
552+
m_rearWheelRotation = WWMath::Normalize_Angle(m_rearWheelRotation);
553+
558554
Matrix3D wheelXfrm(1);
559555
if (m_frontLeftTireBone)
560556
{

Generals/Code/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DTruckDraw.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,9 @@ void W3DTruckDraw::doDrawModule(const Matrix3D* transformMtx)
463463

464464
if (m_frontLeftTireBone || m_rearLeftTireBone)
465465
{
466-
Real powerslideRotationAddition = moduleData->m_powerslideRotationAddition;
466+
const Real rotationFactor = moduleData->m_rotationSpeedMultiplier;
467+
Real powerslideRotationAddition = moduleData->m_powerslideRotationAddition * m_isPowersliding;
468+
467469
if (ai) {
468470
Locomotor *loco = ai->getCurLocomotor();
469471
if (loco) {
@@ -473,16 +475,11 @@ void W3DTruckDraw::doDrawModule(const Matrix3D* transformMtx)
473475
}
474476
}
475477
}
476-
const Real rotationFactor = moduleData->m_rotationSpeedMultiplier;
478+
477479
m_frontWheelRotation += rotationFactor*speed;
478-
if (m_isPowersliding)
479-
{
480-
m_rearWheelRotation += rotationFactor*(speed + powerslideRotationAddition);
481-
}
482-
else
483-
{
484-
m_rearWheelRotation += rotationFactor*speed;
485-
}
480+
m_rearWheelRotation += rotationFactor*(speed + powerslideRotationAddition);
481+
m_frontWheelRotation = WWMath::Normalize_Angle(m_frontWheelRotation);
482+
m_rearWheelRotation = WWMath::Normalize_Angle(m_rearWheelRotation);
486483

487484
// For now, just use the same values for mid wheels -- may want to do independent calcs later...
488485
m_midFrontWheelRotation = m_frontWheelRotation;

GeneralsMD/Code/GameEngine/Include/Common/GameCommon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ enum WhichTurretType CPP_11(: Int)
487487

488488
// ------------------------------------------------------------------------
489489
// this normalizes an angle to the range -PI...PI.
490+
// TheSuperHackers @todo DO NOT USE THIS FUNCTION! Use WWMath::Normalize_Angle instead. Delete this.
490491
extern Real normalizeAngle(Real angle);
491492

492493
// ------------------------------------------------------------------------

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const char *TheRelationshipNames[] =
4848
};
4949

5050
//-------------------------------------------------------------------------------------------------
51+
// TheSuperHackers @todo DO NOT USE THIS FUNCTION! Use WWMath::Normalize_Angle instead. Delete this.
5152
Real normalizeAngle(Real angle)
5253
{
5354
DEBUG_ASSERTCRASH(!_isnan(angle), ("Angle is NAN in normalizeAngle!"));

GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DTankTruckDraw.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -540,21 +540,17 @@ void W3DTankTruckDraw::doDrawModule(const Matrix3D* transformMtx)
540540
const Coord3D *vel = physics->getVelocity();
541541
Real speed = physics->getVelocityMagnitude();
542542

543-
544543
const TWheelInfo *wheelInfo = getDrawable()->getWheelInfo(); // note, can return null!
545544
if (wheelInfo && (m_frontLeftTireBone || m_rearLeftTireBone))
546545
{
547-
static Real rotation = 0;
548546
const Real rotationFactor = getW3DTankTruckDrawModuleData()->m_rotationSpeedMultiplier;
547+
const Real powerslideRotationAddition = getW3DTankTruckDrawModuleData()->m_powerslideRotationAddition * m_isPowersliding;
548+
549549
m_frontWheelRotation += rotationFactor*speed;
550-
if (m_isPowersliding)
551-
{
552-
m_rearWheelRotation += rotationFactor*(speed+getW3DTankTruckDrawModuleData()->m_powerslideRotationAddition);
553-
}
554-
else
555-
{
556-
m_rearWheelRotation += rotationFactor*speed;
557-
}
550+
m_rearWheelRotation += rotationFactor*(speed+powerslideRotationAddition);
551+
m_frontWheelRotation = WWMath::Normalize_Angle(m_frontWheelRotation);
552+
m_rearWheelRotation = WWMath::Normalize_Angle(m_rearWheelRotation);
553+
558554
Matrix3D wheelXfrm(1);
559555
if (m_frontLeftTireBone)
560556
{

GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DTruckDraw.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,9 @@ void W3DTruckDraw::doDrawModule(const Matrix3D* transformMtx)
463463

464464
if (m_frontLeftTireBone || m_rearLeftTireBone)
465465
{
466-
Real powerslideRotationAddition = moduleData->m_powerslideRotationAddition;
466+
const Real rotationFactor = moduleData->m_rotationSpeedMultiplier;
467+
Real powerslideRotationAddition = moduleData->m_powerslideRotationAddition * m_isPowersliding;
468+
467469
if (ai) {
468470
Locomotor *loco = ai->getCurLocomotor();
469471
if (loco) {
@@ -473,16 +475,11 @@ void W3DTruckDraw::doDrawModule(const Matrix3D* transformMtx)
473475
}
474476
}
475477
}
476-
const Real rotationFactor = moduleData->m_rotationSpeedMultiplier;
478+
477479
m_frontWheelRotation += rotationFactor*speed;
478-
if (m_isPowersliding)
479-
{
480-
m_rearWheelRotation += rotationFactor*(speed + powerslideRotationAddition);
481-
}
482-
else
483-
{
484-
m_rearWheelRotation += rotationFactor*speed;
485-
}
480+
m_rearWheelRotation += rotationFactor*(speed + powerslideRotationAddition);
481+
m_frontWheelRotation = WWMath::Normalize_Angle(m_frontWheelRotation);
482+
m_rearWheelRotation = WWMath::Normalize_Angle(m_rearWheelRotation);
486483

487484
// For now, just use the same values for mid wheels -- may want to do independent calcs later...
488485
m_midFrontWheelRotation = m_frontWheelRotation;

0 commit comments

Comments
 (0)