Debian Codegraph @

src/include/engine.h

raw
1 //
2 // Copyright (C) 2004-2006 Jasmine Langridge, [email protected]
3 // Copyright (C) 2017 Emanuele Sorce, [email protected]
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 //
19
20 #ifndef ENGINEH
21 #define ENGINEH
22
23 #include <vector>
24 #include "vmath.h"
25
26 // RPM = revolutions per minute
27 // RPS = radians per second
28
29 #define RPM_TO_RPS(x) ((x) * (PI / 30.0f))
30 #define RPS_TO_RPM(x) ((x) * (30.0f / PI))
31
32 ///
33 /// @brief Datas about performances of an engine
34 ///
35 class PEngine {
36
37 // the curve graph of the power of the engine
38 // x = radians per second
39 // y = output power
40 std::vector<vec2f> powercurve;
41
42 // vector of gears
43 // round ratio
44 std::vector<float> gear;
45
46 // standard time to change gear
47 float gearch_first;
48
49 // time to change gear if you change it just after another change
50 float gearch_repeat;
51
52 // engine minimum and maximum radians per second angular speed
53 float minRPS, maxRPS;
54
55 protected:
56 float getPowerAtRPS(float rps);
57
58 public:
59 PEngine():
60 gearch_first(0.4f),
61 gearch_repeat(0.15f),
62 minRPS(10000000.0f),
63 maxRPS(0.0f) { }
64
65 ///
66 /// @brief Add a point to the power curve
67 /// @param rpm = at 'rpm' round per minute
68 /// @param power = you get 'power' output power
69 /// @todo function to allow to give angular velocity directly in radians per second?
70 ///
71 void addPowerCurvePoint(const float& rpm, const float& power);
72
73 ///
74 /// @brief add a gear
75 /// @param ratio = gear ratio
76 ///
77 void addGear(const float& ratio);
78
79 bool hasGears() { return !gear.empty(); }
80 float getLastGearRatio() { return gear.back(); }
81
82 // get engine horse power
83 float getHorsePower();
84
85 friend class PEngineInstance;
86 };
87
88 ///
89 /// @brief current status of the engine
90 ///
91 class PEngineInstance {
92
93 // reference with performance of the engine
94 PEngine *engine;
95
96 // current rps (always positive)
97 float rps;
98
99 // gear currently used
100 int currentgear;
101
102 // target gear relative (1 to go up, -1 to go down, 0 to stay)
103 int targetgear_rel;
104
105 // timing variable for gear changing
106 float gearch;
107
108 // if the car is going reverse (rps will be always positive)
109 bool reverse;
110
111 // current output engine torque
112 float out_torque;
113
114 // if the vehicle has changed gear recently
115 bool flag_gearchange;
116
117 public:
118 PEngineInstance(PEngine *neweng) :
119 engine(neweng),
120 currentgear(0),
121 targetgear_rel(0),
122 gearch(0.0f),
123 reverse(false),
124 out_torque(0.0f),
125 flag_gearchange(false) { }
126
127 // Simulation tick
128 void tick(float delta, float throttle, float wheel_rps);
129
130 // return current information
131 float getOutputTorque()
132 {
133 return out_torque;
134 }
135 float getEngineRPS()
136 {
137 return rps;
138 }
139 float getEngineRPM() {
140 return RPS_TO_RPM(rps);
141 }
142
143 // return current gear (reverse will out -1)
144 int getCurrentGear() { return reverse ? -1 : currentgear; }
145
146 bool getFlagGearChange() {
147 bool ret = flag_gearchange;
148 flag_gearchange = false;
149 return ret;
150 }
151
152 ///
153 /// @brief Reset the engine, used i.e. with the 'recover' key
154 ///
155 void doReset() {
156 rps = engine->minRPS;
157 currentgear = 0;
158 targetgear_rel = 0;
159 gearch = 0.0f;
160 out_torque = 0.0f;
161 }
162 };
163
164 #endif