wheeledVehicle.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _WHEELEDVEHICLE_H_
  23. #define _WHEELEDVEHICLE_H_
  24. #ifndef _VEHICLE_H_
  25. #include "T3D/vehicles/vehicle.h"
  26. #endif
  27. #ifndef _CLIPPEDPOLYLIST_H_
  28. #include "collision/clippedPolyList.h"
  29. #endif
  30. class ParticleEmitter;
  31. class ParticleEmitterData;
  32. //----------------------------------------------------------------------------
  33. struct WheeledVehicleTire: public SimDataBlock
  34. {
  35. typedef SimDataBlock Parent;
  36. //
  37. StringTableEntry shapeName;// Max shape to render
  38. // Physical properties
  39. F32 mass; // Mass of the whole wheel
  40. F32 kineticFriction; // Tire friction coefficient
  41. F32 staticFriction; // Tire friction coefficient
  42. F32 restitution; // Currently not used
  43. // Tires act as springs and generate lateral and longitudinal
  44. // forces to move the vehicle. These distortion/spring forces
  45. // are what convert wheel angular velocity into forces that
  46. // act on the rigid body.
  47. F32 lateralForce; // Spring force
  48. F32 lateralDamping; // Damping force
  49. F32 lateralRelaxation; // The tire will relax if left alone
  50. F32 longitudinalForce;
  51. F32 longitudinalDamping;
  52. F32 longitudinalRelaxation;
  53. // Shape information initialized in the preload
  54. Resource<TSShape> shape; // The loaded shape
  55. F32 radius; // Tire radius
  56. //
  57. WheeledVehicleTire();
  58. DECLARE_CONOBJECT(WheeledVehicleTire);
  59. static void initPersistFields();
  60. bool preload(bool, String &errorStr);
  61. virtual void packData(BitStream* stream);
  62. virtual void unpackData(BitStream* stream);
  63. };
  64. //----------------------------------------------------------------------------
  65. struct WheeledVehicleSpring: public SimDataBlock
  66. {
  67. typedef SimDataBlock Parent;
  68. F32 length; // Travel distance from root hub position
  69. F32 force; // Spring force
  70. F32 damping; // Damping force
  71. F32 antiSway; // Opposite wheel anti-sway
  72. //
  73. WheeledVehicleSpring();
  74. DECLARE_CONOBJECT(WheeledVehicleSpring);
  75. static void initPersistFields();
  76. virtual void packData(BitStream* stream);
  77. virtual void unpackData(BitStream* stream);
  78. };
  79. //----------------------------------------------------------------------------
  80. struct WheeledVehicleData: public VehicleData
  81. {
  82. typedef VehicleData Parent;
  83. enum Constants
  84. {
  85. MaxWheels = 8,
  86. MaxWheelBits = 3
  87. };
  88. enum Sounds
  89. {
  90. JetSound,
  91. EngineSound,
  92. SquealSound,
  93. WheelImpactSound,
  94. MaxSounds,
  95. };
  96. SFXTrack* sound[MaxSounds];
  97. ParticleEmitterData* tireEmitter;
  98. F32 maxWheelSpeed; // Engine torque is scale based on wheel speed
  99. F32 engineTorque; // Engine force controlled through throttle
  100. F32 engineBrake; // Break force applied when throttle is 0
  101. F32 brakeTorque; // Force used when brakeing
  102. // Initialized onAdd
  103. struct Wheel
  104. {
  105. S32 opposite; // Opposite wheel on Y axis (or -1 for none)
  106. Point3F pos; // Root pos of spring
  107. S32 springNode; // Wheel spring/hub node
  108. S32 springSequence; // Suspension animation
  109. F32 springLength; // Suspension animation length
  110. } wheel[MaxWheels];
  111. U32 wheelCount;
  112. ClippedPolyList rigidBody; // Extracted from shape
  113. S32 brakeLightSequence; // Brakes
  114. S32 steeringSequence; // Steering animation
  115. //
  116. WheeledVehicleData();
  117. DECLARE_CONOBJECT(WheeledVehicleData);
  118. static void initPersistFields();
  119. bool preload(bool, String &errorStr);
  120. bool mirrorWheel(Wheel* we);
  121. virtual void packData(BitStream* stream);
  122. virtual void unpackData(BitStream* stream);
  123. };
  124. //----------------------------------------------------------------------------
  125. class WheeledVehicle: public Vehicle
  126. {
  127. typedef Vehicle Parent;
  128. enum MaskBits
  129. {
  130. WheelMask = Parent::NextFreeMask << 0,
  131. NextFreeMask = Parent::NextFreeMask << 1
  132. };
  133. WheeledVehicleData* mDataBlock;
  134. bool mBraking;
  135. TSThread* mTailLightThread;
  136. SFXSource* mJetSound;
  137. SFXSource* mEngineSound;
  138. SFXSource* mSquealSound;
  139. struct Wheel
  140. {
  141. WheeledVehicleTire *tire;
  142. WheeledVehicleSpring *spring;
  143. WheeledVehicleData::Wheel* data;
  144. F32 extension; // Spring extension (0-1)
  145. F32 avel; // Angular velocity
  146. F32 apos; // Anuglar position (client side only)
  147. F32 Dy,Dx; // Current tire deformation
  148. struct Surface
  149. {
  150. bool contact; // Wheel is touching a surface
  151. Point3F normal; // Surface normal
  152. BaseMatInstance* material; // Surface material
  153. Point3F pos; // Point of contact
  154. SceneObject* object; // Object in contact with
  155. } surface;
  156. TSShapeInstance* shapeInstance;
  157. TSThread* springThread;
  158. F32 steering; // Wheel steering scale
  159. bool powered; // Powered by engine
  160. bool slipping; // Traction on last tick
  161. F32 torqueScale; // Max torque % applied to wheel (0-1)
  162. F32 slip; // Amount of wheel slip (0-1)
  163. SimObjectPtr<ParticleEmitter> emitter;
  164. };
  165. Wheel mWheel[WheeledVehicleData::MaxWheels];
  166. TSThread* mSteeringThread;
  167. //
  168. bool onNewDataBlock( GameBaseData *dptr, bool reload );
  169. void processTick(const Move *move);
  170. void updateMove(const Move *move);
  171. void updateForces(F32 dt);
  172. void extendWheels(bool clientHack = false);
  173. void prepBatchRender( SceneRenderState *state, S32 mountedImageIndex );
  174. // Client sounds & particles
  175. void updateWheelThreads();
  176. void updateWheelParticles(F32 dt);
  177. void updateEngineSound(F32 level);
  178. void updateSquealSound(F32 level);
  179. void updateJetSound();
  180. virtual U32 getCollisionMask();
  181. public:
  182. DECLARE_CONOBJECT(WheeledVehicle);
  183. static void initPersistFields();
  184. WheeledVehicle();
  185. ~WheeledVehicle();
  186. bool onAdd();
  187. void onRemove();
  188. void advanceTime(F32 dt);
  189. bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere);
  190. S32 getWheelCount();
  191. Wheel *getWheel(U32 index) {return &mWheel[index];}
  192. void setWheelSteering(S32 wheel,F32 steering);
  193. void setWheelPowered(S32 wheel,bool powered);
  194. void setWheelTire(S32 wheel,WheeledVehicleTire*);
  195. void setWheelSpring(S32 wheel,WheeledVehicleSpring*);
  196. void getWheelInstAndTransform( U32 wheel, TSShapeInstance** inst, MatrixF* xfrm ) const;
  197. void writePacketData(GameConnection * conn, BitStream *stream);
  198. void readPacketData(GameConnection * conn, BitStream *stream);
  199. U32 packUpdate(NetConnection * conn, U32 mask, BitStream *stream);
  200. void unpackUpdate(NetConnection * conn, BitStream *stream);
  201. };
  202. #endif