From: James Turner Date: Mon, 11 Jan 2016 04:10:10 +0000 (-0600) Subject: Current time of day (warp) fixes: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=f2bf30bcc7fae11d29e31e51f1dbc8d63bfd4917;p=flightgear.git Current time of day (warp) fixes: - sim-speed-up (a/A keys) offsets warp correctly. This means at 4x speed up the warp will increase by 3 seconds for one elapsed real second. In practice this means simulation elements timed using sim ‘current time’ will be correctly synchronised with those using simulated elapsed time. (AI traffic being a prime example of this) - warp-delta (t/T keys) is independent of frame rate, and works when paused. The warp-delta values in the keybindings / GUI dialog will need to be updated since warp-delta in now seconds-per-second, as opposed to seconds-per-frame. --- diff --git a/src/Time/TimeManager.cxx b/src/Time/TimeManager.cxx index f3c7d5a3c..6b8040dc1 100644 --- a/src/Time/TimeManager.cxx +++ b/src/Time/TimeManager.cxx @@ -90,7 +90,7 @@ void TimeManager::init() _impl = new SGTime(globals->get_aircraft_position(), zone, _timeOverride->getLongValue()); - _warpDelta->setIntValue(0); + _warpDelta->setDoubleValue(0.0); globals->get_event_mgr()->addTask("updateLocalTime", this, &TimeManager::updateLocalTime, 30*60 ); @@ -112,6 +112,12 @@ void TimeManager::init() _modelHz = fgGetNode("sim/model-hz", true); _timeDelta = fgGetNode("sim/time/delta-realtime-sec", true); _simTimeDelta = fgGetNode("sim/time/delta-sec", true); + + _simTimeFactor = fgGetNode("/sim/speed-up", true); + // use pre-set value but ensure we get a sane default + if (!_simTimeDelta->hasValue()) { + _simTimeFactor->setDoubleValue(1.0); + } } void TimeManager::unbind() @@ -129,6 +135,7 @@ void TimeManager::unbind() _modelHz.clear(); _timeDelta.clear(); _simTimeDelta.clear(); + _simTimeFactor.clear(); } void TimeManager::postinit() @@ -228,7 +235,7 @@ void TimeManager::computeTimeDeltas(double& simDt, double& realDt) simDt = 0; } else { // sim time can be scaled - simDt = dt * fgGetDouble("/sim/speed-up"); + simDt = dt * _simTimeFactor->getDoubleValue(); } _lastStamp = currentStamp; @@ -243,7 +250,7 @@ void TimeManager::update(double dt) { bool freeze = _clockFreeze->getBoolValue(); time_t now = time(NULL); - + if (freeze) { // clock freeze requested if (_timeOverride->getLongValue() == 0) { @@ -264,12 +271,29 @@ void TimeManager::update(double dt) } _timeOverride->setLongValue(0); } - + + // account for speed-up in warp value. This implies when speed-up is not + // 1.0 we need to continually adjust warp, either forwards for speed-up, + // or backwards for a slow-down. Eg for a speed up of 4x, we want to + // incease warp by 3 additional seconds per elapsed real second. + // for a 1/2x factor, we want to decrease warp by half a second per + // elapsed real second. + double speedUp = _simTimeFactor->getDoubleValue() - 1.0; + if (speedUp != 0.0) { + double realDt = _timeDelta->getDoubleValue(); + double speedUpOffset = speedUp * realDt; + _warp->setDoubleValue(_warp->getDoubleValue() + speedUpOffset); + } + } // of sim not frozen + + // scale warp-delta by real-dt, so rate is constant with frame-rate, + // but warping works while paused int warpDelta = _warpDelta->getIntValue(); - if (warpDelta != 0) { - _warp->setIntValue(_warp->getIntValue() + warpDelta); + if (warpDelta) { + _adjustWarpOnUnfreeze = false; + double warpOffset = warpDelta * _timeDelta->getDoubleValue(); + _warp->setDoubleValue(_warp->getDoubleValue() + warpOffset); } - } _lastClockFreeze = freeze; _impl->update(globals->get_aircraft_position(), diff --git a/src/Time/TimeManager.hxx b/src/Time/TimeManager.hxx index 24c2f841f..37731309e 100644 --- a/src/Time/TimeManager.hxx +++ b/src/Time/TimeManager.hxx @@ -76,7 +76,8 @@ private: SGPropertyNode_ptr _timeOverride; SGPropertyNode_ptr _warp; SGPropertyNode_ptr _warpDelta; - + SGPropertyNode_ptr _simTimeFactor; + bool _lastClockFreeze; bool _adjustWarpOnUnfreeze;