vehiclephys.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (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, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWPhys *
  23. * *
  24. * $Archive:: /Commando/Code/wwphys/vehiclephys.h $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Greg_h $*
  29. * *
  30. * $Modtime:: 10/14/01 2:09p $*
  31. * *
  32. * $Revision:: 16 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #ifndef VEHICLEPHYS_H
  38. #define VEHICLEPHYS_H
  39. #include "rbody.h"
  40. #include "simplevec.h"
  41. class VehiclePhysDefClass;
  42. class SuspensionElementClass;
  43. class VehicleDazzleClass;
  44. /**
  45. ** VehiclePhysClass
  46. ** This RigidBody-derived class adds the constants needed to define the suspension behavior
  47. ** for all vehicles. Most of the actual force calculations are implemented in further
  48. ** derived classes but this is the common base class for various classes (e.g. WheeledVehicle,
  49. ** TrackedVehicle, and VTOLPhys
  50. */
  51. class VehiclePhysClass : public RigidBodyClass
  52. {
  53. public:
  54. VehiclePhysClass(void);
  55. virtual ~VehiclePhysClass(void);
  56. virtual VehiclePhysClass * As_VehiclePhysClass(void) { return this; }
  57. const VehiclePhysDefClass * Get_VehiclePhysDef(void) { return (VehiclePhysDefClass*)Definition; }
  58. void Init(const VehiclePhysDefClass & def);
  59. /*
  60. ** DEBUGGING, re-initialize this object because our definition changed
  61. */
  62. virtual void Definition_Changed(void);
  63. /*
  64. ** Rendering
  65. */
  66. virtual void Render(RenderInfoClass & rinfo);
  67. virtual void Set_Model(RenderObjClass * model);
  68. float Compute_Approximate_Ride_Height(void);
  69. /*
  70. ** Simulation
  71. */
  72. virtual void Timestep(float dt);
  73. /*
  74. ** Gentlemen Start your Engines!
  75. */
  76. void Enable_Engine(bool onoff) { IsEngineOn = onoff; }
  77. bool Is_Engine_Enabled(void) { return IsEngineOn; }
  78. /*
  79. ** Wheel management
  80. */
  81. int Get_Wheel_Count(void) { return Wheels.Length(); }
  82. int Get_Drive_Wheel_Count(void) { return DriveWheelCount; }
  83. int Get_Real_Wheel_Count(void) { return RealWheelCount; }
  84. SuspensionElementClass * Peek_Wheel(int wheel_index);
  85. bool Is_In_Contact(void);
  86. void Update_Wheels (void);
  87. /*
  88. ** Non_Physical_Wheel_Update - used by Renegade's "encyclopedia" to graphically
  89. ** update the wheels when there is no physics scene or processing happening.
  90. */
  91. void Non_Physical_Wheel_Update(float suspension_fraction,float rotation);
  92. /*
  93. ** Teleport to the last good position. This is used when vehicles get flipped over or stuck somehow.
  94. */
  95. void Teleport_To_Last_Good_Position(void);
  96. /*
  97. ** Simulation and rendering toggles for all vehicle physics objects
  98. */
  99. virtual bool Is_Simulation_Disabled(void) { return _DisableVehicleSimulation || RigidBodyClass::Is_Simulation_Disabled(); }
  100. virtual bool Is_Rendering_Disabled(void) { return _DisableVehicleRendering || RigidBodyClass::Is_Simulation_Disabled(); }
  101. static void Disable_All_Simulation(bool onoff) { _DisableVehicleSimulation = onoff; }
  102. static void Disable_All_Rendering(bool onoff) { _DisableVehicleRendering = onoff; }
  103. static bool Is_All_Simulation_Disabled(void) { return _DisableVehicleSimulation; }
  104. static bool Is_All_Rendering_Disabled(void) { return _DisableVehicleRendering; }
  105. /*
  106. ** Save-Load system
  107. */
  108. virtual bool Save(ChunkSaveClass &csave);
  109. virtual bool Load(ChunkLoadClass &cload);
  110. protected:
  111. /*
  112. ** Simulation
  113. */
  114. virtual void Compute_Force_And_Torque(Vector3 * force,Vector3 * torque);
  115. virtual bool Can_Go_To_Sleep(float dt);
  116. /*
  117. ** Suspension initialization
  118. */
  119. void Update_Cached_Model_Parameters(void);
  120. void Release_Wheels(void);
  121. void Create_Wheels(void);
  122. int Find_Fork_Bone(RenderObjClass * model,const char * wpname);
  123. int Find_Rotation_Bone(RenderObjClass * model,const char * wpname);
  124. int Find_Translation_Bone(RenderObjClass * model,const char * wpname);
  125. /*
  126. ** Auxilary bones
  127. */
  128. void Release_Auxiliary_Bones(void);
  129. void Capture_Auxiliary_Bones(void);
  130. /*
  131. ** Dazzle controllers
  132. */
  133. void Release_Dazzles(void);
  134. void Capture_Dazzles(void);
  135. VehicleDazzleClass * Create_Dazzle_Controller(RenderObjClass * obj);
  136. /*
  137. ** Virtual Wheel Creation, Derived classes implement this to create
  138. ** the types of wheels that they need to use. (NOTE: wheels should be 'new'ed
  139. ** since VehiclePhysClass will 'delete' them when done.)
  140. */
  141. virtual SuspensionElementClass * Alloc_Suspension_Element(void) = 0;
  142. /*
  143. ** Implement this to support translating engine flames
  144. */
  145. virtual float Get_Normalized_Engine_Flame(void) { return 0.0f; }
  146. /*
  147. ** Variables
  148. */
  149. bool IsEngineOn; // is the engine on? vehicles will stop if the engine is off
  150. int RealWheelCount; // active wheel count
  151. int DriveWheelCount; // drive wheel count
  152. SimpleVecClass<SuspensionElementClass *> Wheels; // array of wheel objects
  153. SimpleVecClass<VehicleDazzleClass *> Dazzles; // array of dazzle controllers (for headlights, taillights, blinky lights, etc)
  154. SimpleVecClass<int> EngineFlameBones; // indices to "engine-flame" bones
  155. Matrix3D LastGoodPosition; // last state where all wheels were on the ground
  156. float ExpireTimer; // time left before blowing up when rolled over
  157. static bool _DisableVehicleSimulation;
  158. static bool _DisableVehicleRendering;
  159. };
  160. /**
  161. ** VehiclePhysDefClass
  162. ** This definition contains a set of constants used by all types of vehicles.
  163. */
  164. class VehiclePhysDefClass : public RigidBodyDefClass
  165. {
  166. public:
  167. VehiclePhysDefClass(void);
  168. virtual ~VehiclePhysDefClass(void);
  169. // From DefinitionClass
  170. virtual uint32 Get_Class_ID (void) const;
  171. // From PhysDefClass
  172. virtual const char * Get_Type_Name(void) { return "VehiclePhysDef"; }
  173. virtual bool Is_Type(const char *);
  174. // Read access to the constants
  175. float Get_Spring_Constant(void) const { return SpringConstant; }
  176. float Get_Damping_Constant(void) const { return DampingConstant; }
  177. float Get_Spring_Length(void) const { return SpringLength; }
  178. float Get_Traction_Multiplier(void) const { return TractionMultiplier; }
  179. float Get_Lateral_Moment_Arm(void) const { return LateralMomentArm; }
  180. float Get_Tractive_Moment_Arm(void) const { return TractiveMomentArm; }
  181. bool Is_Fake(void) const { return IsFake; }
  182. // Write access (DEBUGGING/TESTING ONLY)
  183. void Set_Spring_Constant(float ks) { SpringConstant = ks; }
  184. void Set_Damping_Constant(float kd) { DampingConstant = kd; }
  185. void Set_Spring_Length(float l) { SpringLength = l; }
  186. void Set_Traction_Multiplier(float k) { TractionMultiplier = k; }
  187. void Set_Lateral_Moment_Arm(float r) { LateralMomentArm = r; }
  188. void Set_Tractive_Moment_Arm(float r) { TractiveMomentArm = r; }
  189. // Save/Load support from PersistClass
  190. virtual const PersistFactoryClass & Get_Factory (void) const;
  191. virtual bool Save(ChunkSaveClass &csave);
  192. virtual bool Load(ChunkLoadClass &cload);
  193. // Editable interface requirements
  194. DECLARE_EDITABLE(VehiclePhysDefClass,RigidBodyDefClass);
  195. protected:
  196. float SpringConstant; // suspension spring constant
  197. float DampingConstant; // suspension damping constant (shock absorber)
  198. float SpringLength; // suspension spring length
  199. float TractionMultiplier; // scales the downforce for more traction
  200. float LateralMomentArm; // arbitrary dist from x-y plane to apply lateral tire forces
  201. float TractiveMomentArm; // arbitrary dist from x-y plane to apply tractive tire forces
  202. float EngineFlameLength; // max length of the engine flame (how far to translate at flame=1.0)
  203. bool IsFake; // short-circuit all possible physics calculations!
  204. friend class VehiclePhysClass;
  205. };
  206. #endif