RigsofRods
Soft-body Physics Simulation
CruiseControl.cpp
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2005-2012 Pierre-Michel Ricordel
4  Copyright 2007-2012 Thomas Fischer
5  Copyright 2015-2020 Petr Ohlidal
6 
7  For more information, see http://www.rigsofrods.org/
8 
9  Rigs of Rods is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License version 3, as
11  published by the Free Software Foundation.
12 
13  Rigs of Rods is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "Actor.h"
23 
24 #include "EngineSim.h"
25 #include "InputEngine.h"
26 
27 #include <Ogre.h>
28 
29 using namespace RoR;
30 
32 {
33  cc_mode = !cc_mode;
34 
35  if (cc_mode)
36  {
39  }
40  else
41  {
42  cc_target_speed = 0;
43  cc_target_rpm = 0;
44  cc_accs.clear();
45  }
46 }
47 
49 {
52  (ar_engine->GetGear() > 0 && ar_parking_brake) ||
53  (ar_engine->GetGear() < 0) ||
54  !ar_engine->isRunning() ||
56  {
57  this->cruisecontrolToggle();
58  return;
59  }
60 
62  return;
63 
64  float acc = ar_engine->GetAccToHoldRPM();
65 
66  if (ar_engine->GetGear() > 0)
67  {
68  // Try to maintain the target speed
69  float power_weight_ratio = getTotalMass(true) / ar_engine->getEnginePower();
70  acc += (cc_target_speed - ar_wheel_speed) * power_weight_ratio * 0.25;
71  }
72  else if (ar_engine->GetGear() == 0) // out of gear
73  {
74  // Try to maintain the target rpm
75  float speed_range = (ar_engine->getMaxRPM() - ar_engine->getMinRPM()) / 50.0f;
76  acc += ar_engine->GetEngineInertia() * (cc_target_rpm - ar_engine->GetEngineRpm()) / speed_range;
77  }
78 
79  cc_accs.push_front(Ogre::Math::Clamp(acc, -1.0f, +1.0f));
80  if (cc_accs.size() > 30)
81  {
82  cc_accs.pop_back();
83  }
84 
85  float avg_acc = 0.0f;
86  for (unsigned int i = 0; i < cc_accs.size(); i++)
87  {
88  avg_acc += cc_accs[i];
89  }
90  avg_acc /= cc_accs.size();
91 
92  ar_engine->autoSetAcc(Ogre::Math::Clamp(avg_acc, ar_engine->GetAcceleration(), 1.0f));
93 
95  {
96  if (ar_engine->GetGear() > 0)
97  {
98  cc_target_speed *= pow(2.0f, dt / 5.0f);
100  if (sl_enabled)
101  {
103  }
104  }
105  else if (ar_engine->GetGear() == 0) // out of gear
106  {
107  cc_target_rpm *= pow(2.0f, dt / 5.0f);
109  }
110  }
111  if (App::GetInputEngine()->getEventBoolValue(EV_TRUCK_CRUISE_CONTROL_DECL))
112  {
113  if (ar_engine->GetGear() > 0)
114  {
115  cc_target_speed *= pow(0.5f, dt / 5.0f);
117  }
118  else if (ar_engine->GetGear() == 0) // out of gear
119  {
120  cc_target_rpm *= pow(0.5f, dt / 5.0f);
122  }
123  }
124  if (App::GetInputEngine()->getEventBoolValue(EV_TRUCK_CRUISE_CONTROL_READJUST))
125  {
127  if (sl_enabled)
128  {
130  }
132  }
133 
134  if (cc_can_brake)
135  {
137  {
138  float brake = (ar_avg_wheel_speed - cc_target_speed) * 0.5f;
139  ar_brake = std::min(brake, 1.0f);
140  }
141  }
142 }
RoR::Actor::ar_avg_wheel_speed
float ar_avg_wheel_speed
Physics state; avg wheel speed in m/s.
Definition: Actor.h:391
RoR::Actor::cc_mode
bool cc_mode
Cruise Control.
Definition: Actor.h:353
RoR::EngineSim::getMaxRPM
float getMaxRPM() const
Definition: EngineSim.h:97
RoR::EngineSim::isRunning
bool isRunning() const
Definition: EngineSim.h:93
RoR::Actor::ar_parking_brake
bool ar_parking_brake
Definition: Actor.h:403
RoR::Actor::cc_target_rpm
float cc_target_rpm
Cruise Control.
Definition: Actor.h:355
RoR::Actor::ar_brake
Ogre::Real ar_brake
Physics state; braking intensity.
Definition: Actor.h:388
RoR::Actor::sl_enabled
bool sl_enabled
Speed limiter;.
Definition: Actor.h:359
RoR::EV_TRUCK_CRUISE_CONTROL_READJUST
@ EV_TRUCK_CRUISE_CONTROL_READJUST
match target speed / rpm with current truck speed / rpm
Definition: InputEngine.h:312
RoR::Actor::ar_engine
EngineSim * ar_engine
Definition: Actor.h:368
RoR::EngineSim::getMinRPM
float getMinRPM() const
Definition: EngineSim.h:98
RoR::Actor::sl_speed_limit
float sl_speed_limit
Speed limiter;.
Definition: Actor.h:360
RoR::Actor::cc_target_speed_lower_limit
float cc_target_speed_lower_limit
Cruise Control.
Definition: Actor.h:357
RoR::EngineSim::autoSetAcc
void autoSetAcc(float val)
Definition: EngineSim.cpp:1078
RoR::InputEngine::getEventValue
float getEventValue(int eventID, bool pure=false, InputSourceType valueSource=InputSourceType::IST_ANY)
valueSource: IST_ANY=digital and analog devices, IST_DIGITAL=only digital, IST_ANALOG=only analog
Definition: InputEngine.cpp:911
Actor.h
EngineSim.h
RoR::Actor::ar_wheel_speed
float ar_wheel_speed
Physics state; wheel speed in m/s.
Definition: Actor.h:389
RoR::EngineSim::GetEngineInertia
float GetEngineInertia()
Definition: EngineSim.h:106
RoR::Actor::cc_target_speed
float cc_target_speed
Cruise Control.
Definition: Actor.h:356
RoR::EngineSim::GetEngineRpm
float GetEngineRpm() const
Definition: EngineSim.h:102
RoR::InputEngine::getEventBoolValue
bool getEventBoolValue(int eventID)
Definition: InputEngine.cpp:710
RoR::Actor::cc_accs
std::deque< float > cc_accs
Cruise Control.
Definition: Actor.h:358
RoR::EngineSim::hasContact
bool hasContact() const
Definition: EngineSim.h:127
RoR::Actor::UpdateCruiseControl
void UpdateCruiseControl(float dt)
Defined in 'gameplay/CruiseControl.cpp'.
Definition: CruiseControl.cpp:48
RoR::EngineSim::GetAccToHoldRPM
float GetAccToHoldRPM()
estimate required throttle input to hold the current rpm
Definition: EngineSim.cpp:1072
RoR::Actor::getTotalMass
float getTotalMass(bool withLocked=true)
Definition: Actor.cpp:758
RoR::EngineSim::GetGear
int GetGear()
low level gear changing
Definition: EngineSim.cpp:1041
RoR::Actor::cc_can_brake
bool cc_can_brake
Cruise Control.
Definition: Actor.h:354
RoR::EV_TRUCK_ACCELERATE
@ EV_TRUCK_ACCELERATE
accelerate the truck
Definition: InputEngine.h:297
RoR::EV_TRUCK_MANUAL_CLUTCH
@ EV_TRUCK_MANUAL_CLUTCH
manual clutch (for manual transmission)
Definition: InputEngine.h:326
RoR::App::GetInputEngine
InputEngine * GetInputEngine()
Definition: Application.cpp:270
RoR::EngineSim::GetAcceleration
float GetAcceleration()
Definition: EngineSim.cpp:880
InputEngine.h
Handles controller inputs from player. Defines input events and binding mechanism,...
RoR::EngineSim::getEnginePower
float getEnginePower()
Definition: EngineSim.h:107
RoR::EV_TRUCK_CRUISE_CONTROL_DECL
@ EV_TRUCK_CRUISE_CONTROL_DECL
decrease target speed / rpm
Definition: InputEngine.h:311
RoR::EV_TRUCK_CRUISE_CONTROL_ACCL
@ EV_TRUCK_CRUISE_CONTROL_ACCL
increase target speed / rpm
Definition: InputEngine.h:310
RoR::Actor::cruisecontrolToggle
void cruisecontrolToggle()
Defined in 'gameplay/CruiseControl.cpp'.
Definition: CruiseControl.cpp:31
RoR
Definition: AppContext.h:36
RoR::EV_TRUCK_BRAKE
@ EV_TRUCK_BRAKE
brake
Definition: InputEngine.h:306