From 87cc77ae4c8828b1a23a200955f2ca9a4855ef39 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 6 Dec 2009 10:52:50 +0000 Subject: [PATCH] Tweak train control algorithm --- include/IController.hpp | 1 + maps/goods_demo/goods_demo.bin | Bin 4364 -> 4364 bytes maps/goods_demo/goods_demo.xml | 4 ++++ src/Engine.cpp | 40 +++++++++++++++++++++++++++------ src/Game.cpp | 3 +++ 5 files changed, 41 insertions(+), 7 deletions(-) diff --git a/include/IController.hpp b/include/IController.hpp index d51604d..334103b 100644 --- a/include/IController.hpp +++ b/include/IController.hpp @@ -27,6 +27,7 @@ enum Action { SHOVEL_COAL, THROTTLE_UP, THROTTLE_DOWN, + TOGGLE_REVERSE, GO_STRAIGHT_ON, GO_LEFT, GO_RIGHT, diff --git a/maps/goods_demo/goods_demo.bin b/maps/goods_demo/goods_demo.bin index 502d61b2b70d1797f6ed0ee967f594eb5aa7d61e..7a6792e669603adb3168593635e2fc7c8f31f198 100644 GIT binary patch delta 289 zcmeBC>QUP8g^gp@Oi4Qsp4`ALGg*Q?f#cj6A6pOx3e@nxI1CI7_LDELiA-L=!7*8d z!$QWy*w}v7%$asT1$Jl8oB_Ni%V4uAjKJItIZ=>Sp#G6Q5D z5Kq?Um7J`=*$rstwCnxZ7P2R?3B>=JoWYYit@(v&! ilQ*!)OkTjP0Cb{-AjoQv6)^4RCU4}m+Pt6JmlXh56mACq delta 296 zcmeBC>QUP8g>CWyX7g&a^vw#>Z}Q1xJQ-M%ps_b7y+&Gt$!RXV07o zW`i^V@#NnenbPOaoUu<&V{>3+U~n)q{%jB8gVY1@0oGV z3|0e@H!(H_SvFapS8}ofmyaY!A4tvb|M3q0|I0g=8gI4-Iq2NvjlBAk6 + + + + diff --git a/src/Engine.cpp b/src/Engine.cpp index 65488d4..57da0f9 100644 --- a/src/Engine.cpp +++ b/src/Engine.cpp @@ -91,6 +91,8 @@ private: double statTractiveEffort; bool isBrakeOn; int myThrottle; // Ratio measured in tenths + bool reverse; + bool haveStopped; track::Choice nextChoice; @@ -103,12 +105,15 @@ private: static const double TRACTIVE_EFFORT_KNEE; static const double INIT_PRESSURE, INIT_TEMP; + + static const double STOP_SPEED; }; const float Engine::MODEL_SCALE(0.4f); const double Engine::TRACTIVE_EFFORT_KNEE(10.0); const double Engine::INIT_PRESSURE(0.2); const double Engine::INIT_TEMP(50.0); +const double Engine::STOP_SPEED(0.01); Engine::Engine(IResourcePtr aRes) : mySpeed(0.0), myMass(29.0), @@ -116,6 +121,8 @@ Engine::Engine(IResourcePtr aRes) myFireTemp(INIT_TEMP), statTractiveEffort(34.7), isBrakeOn(true), myThrottle(0), + reverse(false), + haveStopped(true), resource(aRes) { static IXMLParserPtr parser = makeXMLParser("schemas/engine.xsd"); @@ -148,9 +155,9 @@ double Engine::tractiveEffort() const // Calculate the magnitude of the resistance on the train double Engine::resistance() const { - const double a = 4.0; - const double b = 0.05; - const double c = 0.006; + const double a = 0.0; + const double b = 0.09; + const double c = 0.02; return a + b*mySpeed + c*mySpeed*mySpeed; } @@ -159,7 +166,17 @@ double Engine::brakeForce() const { const double beta = 0.09; const double g = 9.78; - return myMass * g * beta; + + // Brake always acts against direction of motion + double dir; + if (abs(mySpeed) < STOP_SPEED) + dir = 0.0; + else if (mySpeed < 0.0) + dir = -1.0; + else + dir = 1.0; + + return myMass * g * beta * dir; } // Compute the next state of the engine @@ -180,12 +197,18 @@ void Engine::update(int aDelta) const double deltaSeconds = aDelta / 1000.0f; const double a = ((netP - Q - B) / myMass) * deltaSeconds; - mySpeed = max(mySpeed + a, 0.0); + // mySpeed = max(mySpeed + a, 0.0); + mySpeed += a; + + if (abs(mySpeed) < STOP_SPEED) { + mySpeed = 0.0; + haveStopped = true; + } - /*debug() << "P=" << netP << ", Q=" << Q + debug() << "P=" << netP << ", Q=" << Q << ", B=" << B << ", a=" << a << ", v=" << mySpeed - << " (delta=" << aDelta << ")";*/ + << " (delta=" << aDelta << ")"; } track::Choice Engine::consumeChoice() @@ -211,6 +234,9 @@ void Engine::actOn(Action anAction) case THROTTLE_DOWN: myThrottle = max(myThrottle - 1, 0); break; + case TOGGLE_REVERSE: + reverse = !reverse; + break; case GO_STRAIGHT_ON: nextChoice = track::CHOOSE_STRAIGHT_ON; break; diff --git a/src/Game.cpp b/src/Game.cpp index b4acbc2..c55afad 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -264,6 +264,9 @@ void Game::onKeyDown(SDLKey aKey) case SDLK_b: train->controller()->actOn(BRAKE_TOGGLE); break; + case SDLK_r: + train->controller()->actOn(TOGGLE_REVERSE); + break; case SDLK_LCTRL: train->controller()->actOn(SHOVEL_COAL); break; -- 2.39.2