Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
fix(gameengine): return the true forward speed and settle the physics defects
Olcay Seygan122e2dcd840c
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/PhysicsUpdate.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/PhysicsUpdate.hindex f32d77102..ec182b76c 100644--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/PhysicsUpdate.h+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/PhysicsUpdate.h@@ -144,6 +144,16 @@ public:Real getForwardSpeed2D() const; ///< compute speed along object's 2d direction vectorReal getForwardSpeed3D() const; ///< compute speed along object's 3d direction vector+ /**+ Signed speed of vel along the unit vector dir, i.e. the plain dot product. Exposed as a+ static so it can be checked without building an Object; see test_gameengine's+ physics_forward_speed_* witnesses. Pass zeroed z components for the 2d case.+ */+ static Real calcForwardSpeed( const Coord3D& vel, const Coord3D& dir )+ {+ return vel.x * dir.x + vel.y * dir.y + vel.z * dir.z;+ }+ObjectID getCurrentOverlap() const; ///< return object(s) being overlappedObjectID getPreviousOverlap() const; ///< return object(s) that were overlapped last frameObjectID getLastCollidee() const; ///< return object that was last collided with... can be quite olddiff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/DumbProjectileBehavior.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/DumbProjectileBehavior.cppindex d02210bc1..a2ad6085c 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/DumbProjectileBehavior.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/DumbProjectileBehavior.cpp@@ -375,7 +375,12 @@ void DumbProjectileBehavior::projectileFireAtObjectOrPosition( const Object *vicReal minRange = detWeap->getMinimumAttackRange();Real maxRange = detWeap->getUnmodifiedAttackRange();Real range = sqrt(ThePartitionManager->getDistanceSquared( projectile, &victimPosToUse, FROM_CENTER_2D ) );- Real rangeRatio = (range - minRange) / (maxRange - minRange);+ // guard the degenerate min==max range and clamp: a shot inside the minimum range gave a+ // negative ratio and a speed below MinWeaponSpeed (possibly <= 0, which then made+ // ceil(dist/speed) a negative or infinite segment count below).+ Real rangeSpan = maxRange - minRange;+ Real rangeRatio = (rangeSpan > 0.0f) ? ((range - minRange) / rangeSpan) : 1.0f;+ rangeRatio = clamp( 0.0f, rangeRatio, 1.0f );m_flightPathSpeed = (rangeRatio * (weaponSpeed - minWeaponSpeed)) + minWeaponSpeed;}else@@ -649,9 +654,10 @@ UpdateSleepTime DumbProjectileBehavior::update()orientMtx.buildTransformMatrix(Vector3(flightStep.x, flightStep.y, flightStep.z), curDir);getObject()->setTransformMatrix(&orientMtx);}- else // oops! how do we orient the projectile on the zeroeth frame? This didn't matter until we started using the+ else if ( m_flightPath.size() >= 2 ) // oops! how do we orient the projectile on the zeroeth frame? This didn't matter until we started using the//long, blurry projectile graphics which look badly oriented on step 0 of the flight path// so lets orient it the same as if it were on frame 1!+ // (a point blank shot has a single path point, and this used to read m_flightPath[1] anyway){Coord3D prevPos = m_flightPath[0];Coord3D curPos = m_flightPath[1];diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Locomotor.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Locomotor.cppindex 6a590ff9a..3fa0a4920 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Locomotor.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Locomotor.cpp@@ -1436,11 +1436,10 @@ void Locomotor::moveTowardsPositionWheels(Object* obj, PhysicsBehavior *physics,if (getFlag(IS_BRAKING)){- m_brakingFactor = slowDownDist/onPathDistToGoal;- m_brakingFactor *= m_brakingFactor;- if (m_brakingFactor>MAX_BRAKING_FACTOR) {- m_brakingFactor = MAX_BRAKING_FACTOR;- }+ // wheels deliberately brake at a flat factor, unlike treads: the ramped+ // slowDownDist/onPathDistToGoal calculation that used to sit here was overwritten by this+ // line on the very next statement, so it was dead. Behaviour unchanged; only the dead+ // computation is gone.m_brakingFactor = 1.0f;if (slowDownDist>onPathDistToGoal) {goalSpeed = actualSpeed-getBraking();diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/ObjectCreationList.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/ObjectCreationList.cppindex a9459024f..2a7e52af8 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/ObjectCreationList.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/ObjectCreationList.cpp@@ -1040,7 +1040,9 @@ protected:PhysicsBehavior *objectPhysics = obj->getPhysics();if( sourcePhysics && objectPhysics ){- objectPhysics->applyForce( sourcePhysics->getVelocity() );+ // this is a velocity, not a force: applyForce divides by mass, so heavy debris+ // used to inherit almost none of its source's motion.+ objectPhysics->addVelocityTo( sourcePhysics->getVelocity() );}}@@ -1439,7 +1441,7 @@ protected:if (TheGlobalData->m_preloadAssets)debrisModelNamesGlobalHack.push_back(debrisName);debrisNugget->m_names.push_back(AsciiString(debrisName));- debrisName = ini->getNextTokenOrNull();+ // (the for-increment already advances; advancing here too dropped every second name)}}diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/MissileAIUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/MissileAIUpdate.cppindex 4da900197..c587a8935 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/MissileAIUpdate.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/MissileAIUpdate.cpp@@ -438,7 +438,9 @@ void MissileAIUpdate::doKillSelfState(){const MissileAIUpdateModuleData *modData = getMissileAIUpdateModuleData();- if (m_stateTimestamp > TheGameLogic->getFrame() - modData->m_killSelfDelay )+ // added, not subtracted: these are unsigned, so in the first killSelfDelay frames of a match+ // getFrame() - m_killSelfDelay wrapped to ~4 billion and the missile self-killed immediately.+ if (m_stateTimestamp + modData->m_killSelfDelay > TheGameLogic->getFrame() ){// Hold in this state [modData->m_killSelfDelay] frames to let the contrail catch up. jba.return;diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/PhysicsUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/PhysicsUpdate.cppindex 343a23aee..43f3db636 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/PhysicsUpdate.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/PhysicsUpdate.cpp@@ -45,6 +45,7 @@#include "GameLogic/TerrainLogic.h"#include "GameLogic/Weapon.h"#include "GameLogic/LogicRandomValue.h"+#include "GameClient/Statistics.h" // MuLaw/NormalizeToRange, for the bounce sound volumeconst Real DEFAULT_MASS = 1.0f;@@ -964,20 +965,21 @@ Real PhysicsBehavior::getForwardSpeed2D() const{const Coord3D *dir = getObject()->getUnitDirectionVector2D();- Real vx = m_vel.x * dir->x;- Real vy = m_vel.y * dir->y;-- Real dot = vx + vy;-- Real speedSquared = vx*vx + vy*vy;-// DEBUG_ASSERTCRASH( speedSquared != 0, ("zero speedSquared will overflow sqrtf()!") );// lorenzen... sanity check-- Real speed = (Real)sqrtf( speedSquared );-- if (dot >= 0.0f)- return speed;-- return -speed;+ //+ // The forward speed is the projection of the velocity onto the facing, i.e. the dot product.+ // This used to return sqrt((vx*dx)^2 + (vy*dy)^2) instead, which is exact on the axes but+ // only 0.707x the real speed at 45 degrees. The locomotors drive on+ // speedDelta = goalSpeed - getForwardSpeed2D() and nothing else caps velocity (forward+ // friction is suppressed while motive), so the loop settled at goalSpeed/0.707 and a+ // diagonally moving unit ran up to 1.41x its max speed. applyFrictionalForces() a few+ // hundred lines up always used the correct dot product.+ //+ Coord3D vel2D = m_vel;+ vel2D.z = 0.0f;+ Coord3D dir2D = *dir;+ dir2D.z = 0.0f;++ return calcForwardSpeed( vel2D, dir2D );}//-------------------------------------------------------------------------------------------------@@ -989,18 +991,13 @@ Real PhysicsBehavior::getForwardSpeed3D() const{Vector3 dir = getObject()->getTransformMatrix()->Get_X_Vector();- Real vx = m_vel.x * dir.X;- Real vy = m_vel.y * dir.Y;- Real vz = m_vel.z * dir.Z;+ // same wrong-norm bug as getForwardSpeed2D above - see the comment there.+ Coord3D dir3D;+ dir3D.x = dir.X;+ dir3D.y = dir.Y;+ dir3D.z = dir.Z;- Real dot = vx + vy + vz;-- Real speed = (Real)sqrtf( vx*vx + vy*vy + vz*vz );-- if (dot >= 0.0f)- return speed;-- return -speed;+ return calcForwardSpeed( m_vel, dir3D );}//-------------------------------------------------------------------------------------------------@@ -1142,11 +1139,12 @@ void PhysicsBehavior::doBounceSound(const Coord3D& prevPos)mass = 0;}-#ifdef FIX_AUDIO+ // this was fenced behind FIX_AUDIO, which is never defined, so every bounce played at full+ // volume no matter how light or slow the thing was. vel and mass are clamped to+ // [0,NORMAL_*] above, so MuLaw stays in [-1,1] and the scale lands in [0.25,1] per term.Real volAdjust = NormalizeToRange(MuLaw(vel, NORMAL_VEL_Z, 500), -1, 1, 0.25, 1.0);volAdjust *= NormalizeToRange(MuLaw(mass, NORMAL_MASS, 500), -1, 1, 0.25, 1.0);collisionSound.setVolume(volAdjust);-#endifcollisionSound.setObjectID(getObject()->getID());TheAudio->addAudioEvent(&collisionSound);}diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/ToppleUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/ToppleUpdate.cppindex bde5710e5..a44edbc48 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/ToppleUpdate.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/ToppleUpdate.cpp@@ -161,6 +161,14 @@ void ToppleUpdate::applyTopplingForce( const Coord3D* toppleDirection, Real toppm_toppleDirection.normalize();TheScriptEngine->adjustToppleDirection(getObject(), &m_toppleDirection);+ // a crusher with no PhysicsBehavior (or one that is standing still while overlapping) hands us+ // a zero speed. That left both angular terms at zero: ANGULAR_LIMIT/(0*2) is infinity, which+ // REAL_TO_INT_FLOOR turns into garbage below, and the object then sat in TOPPLE_FALLING+ // forever, updating every frame and never dying. Floor it so it always actually falls.+ const Real MIN_TOPPLE_SPEED = 0.01f;+ if (toppleSpeed < MIN_TOPPLE_SPEED)+ toppleSpeed = MIN_TOPPLE_SPEED;+m_angularVelocity = toppleSpeed * d->m_initialVelocityPercent;m_angularAcceleration = toppleSpeed * d->m_initialAccelPercent;m_toppleState = TOPPLE_FALLING;diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 18a1d61f4..2a3cf120a 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -28,9 +28,11 @@#include "Common/STLTypedefs.h"#include "Common/StackDump.h"#include "GameClient/Water.h"+#include "GameLogic/Module/PhysicsUpdate.h"#include <stdio.h>#include <string.h>+#include <math.h>//////////////////////////////////////////////////////////////////////////////// Boot scaffolding@@ -469,3 +471,43 @@ TEST(logic_tick_is_wall_clock_paced_not_render_paced)/* A non-positive fps never throttles (the -noFPSLimit style dev mode). */CHECK( GameEngine_isLogicFrameDue( accum, 0.0f, 0 ) );}++/* PhysicsUpdate.cpp: the forward speed a locomotor steers on is the projection+ of the velocity onto the facing - a plain dot product. It used to be+ sqrt((vx*dx)^2 + (vy*dy)^2), which is exact on the axes but reads only+ sqrt(cos^4 + sin^4) = 0.707 of the true speed at 45 degrees. Since the+ locomotors close speedDelta = goalSpeed - actualSpeed by accelerating, and+ nothing else caps velocity, an under-read made diagonal units settle at+ goalSpeed/0.707 - up to 1.41x their max speed.++ The witness is heading independence: drive at a known speed along the+ facing and the reported forward speed must be that speed at every heading. */+TEST(physics_forward_speed_is_the_projection_not_a_per_axis_norm)+{+ const Real speed = 40.0f;++ for( Int deg = 0; deg <= 360; deg += 5 )+ {+ Real a = (Real)(deg * PI / 180.0);+ Coord3D dir; dir.x = (Real)cos(a); dir.y = (Real)sin(a); dir.z = 0.0f;+ Coord3D vel; vel.x = dir.x * speed; vel.y = dir.y * speed; vel.z = 0.0f;++ /* moving along the facing: full speed, whatever the heading. The old+ code returned 0.707*speed here at 45/135/225/315 degrees. */+ CHECK_NEAR( PhysicsBehavior::calcForwardSpeed( vel, dir ), speed, 0.01f );++ /* moving backwards along the facing: the same speed, negated. */+ Coord3D back; back.x = -vel.x; back.y = -vel.y; back.z = 0.0f;+ CHECK_NEAR( PhysicsBehavior::calcForwardSpeed( back, dir ), -speed, 0.01f );++ /* moving straight across the facing contributes nothing. The old code+ returned a positive magnitude here for every off-axis heading. */+ Coord3D side; side.x = -dir.y * speed; side.y = dir.x * speed; side.z = 0.0f;+ CHECK_NEAR( PhysicsBehavior::calcForwardSpeed( side, dir ), 0.0f, 0.01f );+ }++ /* the 3d form is the same dot product, z included. */+ Coord3D up; up.x = 0.0f; up.y = 0.0f; up.z = 1.0f;+ Coord3D climb; climb.x = 0.0f; climb.y = 0.0f; climb.z = 12.0f;+ CHECK_NEAR( PhysicsBehavior::calcForwardSpeed( climb, up ), 12.0f, 0.01f );+}