Rigs of Rods 2023.09
Soft-body Physics Simulation
Loading...
Searching...
No Matches
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 "Engine.h"
25#include "InputEngine.h"
26
27#include <Ogre.h>
28
29using namespace RoR;
30
32{
34
35 if (cc_mode)
36 {
39 }
40 else
41 {
43 cc_target_rpm = 0;
44 cc_accs.clear();
45 }
46}
47
49{
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->getShiftUpRPM() - ar_engine->getShiftDownRPM()) / 50.0f;
76 acc += ar_engine->getEngineInertia() * (cc_target_rpm - ar_engine->getRPM()) / 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->getAcc(), 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 }
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}
Handles controller inputs from player.
bool ar_parking_brake
Definition Actor.h:467
EnginePtr ar_engine
Definition Actor.h:432
float ar_wheel_speed
Physics state; wheel speed in m/s.
Definition Actor.h:453
std::deque< float > cc_accs
Cruise Control.
Definition Actor.h:422
bool sl_enabled
Speed limiter;.
Definition Actor.h:423
float cc_target_speed_lower_limit
Cruise Control.
Definition Actor.h:421
float ar_avg_wheel_speed
Physics state; avg wheel speed in m/s.
Definition Actor.h:455
Ogre::Real ar_brake
Physics state; braking intensity.
Definition Actor.h:452
float cc_target_rpm
Cruise Control.
Definition Actor.h:419
bool cc_mode
Cruise Control.
Definition Actor.h:417
float getTotalMass(bool withLocked=true)
Definition Actor.cpp:843
void UpdateCruiseControl(float dt)
Defined in 'gameplay/CruiseControl.cpp'.
float cc_target_speed
Cruise Control.
Definition Actor.h:420
float sl_speed_limit
Speed limiter;.
Definition Actor.h:424
bool cc_can_brake
Cruise Control.
Definition Actor.h:418
void cruisecontrolToggle()
Defined in 'gameplay/CruiseControl.cpp'.
float getRPM()
Definition Engine.h:94
float getEngineInertia() const
('engoption' attr #1)
Definition Engine.h:68
float getAccToHoldRPM()
estimate required throttle input to hold the current rpm
Definition Engine.cpp:1069
float getEnginePower()
Definition Engine.h:106
float getShiftDownRPM() const
Shift down RPM ('engine' attr #1)
Definition Engine.h:57
float getShiftUpRPM() const
Shift up RPM ('engine' attr #2)
Definition Engine.h:58
void autoSetAcc(float val)
Definition Engine.cpp:1075
int getGear()
Definition Engine.cpp:1038
float getAcc()
Definition Engine.cpp:880
bool isRunning()
Definition Engine.h:101
bool hasContact()
Ignition.
Definition Engine.h:102
bool getEventBoolValue(int eventID)
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
@ EV_TRUCK_ACCELERATE
accelerate the truck
@ EV_TRUCK_MANUAL_CLUTCH
manual clutch (for manual transmission)
@ EV_TRUCK_CRUISE_CONTROL_READJUST
match target speed / rpm with current truck speed / rpm
@ EV_TRUCK_CRUISE_CONTROL_DECL
decrease target speed / rpm
@ EV_TRUCK_BRAKE
brake
@ EV_TRUCK_CRUISE_CONTROL_ACCL
increase target speed / rpm
InputEngine * GetInputEngine()