From 4b1ffbf2b816652a1792676af4ff5f3bf0d6160f Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 27 Feb 2010 13:51:57 +0000 Subject: [PATCH] Fix speed issue when reversing --- src/Engine.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Engine.cpp b/src/Engine.cpp index 3b520b3..3c76ac7 100644 --- a/src/Engine.cpp +++ b/src/Engine.cpp @@ -157,10 +157,15 @@ double Engine::tractiveEffort() const // Calculate the magnitude of the resistance on the train double Engine::resistance() const { + const double sign = mySpeed < 0.0 ? -1.0 : 1.0; + const double a = 0.0; const double b = 0.09; const double c = 0.02; - return a + b*mySpeed + c*mySpeed*mySpeed; + + const double absSpeed = abs(mySpeed); + + return sign * (a + b*absSpeed + c*absSpeed*absSpeed); } // Calculate the magnitude of the braking force @@ -209,10 +214,10 @@ void Engine::update(int aDelta) else haveStopped = false; - //debug() << "P=" << netP << ", Q=" << Q - // << ", B=" << B - // << ", a=" << a << ", v=" << mySpeed - // << " (delta=" << aDelta << ")"; + // debug() << "P=" << netP << ", Q=" << Q + // << ", B=" << B + // << ", a=" << a << ", v=" << mySpeed + // << " (delta=" << aDelta << ")"; } // User interface to the engine -- 2.39.2