1 #pragma once 2 #include <iostream> 3 #include <map> 4 #include <fstream> 5 6 #include "dbl.h" 7 #include "rotationalframe.h" 8 #include "matrix3.h" 9 #include "spline.h" 10 #include "linearinterp.h" 11 #include "mathvector.h" 12 #include "../ogre/common/Def_Str.h" 13 14 15 class CARENGINE 16 { 17 private: 18 // constants 19 Dbl rpm_max; ///< the redline in RPMs 20 //Dbl rpm_limit; ///< peak engine RPMs after which limiting occurs 21 Dbl idle; ///< idle throttle percentage; this is calculated algorithmically 22 Dbl start_rpm; ///< initial condition RPM 23 Dbl stall_rpm; ///< RPM at which the engine dies 24 Dbl fuel_consumption; ///< fuel consumed each second (in liters) is the fuel-consumption parameter times RPM times throttle 25 Dbl friction; ///< friction coefficient from the engine; this is calculated algorithmically 26 Dbl frict_coeffB; ///< friction coefficient 27 28 std::map <Dbl, Dbl> torque_map; ///< a set of RPMs that map to torque values 29 Dbl mass; 30 MATHVECTOR<Dbl,3> position; 31 SPLINE <Dbl> torque_curve; 32 33 // variables 34 Dbl throttle_position; 35 Dbl clutch_torque; 36 bool out_of_gas; 37 bool rev_limit_exceeded; 38 ROTATIONALFRAME crankshaft; 39 40 //for info only 41 Dbl friction_torque; 42 Dbl combustion_torque; 43 bool stalled; 44 45 public: 46 //default constructor makes an S2000-like car 47 CARENGINE(); 48 49 Dbl real_pow_tq_mul; // .car params 50 std::string sound_name; 51 52 Dbl GetTorqueCurve(const Dbl cur_throttle, const Dbl cur_rpm) const; 53 54 Dbl GetFrictionTorque(Dbl cur_angvel, Dbl friction_factor, Dbl throttle_position); 55 SetInertia(const Dbl & value)56 void SetInertia(const Dbl& value) 57 { 58 MATRIX3 <Dbl> inertia; 59 inertia.Scale(value); 60 crankshaft.SetInertia(inertia); 61 } 62 GetInertia()63 Dbl GetInertia() const 64 { 65 return crankshaft.GetInertia()[0]; 66 } 67 SetFrictionB(const Dbl & value)68 void SetFrictionB(const Dbl& value) { frict_coeffB = value; } GetFrictionB()69 Dbl GetFrictionB() const { return frict_coeffB; } 70 SetRpmMax(const Dbl & value)71 void SetRpmMax(const Dbl& value) { rpm_max = value; } GetRpmMax()72 Dbl GetRpmMax() const { return rpm_max; } 73 GetIdle()74 Dbl GetIdle() const { return idle; } 75 SetStartRPM(const Dbl & value)76 void SetStartRPM(const Dbl& value) { start_rpm = value; } GetStartRPM()77 Dbl GetStartRPM() const { return start_rpm; } 78 SetStallRPM(const Dbl & value)79 void SetStallRPM(const Dbl& value) { stall_rpm = value; } GetStallRPM()80 Dbl GetStallRPM() const { return stall_rpm; } 81 SetFuelConsumption(const Dbl & value)82 void SetFuelConsumption(const Dbl& value) { fuel_consumption = value; } GetFuelConsumption()83 Dbl GetFuelConsumption() const { return fuel_consumption; } 84 Integrate1(const Dbl dt)85 void Integrate1(const Dbl dt) 86 { 87 crankshaft.Integrate1(dt); 88 } 89 Integrate2(const Dbl dt)90 void Integrate2(const Dbl dt) 91 { 92 //std::cout << "torque: " << crankshaft.GetTorque()[0] << std::endl; 93 //std::cout << "ang vel prev: " << crankshaft.GetAngularVelocity()[0] << std::endl; 94 crankshaft.Integrate2(dt); 95 //std::cout << "ang vel next: " << crankshaft.GetAngularVelocity()[0] << std::endl; 96 } 97 GetRPM()98 const Dbl GetRPM() const 99 { 100 return crankshaft.GetAngularVelocity()[0] * 30.0 / PI_d; 101 } 102 103 ///set the throttle position where 0.0 is no throttle and 1.0 is full throttle SetThrottle(const Dbl & value)104 void SetThrottle(const Dbl& value) { throttle_position = value; } GetThrottle()105 Dbl GetThrottle() const { return throttle_position; } 106 SetInitialConditions()107 void SetInitialConditions() 108 { 109 MATHVECTOR<Dbl,3> v; 110 crankshaft.SetInitialTorque(v); 111 StartEngine(); 112 } 113 StartEngine()114 void StartEngine() 115 { 116 MATHVECTOR<Dbl,3> v; 117 v[0] = start_rpm * PI_d / 30.0; 118 crankshaft.SetAngularVelocity(v); 119 } 120 121 ///used to set the drag on the engine from the clutch being partially engaged SetClutchTorque(Dbl newtorque)122 void SetClutchTorque(Dbl newtorque) { clutch_torque = newtorque; } 123 GetAngularVelocity()124 Dbl GetAngularVelocity() const 125 { 126 return crankshaft.GetAngularVelocity()[0]; 127 } 128 129 ///used to set the engine speed to the transmission speed when the clutch is fully engaged SetAngularVelocity(Dbl angvel)130 void SetAngularVelocity(Dbl angvel) 131 { 132 MATHVECTOR<Dbl,3> v(angvel, 0, 0); 133 crankshaft.SetAngularVelocity(v); 134 } 135 136 void DebugPrint(std::ostream & out); 137 138 ///return the sum of all torques acting on the engine (except clutch forces) GetTorque()139 Dbl GetTorque() 140 { 141 return combustion_torque + friction_torque; 142 } 143 144 void ComputeForces(); 145 146 void ApplyForces(); 147 148 ///Set the torque curve using a vector of (RPM, torque) pairs. 149 /// also recalculate engine friction 150 /// the max_power_rpm value should be set to the engine's redline 151 void SetTorqueCurve(Dbl max_power_rpm, std::vector <std::pair <Dbl, Dbl> > & curve); 152 SetMass(const Dbl & value)153 void SetMass(const Dbl& value) { mass = value; } GetMass()154 Dbl GetMass() const { return mass; } 155 SetPosition(const MATHVECTOR<Dbl,3> & value)156 void SetPosition(const MATHVECTOR<Dbl,3>& value) { position = value; } GetPosition()157 MATHVECTOR<Dbl,3> GetPosition() const { return position; } 158 FuelRate()159 Dbl FuelRate() const 160 { 161 return fuel_consumption * GetAngularVelocity() * throttle_position; 162 } 163 SetOutOfGas(bool value)164 void SetOutOfGas(bool value) { out_of_gas = value; } 165 166 ///returns true if the engine is combusting fuel GetCombustion()167 bool GetCombustion() const 168 { 169 return !stalled; 170 } 171 }; 172