1 #pragma once 2 #include "dbl.h" 3 #include "mathvector.h" 4 #include "quaternion.h" 5 #include "rigidbody.h" 6 7 #include "carengine.h" 8 #include "carclutch.h" 9 #include "cartransmission.h" 10 #include "carfueltank.h" 11 #include "cardifferential.h" 12 #include "caraero.h" 13 14 #include "carsuspension.h" 15 #include "carwheel.h" 16 #include "cartire.h" 17 #include "carbrake.h" 18 19 #include "cardefs.h" 20 #include "collision_contact.h" 21 #include "../btOgre/BtOgreDebug.h" 22 #include "btBulletCollisionCommon.h" 23 #include "btBulletDynamicsCommon.h" 24 25 class MODEL; class CONFIGFILE; class COLLISION_WORLD; class FluidBox; class GAME; 26 27 28 class CARDYNAMICS : public btActionInterface 29 { 30 public: 31 32 class SETTINGS* pSet; 33 class Scene* pScene; // for fluids 34 class FluidsXml* pFluids; // to get fluid params 35 std::vector<float> inputsCopy; // just for dbg info txt 36 37 int numWheels; // copy from CAR 38 void SetNumWheels(int n); 39 40 CARDYNAMICS(); 41 ~CARDYNAMICS(); 42 43 GAME* pGame; 44 bool Load(GAME* game, CONFIGFILE & c); 45 46 void Init( 47 class SETTINGS* pSet1, class Scene* pScene1, class FluidsXml* pFluids1, 48 COLLISION_WORLD & world, 49 const MATHVECTOR<Dbl,3> & position, 50 const QUATERNION<Dbl> & orientation); 51 void RemoveBlt(); 52 53 // bullet interface 54 virtual void updateAction(btCollisionWorld* collisionWorld, btScalar dt); debugDraw(btIDebugDraw * debugDrawer)55 virtual void debugDraw(btIDebugDraw* debugDrawer) { } 56 57 // graphics interface, interpolated 58 void Update(), UpdateBuoyancy(); // update interpolated chassis state GetCenterOfMassPosition()59 const MATHVECTOR<Dbl,3> & GetCenterOfMassPosition() const { return chassisCenterOfMass; } GetPosition()60 const MATHVECTOR<Dbl,3> & GetPosition() const { return chassisPosition; } GetOrientation()61 const QUATERNION<Dbl> & GetOrientation() const { return chassisRotation; } 62 63 MATHVECTOR<Dbl,3> GetWheelPosition(WHEEL_POSITION wp) const; 64 MATHVECTOR<Dbl,3> GetWheelPosition(WHEEL_POSITION wp, Dbl displacement_percent) const; // for debugging 65 QUATERNION<Dbl> GetWheelOrientation(WHEEL_POSITION wp) const; 66 67 QUATERNION<Dbl> GetUprightOrientation(WHEEL_POSITION wp) const; GetWheelVelocity(WHEEL_POSITION wp)68 MATHVECTOR<Dbl,3> GetWheelVelocity(WHEEL_POSITION wp) const { return wheel_velocity[wp]; } // worldspace wheel center position 69 70 // collision world interface GetWheelContact(WHEEL_POSITION wp)71 const COLLISION_CONTACT & GetWheelContact(WHEEL_POSITION wp) const { return wheel_contact[wp]; } GetWheelContact(WHEEL_POSITION wp)72 COLLISION_CONTACT & GetWheelContact(WHEEL_POSITION wp) { return wheel_contact[wp]; } 73 74 /// set from terrain blendmap 75 std::vector<int> iWhOnRoad, whTerMtr, whRoadMtr; 76 77 // chassis 78 float GetMass() const; 79 Dbl GetSpeed() const; Dbl GetSpeedDir() const; 80 MATHVECTOR<Dbl,3> GetVelocity() const; 81 MATHVECTOR<Dbl,3> GetAngularVelocity() const; 82 MATHVECTOR<Dbl,3> GetEnginePosition() const; 83 84 /// custom collision params 85 float coll_R, coll_R2m, coll_W, coll_H, 86 coll_Hofs, coll_Wofs, coll_Lofs, 87 coll_flTrig_H, com_ofs_H, com_ofs_L; 88 float coll_posLfront, coll_posLback, coll_friction; 89 float coll_FrWmul, coll_FrHmul, coll_TopWmul, 90 coll_TopFr, coll_TopMid, coll_TopBack, 91 coll_TopFrHm, coll_TopMidHm, coll_TopBackHm; 92 93 // damage 94 // changed in CAR::UpdateSounds from bullet hit info 95 float fDamage; // 0-100 % 96 97 // driveline 98 // driveline input 99 void StartEngine(); void ShiftGear(int value); float GetThrottle() const; 100 void SetThrottle(float value); void SetClutch(float value); 101 void SetBrake(float value); void SetHandBrake(float value); 102 void SetAutoClutch(bool value); void SetAutoShift(bool value); void SetAutoRear(bool value); 103 104 // speedometer/tachometer based on driveshaft rpm 105 Dbl GetSpeedMPS() const; Dbl GetTachoRPM() const; 106 107 // driveline state access GetEngine()108 const CARENGINE & GetEngine() const { return engine; } GetClutch()109 const CARCLUTCH & GetClutch() const { return clutch; } GetTransmission()110 const CARTRANSMISSION & GetTransmission() const { return transmission; } GetBrake(WHEEL_POSITION pos)111 const CARBRAKE & GetBrake(WHEEL_POSITION pos) const { return brake[pos]; } GetWheel(WHEEL_POSITION pos)112 const CARWHEEL & GetWheel(WHEEL_POSITION pos) const { return wheel[pos]; } 113 114 // traction control 115 void SetABS(const bool newabs); bool GetABSEnabled() const; bool GetABSActive() const; 116 void SetTCS(const bool newtcs); bool GetTCSEnabled() const; bool GetTCSActive() const; 117 118 // cardynamics 119 void SetPosition(const MATHVECTOR<Dbl,3> & pos); 120 121 // move the car along z-axis until it is touching the ground, false on error 122 void AlignWithGround(); 123 124 // set the steering angle to "value", where 1.0 is maximum right lock and -1.0 is maximum left lock. 125 void SetSteering(const Dbl value, const float range_mul); 126 Dbl steerValue; // copy from SetSteering GetSteering()127 Dbl GetSteering() const { return steerValue; } 128 129 // get the maximum steering angle in degrees GetMaxSteeringAngle()130 Dbl GetMaxSteeringAngle() const { return maxangle; } 131 GetTire(WHEEL_POSITION pos)132 /*const*/ CARTIRE* GetTire(WHEEL_POSITION pos) const { return wheel_contact[pos].GetSurface().tire; } GetSuspension(WHEEL_POSITION pos)133 const CARSUSPENSION & GetSuspension(WHEEL_POSITION pos) const { return suspension[pos]; } 134 135 MATHVECTOR<Dbl,3> GetTotalAero() const; 136 137 Dbl GetAerodynamicDownforceCoefficient() const; 138 Dbl GetAeordynamicDragCoefficient() const; 139 GetFeedback()140 Dbl GetFeedback() const { return feedback; } 141 142 // print debug info to the given ostream. set p1, p2, etc if debug info part 1, and/or part 2, etc is desired 143 void DebugPrint(std::ostream & out, bool p1, bool p2, bool p3, bool p4); 144 // common tool for reading .car tags 145 static void GetWPosStr(int axle, int numWheels, WHEEL_POSITION& wl, WHEEL_POSITION& wr, std::string& pos); 146 147 public: 148 /// camera bounce 149 LINEARFRAME cam_body; 150 MATHVECTOR<Dbl,3> cam_force; 151 152 /// buoyancy 153 std::vector<float> whH; // wheel submerge 0..1 154 std::vector<int> whP; // fluid particles id 155 std::vector<float> whDmg; // damage from fluid 156 struct Polyhedron* poly; 157 float body_mass; btVector3 body_inertia; 158 159 // interpolated chassis state 160 MATHVECTOR<Dbl,3> chassisPosition, chassisCenterOfMass; 161 QUATERNION<Dbl> chassisRotation; 162 163 // manual flip over, rocket boost 164 float doFlip, doBoost, boostFuel,boostFuelStart, boostVal, fBoostFov; 165 166 std::list<FluidBox*> inFluids; /// list of fluids this car is in (if any) 167 std::vector<std::list<FluidBox*> > inFluidsWh; 168 169 Ogre::Vector3 vHitPos,vHitNorm; // world hit data 170 Ogre::Vector3 vHitCarN,vHitDmgN; float fHitDmgA; // damage factors 171 float fHitTime, fParIntens,fParVel, fHitForce, 172 fHitForce2,fHitForce3, //dbg info only 173 fCarScrap,fCarScreech; 174 btVector3 velPrev; 175 Dbl time; // for wind only 176 177 /// other vehicles * * * 178 VehicleType vtype; 179 180 float sphereYaw; // dir 181 float hov_throttle, hov_roll; 182 183 void SimulateSpaceship(Dbl dt), SimulateSphere(Dbl dt); 184 std::string sHov; 185 186 /// -- spaceship sim params (from .car) 187 struct HoverPar 188 { 189 float hAbove, hRayLen; 190 float steerForce, steerDamp, steerDampP; 191 float engineForce, engineVelDec,engineVelDecR, brakeForce; 192 float dampAirRes, dampSide, dampUp, dampDn, dampPmul; 193 MATHVECTOR <float,3> alt, alp; // align torque, pipe 194 float pitchTq, rollTq, roll; 195 float hov_vz, hov_vsat, hov_dsat; // hover 196 float hov_dampP, hov_damp; 197 float hov_fall, hov_riseP, hov_rise; 198 void Default(); 199 } hov; 200 201 // bullet to delete ----------------- 202 btAlignedObjectArray<btCollisionShape*> shapes; 203 btAlignedObjectArray<btActionInterface*> actions; 204 btAlignedObjectArray<btTypedConstraint*> constraints; 205 btAlignedObjectArray<btRigidBody*> rigids; 206 207 // chassis state ----------------- 208 RIGIDBODY body; 209 MATHVECTOR<Dbl,3> center_of_mass; 210 COLLISION_WORLD* world; 211 btRigidBody *chassis, *whTrigs; 212 213 // driveline state ----------------- 214 CARFUELTANK fuel_tank; 215 CARENGINE engine; 216 CARCLUTCH clutch; 217 CARTRANSMISSION transmission; 218 CARDIFFERENTIAL diff_front, diff_rear, diff_center; 219 std::vector <CARBRAKE> brake; 220 std::vector <CARWHEEL> wheel; 221 222 enum { FWD = 3, RWD = 12, AWD = 15 } drive; 223 Dbl driveshaft_rpm, tacho_rpm; float engine_vol_mul; 224 225 bool autoclutch, autoshift, autorear, shifted; 226 int shift_gear; 227 Dbl last_auto_clutch, rem_shift_time, shift_time; //remaining 228 229 // traction control state 230 bool abs, tcs; 231 std::vector <int> abs_active, tcs_active; 232 233 // cardynamics state ----------------- 234 std::vector <MATHVECTOR<Dbl,3> > wheel_velocity, wheel_position; 235 std::vector <QUATERNION<Dbl> > wheel_orientation; 236 std::vector <COLLISION_CONTACT> wheel_contact; 237 238 std::vector <CARSUSPENSION> suspension; 239 std::vector <CARAERO> aerodynamics; 240 241 std::list <std::pair <Dbl, MATHVECTOR<Dbl,3> > > mass_only_particles; 242 243 Dbl feedback, maxangle, flip_mul; 244 Dbl ang_damp; Dbl rot_coef[4]; /// new 245 246 // chassis, cardynamics 247 MATHVECTOR<Dbl,3> GetDownVector() const; 248 249 // wrappers (to be removed) 250 QUATERNION<Dbl> Orientation() const; 251 MATHVECTOR<Dbl,3> Position() const; 252 253 MATHVECTOR<Dbl,3> LocalToWorld(const MATHVECTOR<Dbl,3> & local) const; 254 MATHVECTOR<Dbl,3> GetLocalWheelPosition(WHEEL_POSITION wp, Dbl displacement_percent) const; 255 256 QUATERNION<Dbl> GetWheelSteeringAndSuspensionOrientation(WHEEL_POSITION wp) const; 257 MATHVECTOR<Dbl,3> GetWheelPositionAtDisplacement(WHEEL_POSITION wp, Dbl displacement_percent) const; 258 259 void ApplyForce(const MATHVECTOR<Dbl,3> & force); 260 void ApplyForce(const MATHVECTOR<Dbl,3> & force, const MATHVECTOR<Dbl,3> & offset); 261 void ApplyTorque(const MATHVECTOR<Dbl,3> & torque); 262 263 void UpdateWheelVelocity(); 264 void UpdateWheelTransform(); 265 266 void ApplyEngineTorqueToBody(); // apply engine torque to chassis 267 void ApplyAerodynamicsToBody(Dbl dt); // apply aerodynamic forces / torques to chassis 268 269 MATHVECTOR<Dbl,3> UpdateSuspension(int i, Dbl dt); // update suspension diplacement, return suspension force 270 271 // apply tire friction to body, return friction in world space 272 MATHVECTOR<Dbl,3> ApplyTireForce(int i, const Dbl normal_force, const QUATERNION<Dbl> & wheel_space); 273 // apply wheel torque to chassis 274 void ApplyWheelTorque(Dbl dt, Dbl drive_torque, int i, MATHVECTOR<Dbl,3> tire_friction, const QUATERNION<Dbl> & wheel_space); 275 276 void Tick(Dbl dt); /// update simulation 277 void UpdateBody(Dbl dt, Dbl drive_torque[]); // advance chassis(body, suspension, wheels) simulation by dt 278 279 void UpdateMass(); Dbl fBncMass; 280 void SynchronizeBody(); 281 void SynchronizeChassis(); 282 283 void UpdateWheelContacts(); 284 void InterpolateWheelContacts(Dbl dt); 285 286 // driveline 287 void UpdateDriveline(Dbl dt, Dbl drive_torque[]); // update engine, return wheel drive torque 288 void ApplyClutchTorque(Dbl engine_drag, Dbl clutch_speed); // apply clutch torque to engine 289 290 void CalculateDriveTorque(Dbl wheel_drive_torque[], Dbl clutch_torque); // calculate wheel drive torque 291 Dbl CalculateDriveshaftSpeed(); // calculate driveshaft speed given wheel angular velocity 292 void UpdateTransmission(Dbl dt); // calculate throttle, clutch, gear 293 Dbl CalculateDriveshaftRPM() const; // calculate clutch driveshaft rpm 294 295 Dbl AutoClutch(Dbl last_clutch, Dbl dt) const; 296 Dbl ShiftAutoClutch() const; 297 Dbl ShiftAutoClutchThrottle(Dbl throttle, Dbl dt); 298 299 int NextGear() const; // calculate next gear based on engine rpm 300 Dbl DownshiftRPM(int gear, float avg_whH=0.f) const; // calculate downshift point based on gear, engine rpm 301 302 // traction control 303 void DoTCS(int i, Dbl normal_force); // do traction control system calculations and modify the throttle position if necessary 304 void DoABS(int i, Dbl normal_force); // do anti-lock brake system calculations and modify the brake force if necessary 305 306 // cardynamics initialization 307 //Set the maximum steering angle in degrees SetMaxSteeringAngle(Dbl newangle)308 void SetMaxSteeringAngle(Dbl newangle) { maxangle = newangle; } SetAngDamp(Dbl newang)309 void SetAngDamp(Dbl newang) { ang_damp = newang; } 310 311 void SetDrive(const std::string & newdrive); WheelDriven(int i)312 bool WheelDriven(int i) const { return (1 << i) & drive; } 313 314 void AddMassParticle(Dbl newmass, MATHVECTOR<Dbl,3> newpos); 315 316 void AddAerodynamicDevice( const MATHVECTOR<Dbl,3> & newpos, 317 Dbl drag_frontal_area, Dbl drag_coefficient, 318 Dbl lift_surface_area, Dbl lift_coefficient, Dbl lift_efficiency); 319 320 char IsBraking() const; 321 }; 322