Skip to content

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.

fix(gameengine): ease helicopter acceleration, braking, turns and lean

Olcay Seygan6d158fff5682
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1cf6e0a38..718bbd656 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -41,7 +41,8 @@ found and fixed — EA's own, not port damage.**
- Every helicopter in the game, on every side and for every general, used to drive like a tank with rotors. Told to go somewhere behind it, a Comanche swung its nose all the way round on the spot and only then flew off nose first. Now it flies where it is sent, backwards or sideways, with its nose left where it was. Sent 300 behind itself, a Comanche turned 178 degrees before it went and was 30 out after a second and a half; now it goes tail first, never turns, and is 30 out after one second. On a long flight the nose comes round into the direction of travel while the helicopter is already on its way.
- A Comanche keeps its guns on what it is shooting at while it flies. Its cannon and missiles only fire straight ahead, so a Comanche ordered to move in the middle of a fight turned away from its target. It now holds its nose on the target and slides along beside it: one sent across in front of an Overlord it had been shooting stayed within a degree of the tank the whole way, where it used to point 84 degrees off on average. One flying past a supply truck 120 off its line now turns to it and shoots, where it used to fly straight by.
-- A helicopter leans into the move. It dips its nose to speed up and lifts it to stop, rolls into a slide sideways, holds a slight lean at cruising speed, and comes level again in a hover. A replay recorded before this change plays back differently.
+- A helicopter leans into the move. It dips its nose to speed up and lifts it to stop, rolls into a slide sideways and comes level again in a hover, never past about 12 degrees and never with a jerk: the lean moves by at most a degree and a half from one frame to the next.
+- Helicopters ease in and out of everything they do. Speed builds up over a moment instead of all at once, a stop flares out and settles on the exact spot it was sent to, and a turn winds up to full speed over a third of a second and winds down again before the nose arrives. Chinooks and Helixes fly the same way as the Comanche, and a gattling Helix keeps its gun on a tank while it slides past. A replay recorded before this change plays back differently.
## The frame rate cap is gone
diff --git a/GeneralsMD/Code/BuildFingerprint.manifest b/GeneralsMD/Code/BuildFingerprint.manifest
index 824b68922..ce87777a2 100644
--- a/GeneralsMD/Code/BuildFingerprint.manifest
+++ b/GeneralsMD/Code/BuildFingerprint.manifest
@@ -44,6 +44,7 @@ Data/Scenarios/bridgelong.txt
Data/Scenarios/chokeprobe.txt
Data/Scenarios/crowdbattle.txt
Data/Scenarios/helislide.txt
+Data/Scenarios/helitypes.txt
Data/Scenarios/inferno30.txt
Data/Scenarios/jetpass.txt
Data/Scenarios/leadinfantry.txt
diff --git a/GeneralsMD/Code/Data/Scenarios/helislide.txt b/GeneralsMD/Code/Data/Scenarios/helislide.txt
index 4e326c476..9d3290b61 100644
--- a/GeneralsMD/Code/Data/Scenarios/helislide.txt
+++ b/GeneralsMD/Code/Data/Scenarios/helislide.txt
@@ -16,6 +16,12 @@
# flies backwards (180 degrees off its nose) and sideways without turning, is 30 out after 30
# frames, and turns its nose 165 degrees on the westward flight while already moving. The second
# turns to the truck and takes 50 off it. The third stays within 0.9 degrees of the Overlord.
+#
+# Eased the same day. The first Comanche's acceleration now changes by at most 0.01 a frame, where
+# it jumped by up to 0.25; the westward turn winds up to 6 degrees a frame and changes rate by 0.6
+# a frame at most, where it jumped by 3; it ends every leg with no overshoot. Its lean, replayed
+# from the logged motion, tops out at 12.4 degrees and moves 1.3 a frame at most, where the first
+# free-flight build hit 22.9 and moved 30 in a frame when it braked.
30 spawn 0 AmericaVehicleComanche 1 1100 3500
150 move 0 AmericaVehicleComanche 800 3500
diff --git a/GeneralsMD/Code/Data/Scenarios/helitypes.txt b/GeneralsMD/Code/Data/Scenarios/helitypes.txt
new file mode 100644
index 000000000..2c4477529
--- /dev/null
+++ b/GeneralsMD/Code/Data/Scenarios/helitypes.txt
@@ -0,0 +1,40 @@
+# helislide.txt for the other helicopters. A Chinook and a Helix are each sent 300 behind
+# themselves, then 300 beside, then on a long flight east and back west. A General Tsing Shi Tao
+# Helix buys its gattling cannon and is then put on a Crusader, which cannot shoot at aircraft, and
+# ordered across in front of it.
+#
+# generals.exe -headless -quickstart -noshellmap -observer -multiInstance -noFPSLimit
+# -map "Maps\Twilight Flame\Twilight Flame.map" -autoskirmish 2 -takeover
+# -side 0 FactionAmerica -side 1 FactionChina -seed 7 -maxframes 2200
+# -scenario helitypes -logPrefix helitypes_
+#
+# Measured 2026-10-03 with a temporary per-frame log of position, facing, velocity and acceleration.
+# Both pass Locomotor::isHelicopter, as does every Chinook and Helix in the INI. Before helicopters
+# flew free, each turned its nose 178 degrees before it went behind itself. Now the Chinook and the
+# Helix go backwards and sideways without turning, are 30 out after 32 frames, end every leg within
+# 0.7 of the goal with no overshoot, and turn into the long westward flight while moving, the nose
+# winding up to 6 degrees a frame. Acceleration changes by at most 0.01 a frame. Their lean,
+# replayed from the logged motion, tops out at 12.5 degrees and moves 1.5 a frame at most. The
+# gattling Helix takes the Crusader from 448 to 378 health while it slides across in front of it.
+
+30 spawn 0 AmericaVehicleChinook 1 1100 3200
+150 move 0 AmericaVehicleChinook 800 3200
+450 move 0 AmericaVehicleChinook 800 3500
+750 move 0 AmericaVehicleChinook 2500 3200
+1300 move 0 AmericaVehicleChinook 800 3200
+
+30 spawn 1 ChinaVehicleHelix 1 1100 2600
+150 move 1 ChinaVehicleHelix 800 2600
+450 move 1 ChinaVehicleHelix 800 2900
+750 move 1 ChinaVehicleHelix 2000 2600
+1450 move 1 ChinaVehicleHelix 800 2600
+
+30 spawn 1 Tank_ChinaVehicleHelix 1 1180 4480
+40 shiftupgrade 1 Tank_ChinaVehicleHelix Upgrade_ChinaHelixGattlingCannon
+760 spawn 0 AmericaTankCrusader 1 1300 4560
+770 attack 1 Tank_ChinaVehicleHelix 0 AmericaTankCrusader
+820 move 1 Tank_ChinaVehicleHelix 1420 4480
+820 tally 0 AmericaTankCrusader
+880 tally 0 AmericaTankCrusader
+940 tally 0 AmericaTankCrusader
+1000 tally 0 AmericaTankCrusader
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/Drawable.h b/GeneralsMD/Code/GameEngine/Include/GameClient/Drawable.h
index b937f07dd..b177df315 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/Drawable.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/Drawable.h
@@ -177,6 +177,9 @@ public:
Real m_wobble; ///< for wobbling
Real m_yawModulator; ///< for the swimmy soft hover of a helicopter
Real m_pitchModulator; ///< for the swimmy soft hover of a helicopter
+ Real m_leanPitch[2]; ///< a helicopter's pitch target, smoothed through two stages once a logic frame
+ Real m_leanRoll[2]; ///< the same for roll
+ UnsignedInt m_leanFrame; ///< the logic frame the lean targets were last stepped on
TWheelInfo m_wheelInfo; ///< Wheel offset & angle info for a wheeled type locomotor.
DrawableLocoInfo();
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/Locomotor.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/Locomotor.h
index 7a8218cee..b2c3022c1 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/Locomotor.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/Locomotor.h
@@ -392,6 +392,9 @@ protected:
void moveTowardsPositionHover(Object* obj, PhysicsBehavior *physics, const Coord3D& goalPos, Real onPathDistToGoal, Real desiredSpeed, const Coord3D *faceTarget);
void moveTowardsPositionHelicopter(Object* obj, PhysicsBehavior *physics, const Coord3D& goalPos, Real onPathDistToGoal, Real desiredSpeed, const Coord3D *faceTarget);
void brakeHelicopter(Object* obj, PhysicsBehavior *physics);
+ PhysicsTurningType turnHelicopter(Object* obj, const Coord3D *toward);
+ void steerHelicopter(Object* obj, PhysicsBehavior *physics, Real wantX, Real wantY);
+ Real getHelicopterStopDecel(BodyDamageType condition) const;
void moveTowardsPositionThrust(Object* obj, PhysicsBehavior *physics, const Coord3D& goalPos, Real onPathDistToGoal, Real desiredSpeed);
void moveTowardsPositionWings(Object* obj, PhysicsBehavior *physics, const Coord3D& goalPos, Real onPathDistToGoal, Real desiredSpeed);
@@ -478,6 +481,9 @@ private:
Real m_angleOffset;
Real m_offsetIncrement;
UnsignedInt m_donutTimer; ///< Frame time to keep units from doing the donut. jba.
+ Real m_helicopterYawRate; ///< radians a frame a helicopter's nose is swinging, eased up and down
+ Real m_helicopterAccelX; ///< the 2D acceleration a helicopter is pulling, eased towards what it wants
+ Real m_helicopterAccelY;
};
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp
index ea2c84110..bf4a3cf61 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp
@@ -223,6 +223,9 @@ DrawableLocoInfo::DrawableLocoInfo()
m_yawModulator = 0.0f;
m_pitchModulator = 0.0f;
+ m_leanPitch[0] = m_leanPitch[1] = 0.0f;
+ m_leanRoll[0] = m_leanRoll[1] = 0.0f;
+ m_leanFrame = 0;
}
// ------------------------------------------------------------------------------------------------
@@ -1751,6 +1754,8 @@ void Drawable::calcPhysicsXformHoverOrWings( const Locomotor *locomotor, Physics
{
const Real LEAN_AT_FULL_ACCEL = 0.2f; // radians, at the locomotor's own Acceleration
const Real LEAN_AT_FULL_SPEED = 0.12f; // radians, held at the locomotor's own Speed
+ const Real SOFT_LEAN = 0.27f; // the lean flattens out towards this, about 15 degrees
+ const Real LEAN_EASE_FRAMES = 5.0f; // each of the two smoothing stages, in logic frames
const Real MAX_LEAN = 0.4f;
if (accelPitchLimit == 0.0f)
accelPitchLimit = MAX_LEAN;
@@ -1770,9 +1775,24 @@ void Drawable::calcPhysicsXformHoverOrWings( const Locomotor *locomotor, Physics
Real forwardAccel = dir->x * accel->x + dir->y * accel->y;
Real lateralAccel = -dir->y * accel->x + dir->x * accel->y;
- pitchTarget = forwardAccel * accelScale + forwardVel * speedScale;
- rollTarget = lateralAccel * accelScale + lateralVel * speedScale;
+ pitchTarget = SOFT_LEAN * tanh((forwardAccel * accelScale + forwardVel * speedScale) / SOFT_LEAN);
+ rollTarget = SOFT_LEAN * tanh((lateralAccel * accelScale + lateralVel * speedScale) / SOFT_LEAN);
}
+
+ /* The acceleration jumps from one logic frame to the next, and the spring below runs once a
+ drawn frame, which at 120 frames a second is fast enough to copy every jump. Two smoothing
+ stages stepped once a logic frame ease the lean in and out over about half a second. */
+ UnsignedInt now = TheGameLogic->getFrame();
+ if (now != m_locoInfo->m_leanFrame)
+ {
+ m_locoInfo->m_leanFrame = now;
+ m_locoInfo->m_leanPitch[0] += (pitchTarget - m_locoInfo->m_leanPitch[0]) / LEAN_EASE_FRAMES;
+ m_locoInfo->m_leanPitch[1] += (m_locoInfo->m_leanPitch[0] - m_locoInfo->m_leanPitch[1]) / LEAN_EASE_FRAMES;
+ m_locoInfo->m_leanRoll[0] += (rollTarget - m_locoInfo->m_leanRoll[0]) / LEAN_EASE_FRAMES;
+ m_locoInfo->m_leanRoll[1] += (m_locoInfo->m_leanRoll[0] - m_locoInfo->m_leanRoll[1]) / LEAN_EASE_FRAMES;
+ }
+ pitchTarget = m_locoInfo->m_leanPitch[1];
+ rollTarget = m_locoInfo->m_leanRoll[1];
}
// process chassis acceleration dynamics - damp back towards zero, or a helicopter's lean
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Locomotor.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Locomotor.cpp
index 6dabb9fd8..4d8a9f1ee 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Locomotor.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Locomotor.cpp
@@ -767,6 +767,9 @@ Locomotor::Locomotor(const LocomotorTemplate* tmpl)
m_offsetIncrement = (PI/40) * (GameLogicRandomValueReal(0.8f, 1.2f)/m_template->m_wanderLengthFactor);
setFlag(OFFSET_INCREASING, GameLogicRandomValue(0,1));
m_donutTimer = TheGameLogic->getFrame()+DONUT_TIME_DELAY_SECONDS*LOGICFRAMES_PER_SECOND;
+ m_helicopterYawRate = 0.0f;
+ m_helicopterAccelX = 0.0f;
+ m_helicopterAccelY = 0.0f;
}
//-------------------------------------------------------------------------------------------------
@@ -796,6 +799,9 @@ Locomotor::Locomotor(const Locomotor& that)
m_sineDescentDistance = that.m_sineDescentDistance;
m_angleOffset = that.m_angleOffset;
m_offsetIncrement = that.m_offsetIncrement;
+ m_helicopterYawRate = that.m_helicopterYawRate;
+ m_helicopterAccelX = that.m_helicopterAccelX;
+ m_helicopterAccelY = that.m_helicopterAccelY;
}
//-------------------------------------------------------------------------------------------------
@@ -818,6 +824,9 @@ Locomotor& Locomotor::operator=(const Locomotor& that)
m_preferredHeight = that.m_preferredHeight;
m_preferredHeightDamping = that.m_preferredHeightDamping;
m_sineDescentDistance = that.m_sineDescentDistance;
+ m_helicopterYawRate = that.m_helicopterYawRate;
+ m_helicopterAccelX = that.m_helicopterAccelX;
+ m_helicopterAccelY = that.m_helicopterAccelY;
}
return *this;
}
@@ -839,12 +848,13 @@ void Locomotor::crc( Xfer *xfer )
/** Xfer method
* Version Info:
* 1: Initial version
- * 3: m_sineDescentDistance */
+ * 3: m_sineDescentDistance
+ * 4: m_helicopterYawRate, m_helicopterAccelX, m_helicopterAccelY */
// ------------------------------------------------------------------------------------------------
void Locomotor::xfer( Xfer *xfer )
{
// version
- const XferVersion currentVersion = 3;
+ const XferVersion currentVersion = 4;
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
@@ -872,6 +882,13 @@ void Locomotor::xfer( Xfer *xfer )
if (version >= 3)
xfer->xferReal(&m_sineDescentDistance);
+ if (version >= 4)
+ {
+ xfer->xferReal(&m_helicopterYawRate);
+ xfer->xferReal(&m_helicopterAccelX);
+ xfer->xferReal(&m_helicopterAccelY);
+ }
+
} // end xfer
// ------------------------------------------------------------------------------------------------
@@ -1019,7 +1036,10 @@ void Locomotor::locoUpdate_moveTowardsAngle(Object* obj, Real goalAngle)
physics->setTurning(rotating);
// a helicopter turning in place has no sideways friction to stop a slide it came in with
if (isHelicopter(obj))
+ {
brakeHelicopter(obj, physics);
+ m_helicopterYawRate = 0.0f; // this turn is rotateTowardsPosition's, which parking needs to stop dead on the heading
+ }
handleBehaviorZ(obj, physics, *obj->getPosition());
}
@@ -1051,7 +1071,15 @@ void Locomotor::setPhysicsOptions(Object* obj)
physics->setExtraFriction(m_template->m_extra2DFriction + extraExtraFriction);
physics->setAllowAirborneFriction(getApply2DFrictionWhenAirborne()); // you'd think we wouldn't want friction in the air, but it's needed for realistic behavior.
physics->setStickToGround(getStickToGround()); // walking guys aren't allowed to catch huge (or even small) air.
- physics->setMotiveSteersSideways(isHelicopter(obj));
+ Bool helicopter = isHelicopter(obj);
+ physics->setMotiveSteersSideways(helicopter);
+ if (!helicopter)
+ {
+ // landed, taxiing or dying: whatever it was pulling or swinging when it last flew is over
+ m_helicopterYawRate = 0.0f;
+ m_helicopterAccelX = 0.0f;
+ m_helicopterAccelY = 0.0f;
+ }
}
//-------------------------------------------------------------------------------------------------
@@ -2045,16 +2073,35 @@ void Locomotor::moveTowardsPositionHover(Object* obj, PhysicsBehavior *physics,
//-------------------------------------------------------------------------------------------------
/*
- A helicopter flies the velocity it wants rather than the way it faces. Every frame it pushes its
- 2D velocity towards the goal at the speed it can still stop from, by at most its Acceleration,
- or its Braking while it is shedding speed in the direction it is going. The nose is a separate
+ A helicopter flies the velocity it wants rather than the way it faces. It wants the goal's
+ direction at the speed it can still stop from, and steerHelicopter eases its 2D velocity there.
+ The planned stop is gentler than Braking (getHelicopterStopDecel), starts early enough for the
+ eased braking to build up, and fades out over the last HELICOPTER_STOP_EASE_FRAMES, so the
+ helicopter flares to a halt on the goal instead of slamming into one. The nose is a separate
choice: on the target when there is one, turned into the flight while the goal is more than
- LONG_FLIGHT_SECONDS out at full speed, and otherwise left where it is, so a short hop behind or
- beside the helicopter is flown backwards or sideways and a long one turns as it goes.
+ LONG_FLIGHT_SECONDS out at full speed and LONG_FLIGHT_MIN_DIST away, and otherwise left where it
+ is, so a short hop behind or beside the helicopter is flown backwards or sideways and a long one
+ turns as it goes.
*/
+static const Real HELICOPTER_STOP_EASE_FRAMES = 6.0f; // the planned braking fades out over about this many frames
+static const Real HELICOPTER_YAW_EASE_FRAMES = 10.0f; // frames for the nose to wind up to TurnRate, or back down from it
+static const Real HELICOPTER_ACCEL_EASE_FRAMES = 5.0f; // frames for the pull to build from nothing to full Acceleration
+static const Real HELICOPTER_VELOCITY_FRAMES = 4.0f; // a velocity error is asked back over about this many frames
+// the stop is planned this many frames of travel early, for the braking to build up in
+static const Real HELICOPTER_STOP_LEAD_FRAMES = HELICOPTER_ACCEL_EASE_FRAMES * 0.5f + HELICOPTER_VELOCITY_FRAMES;
+
+Real Locomotor::getHelicopterStopDecel(BodyDamageType condition) const
+{
+ Real decel = 2.0f * getMaxAcceleration(condition);
+ if (decel > getBraking())
+ decel = getBraking();
+ return decel;
+}
+
void Locomotor::moveTowardsPositionHelicopter(Object* obj, PhysicsBehavior *physics, const Coord3D& goalPos, Real onPathDistToGoal, Real desiredSpeed, const Coord3D *faceTarget)
{
const Real LONG_FLIGHT_SECONDS = 3.0f;
+ const Real LONG_FLIGHT_MIN_DIST = 400.0f; // so a slow Helix still slides a short hop instead of turning for it
const Real TOO_CLOSE_TO_AIM = 1.0f; // right over the target the bearing to it is noise
BodyDamageType bdt = obj->getBodyModule()->getDamageState();
@@ -2063,22 +2110,35 @@ void Locomotor::moveTowardsPositionHelicopter(Object* obj, PhysicsBehavior *phys
desiredSpeed = maxSpeed;
const Coord3D *pos = obj->getPosition();
- PhysicsTurningType rotating = TURN_NONE;
+ const Coord3D *noseToward = NULL;
if (faceTarget)
{
if (fabs(faceTarget->x - pos->x) > TOO_CLOSE_TO_AIM || fabs(faceTarget->y - pos->y) > TOO_CLOSE_TO_AIM)
- rotating = rotateTowardsPosition(obj, *faceTarget);
+ noseToward = faceTarget;
}
- else if (onPathDistToGoal > maxSpeed * LOGICFRAMES_PER_SECOND * LONG_FLIGHT_SECONDS)
+ else if (onPathDistToGoal > maxSpeed * LOGICFRAMES_PER_SECOND * LONG_FLIGHT_SECONDS
+ && onPathDistToGoal > LONG_FLIGHT_MIN_DIST)
{
- rotating = rotateTowardsPosition(obj, goalPos);
+ noseToward = &goalPos;
}
- physics->setTurning(rotating);
+ physics->setTurning(turnHelicopter(obj, noseToward));
Real goalSpeed = desiredSpeed;
if (!getFlag(NO_SLOW_DOWN_AS_APPROACHING_DEST))
{
- Real stopSpeed = sqrt(2.0f * getBraking() * onPathDistToGoal);
+ /* The fastest speed it can still stop from in the distance left, slowing at decel. The
+ plain sqrt(2*decel*dist) holds full braking to the last frame and then stops dead; with
+ the ease speed taken off, the braking fades out at the end. The distance is counted short
+ by the travel of the lead frames, the time the eased braking takes to build up, or the
+ helicopter overshoots a short hop. */
+ const Coord3D *vel = physics->getVelocity();
+ Real speed = sqrt(sqr(vel->x) + sqr(vel->y));
+ Real distLeft = onPathDistToGoal - speed * HELICOPTER_STOP_LEAD_FRAMES;
+ if (distLeft < 0.0f)
+ distLeft = 0.0f;
+ Real decel = getHelicopterStopDecel(bdt);
+ Real ease = decel * HELICOPTER_STOP_EASE_FRAMES;
+ Real stopSpeed = sqrt(2.0f * decel * distLeft + ease * ease) - ease;
if (goalSpeed > stopSpeed)
goalSpeed = stopSpeed;
}
@@ -2094,47 +2154,100 @@ void Locomotor::moveTowardsPositionHelicopter(Object* obj, PhysicsBehavior *phys
wantY = dy * goalSpeed / dist;
}
+ steerHelicopter(obj, physics, wantX, wantY);
+}
+
+//-------------------------------------------------------------------------------------------------
+/**
+ Ease a helicopter's 2D velocity towards (wantX, wantY). It asks for the velocity error back over
+ HELICOPTER_VELOCITY_FRAMES, no harder than its Acceleration, or its Braking when that slows the way
+ it is going; and the pull it actually applies moves towards that by at most an
+ HELICOPTER_ACCEL_EASE_FRAMES share of its Acceleration a frame, so a start, a stop or a change
+ of direction builds up and dies away instead of jumping.
+*/
+void Locomotor::steerHelicopter(Object* obj, PhysicsBehavior *physics, Real wantX, Real wantY)
+{
+ BodyDamageType bdt = obj->getBodyModule()->getDamageState();
+ Real maxAccel = getMaxAcceleration(bdt);
const Coord3D *vel = physics->getVelocity();
- Real ax = wantX - vel->x;
- Real ay = wantY - vel->y;
- Real need = sqrt(ax*ax + ay*ay);
- if (need == 0.0f)
- return;
- Real limit = (ax * vel->x + ay * vel->y < 0.0f) ? getBraking() : getMaxAcceleration(bdt);
- if (need > limit)
+ Real ax = (wantX - vel->x) / HELICOPTER_VELOCITY_FRAMES;
+ Real ay = (wantY - vel->y) / HELICOPTER_VELOCITY_FRAMES;
+ Real ask = sqrt(ax*ax + ay*ay);
+ Real limit = (ax * vel->x + ay * vel->y < 0.0f) ? getBraking() : maxAccel;
+ if (ask > limit)
{
- ax *= limit / need;
- ay *= limit / need;
+ ax *= limit / ask;
+ ay *= limit / ask;
}
+ Real changeX = ax - m_helicopterAccelX;
+ Real changeY = ay - m_helicopterAccelY;
+ Real change = sqrt(changeX*changeX + changeY*changeY);
+ Real jerk = maxAccel / HELICOPTER_ACCEL_EASE_FRAMES;
+ if (change > jerk)
+ {
+ changeX *= jerk / change;
+ changeY *= jerk / change;
+ }
+ m_helicopterAccelX += changeX;
+ m_helicopterAccelY += changeY;
+
+ if (m_helicopterAccelX == 0.0f && m_helicopterAccelY == 0.0f)
+ return;
+
Real mass = physics->getMass();
Coord3D force;
- force.x = mass * ax;
- force.y = mass * ay;
+ force.x = mass * m_helicopterAccelX;
+ force.y = mass * m_helicopterAccelY;
force.z = 0.0f;
physics->applyMotiveForce(&force);
}
//-------------------------------------------------------------------------------------------------
-/// stop a helicopter's 2D velocity whichever way it points, at its Braking
+/// bring a helicopter's 2D velocity to rest whichever way it points, eased like any other change
void Locomotor::brakeHelicopter(Object* obj, PhysicsBehavior *physics)
{
- const Coord3D *vel = physics->getVelocity();
- Real speed = sqrt(sqr(vel->x) + sqr(vel->y));
- if (speed == 0.0f)
- return;
+ steerHelicopter(obj, physics, 0.0f, 0.0f);
+}
- Real cut = getBraking();
- if (cut > speed)
- cut = speed;
+//-------------------------------------------------------------------------------------------------
+/**
+ Swing a helicopter's nose towards a point, or let the swing die away when toward is null. The
+ turn winds up to TurnRate over HELICOPTER_YAW_EASE_FRAMES and winds down again so it stops on the
+ heading, where rotateTowardsPosition jumps straight to full rate and stops dead.
+*/
+PhysicsTurningType Locomotor::turnHelicopter(Object* obj, const Coord3D *toward)
+{
+ Real maxRate = getMaxTurnRate(obj->getBodyModule()->getDamageState());
+ Real yawAccel = maxRate / HELICOPTER_YAW_EASE_FRAMES;
+ Real angle = obj->getOrientation();
- Real scale = -physics->getMass() * cut / speed;
- Coord3D force;
- force.x = scale * vel->x;
- force.y = scale * vel->y;
- force.z = 0.0f;
- physics->applyMotiveForce(&force);
+ Real wantRate = 0.0f;
+ if (toward)
+ {
+ const Coord3D *pos = obj->getPosition();
+ Real error = stdAngleDiff(ATan2(toward->y - pos->y, toward->x - pos->x), angle);
+ // as fast as it can go and still wind down onto the heading, and never past it in one frame
+ Real rate = sqrt(2.0f * yawAccel * fabs(error));
+ if (rate > maxRate)
+ rate = maxRate;
+ if (rate > fabs(error))
+ rate = fabs(error);
+ wantRate = (error < 0.0f) ? -rate : rate;
+ }
+
+ Real change = wantRate - m_helicopterYawRate;
+ if (change > yawAccel)
+ change = yawAccel;
+ else if (change < -yawAccel)
+ change = -yawAccel;
+ m_helicopterYawRate += change;
+
+ if (m_helicopterYawRate == 0.0f)
+ return TURN_NONE;
+ obj->setOrientation(normalizeAngle(angle + m_helicopterYawRate));
+ return (m_helicopterYawRate > 0.0f) ? TURN_POSITIVE : TURN_NEGATIVE;
}
//-------------------------------------------------------------------------------------------------
@@ -2839,6 +2952,7 @@ void Locomotor::maintainCurrentPositionHover(Object* obj, PhysicsBehavior *physi
physics->setTurning(TURN_NONE);
if (isHelicopter(obj))
{
+ physics->setTurning(turnHelicopter(obj, NULL)); // a swing still going winds down rather than stopping dead
if (physics->isMotive())
brakeHelicopter(obj, physics);
return;