wheeledVehicle.h 8.3 KB

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