wheeledVehicle.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #include "T3D/assets/ShapeAsset.h"
  31. class ParticleEmitter;
  32. class ParticleEmitterData;
  33. //----------------------------------------------------------------------------
  34. struct WheeledVehicleTire: public SimDataBlock, protected AssetPtrCallback
  35. {
  36. typedef SimDataBlock Parent;
  37. DECLARE_SHAPEASSET_REFACTOR(WheeledVehicleTire, Shape)
  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. F32 radius; // Tire radius
  55. //
  56. WheeledVehicleTire();
  57. DECLARE_CONOBJECT(WheeledVehicleTire);
  58. static void initPersistFields();
  59. bool preload(bool, String &errorStr) override;
  60. void packData(BitStream* stream) override;
  61. void unpackData(BitStream* stream) override;
  62. protected:
  63. void onAssetRefreshed(AssetPtrBase* pAssetPtrBase) override
  64. {
  65. reloadOnLocalClient();
  66. }
  67. };
  68. //----------------------------------------------------------------------------
  69. struct WheeledVehicleSpring: public SimDataBlock
  70. {
  71. typedef SimDataBlock Parent;
  72. F32 length; // Travel distance from root hub position
  73. F32 force; // Spring force
  74. F32 damping; // Damping force
  75. F32 antiSway; // Opposite wheel anti-sway
  76. //
  77. WheeledVehicleSpring();
  78. DECLARE_CONOBJECT(WheeledVehicleSpring);
  79. static void initPersistFields();
  80. void packData(BitStream* stream) override;
  81. void unpackData(BitStream* stream) override;
  82. };
  83. //----------------------------------------------------------------------------
  84. struct WheeledVehicleData: public VehicleData
  85. {
  86. typedef VehicleData Parent;
  87. enum Constants
  88. {
  89. MaxWheels = 8,
  90. MaxWheelBits = 3
  91. };
  92. enum Sounds
  93. {
  94. JetSound,
  95. EngineSound,
  96. SquealSound,
  97. WheelImpactSound,
  98. MaxSounds,
  99. };
  100. DECLARE_SOUNDASSET_ARRAY(WheeledVehicleData, WheeledVehicleSounds, Sounds::MaxSounds);
  101. ParticleEmitterData* tireEmitter;
  102. F32 maxWheelSpeed; // Engine torque is scale based on wheel speed
  103. F32 engineTorque; // Engine force controlled through throttle
  104. F32 engineBrake; // Break force applied when throttle is 0
  105. F32 brakeTorque; // Force used when brakeing
  106. // Initialized onAdd
  107. struct Wheel
  108. {
  109. S32 opposite; // Opposite wheel on Y axis (or -1 for none)
  110. Point3F pos; // Root pos of spring
  111. S32 springNode; // Wheel spring/hub node
  112. S32 springSequence; // Suspension animation
  113. F32 springLength; // Suspension animation length
  114. } wheel[MaxWheels];
  115. U32 wheelCount;
  116. ClippedPolyList rigidBody; // Extracted from shape
  117. S32 brakeLightSequence; // Brakes
  118. S32 steeringSequence; // Steering animation
  119. F32 mDownForce;
  120. //
  121. WheeledVehicleData();
  122. DECLARE_CONOBJECT(WheeledVehicleData);
  123. static void initPersistFields();
  124. bool preload(bool, String &errorStr) override;
  125. bool mirrorWheel(Wheel* we);
  126. void packData(BitStream* stream) override;
  127. void unpackData(BitStream* stream) override;
  128. };
  129. //----------------------------------------------------------------------------
  130. class WheeledVehicle: public Vehicle
  131. {
  132. typedef Vehicle Parent;
  133. enum MaskBits
  134. {
  135. WheelMask = Parent::NextFreeMask << 0,
  136. NextFreeMask = Parent::NextFreeMask << 1
  137. };
  138. WheeledVehicleData* mDataBlock;
  139. bool mBraking;
  140. TSThread* mTailLightThread;
  141. SFXSource* mJetSound;
  142. SFXSource* mEngineSound;
  143. SFXSource* mSquealSound;
  144. struct Wheel
  145. {
  146. WheeledVehicleTire *tire;
  147. WheeledVehicleSpring *spring;
  148. WheeledVehicleData::Wheel* data;
  149. F32 extension; // Spring extension (0-1)
  150. F32 avel; // Angular velocity
  151. F32 apos; // Anuglar position (client side only)
  152. F32 Dy,Dx; // Current tire deformation
  153. struct Surface
  154. {
  155. bool contact; // Wheel is touching a surface
  156. Point3F normal; // Surface normal
  157. BaseMatInstance* material; // Surface material
  158. Point3F pos; // Point of contact
  159. SceneObject* object; // Object in contact with
  160. } surface;
  161. TSShapeInstance* shapeInstance;
  162. TSThread* springThread;
  163. F32 steering; // Wheel steering scale
  164. bool powered; // Powered by engine
  165. bool slipping; // Traction on last tick
  166. F32 torqueScale; // Max torque % applied to wheel (0-1)
  167. F32 slip; // Amount of wheel slip (0-1)
  168. SimObjectPtr<ParticleEmitter> emitter;
  169. };
  170. Wheel mWheel[WheeledVehicleData::MaxWheels];
  171. TSThread* mSteeringThread;
  172. //
  173. bool onNewDataBlock( GameBaseData *dptr, bool reload ) override;
  174. void processTick(const Move *move) override;
  175. void updateMove(const Move *move) override;
  176. void updateForces(F32 dt) override;
  177. void extendWheels(bool clientHack = false);
  178. void prepBatchRender( SceneRenderState *state, S32 mountedImageIndex ) override;
  179. // Client sounds & particles
  180. void updateWheelThreads();
  181. void updateWheelParticles(F32 dt);
  182. void updateEngineSound(F32 level);
  183. void updateSquealSound(F32 level);
  184. void updateJetSound();
  185. U32 getCollisionMask() override;
  186. public:
  187. DECLARE_CONOBJECT(WheeledVehicle);
  188. DECLARE_CATEGORY("Actor \t Controllable");
  189. static void initPersistFields();
  190. WheeledVehicle();
  191. ~WheeledVehicle();
  192. bool onAdd() override;
  193. void onRemove() override;
  194. void advanceTime(F32 dt) override;
  195. bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere) override;
  196. S32 getWheelCount();
  197. Wheel *getWheel(U32 index) {return &mWheel[index];}
  198. void setWheelSteering(S32 wheel,F32 steering);
  199. void setWheelPowered(S32 wheel,bool powered);
  200. void setWheelTire(S32 wheel,WheeledVehicleTire*);
  201. void setWheelSpring(S32 wheel,WheeledVehicleSpring*);
  202. void getWheelInstAndTransform( U32 wheel, TSShapeInstance** inst, MatrixF* xfrm ) const;
  203. void writePacketData(GameConnection * conn, BitStream *stream) override;
  204. void readPacketData(GameConnection * conn, BitStream *stream) override;
  205. U32 packUpdate(NetConnection * conn, U32 mask, BitStream *stream) override;
  206. void unpackUpdate(NetConnection * conn, BitStream *stream) override;
  207. };
  208. #endif