vehicle.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 _VEHICLE_H_
  23. #define _VEHICLE_H_
  24. #ifndef _SHAPEBASE_H_
  25. #include "T3D/shapeBase.h"
  26. #endif
  27. #ifndef _RIGID_H_
  28. #include "T3D/rigid.h"
  29. #endif
  30. #ifndef _BOXCONVEX_H_
  31. #include "collision/boxConvex.h"
  32. #endif
  33. class ParticleEmitter;
  34. class ParticleEmitterData;
  35. class ClippedPolyList;
  36. struct RenderInst;
  37. class Vehicle;
  38. //----------------------------------------------------------------------------
  39. struct VehicleData: public ShapeBaseData
  40. {
  41. typedef ShapeBaseData Parent;
  42. struct Body {
  43. enum Sounds {
  44. SoftImpactSound,
  45. HardImpactSound,
  46. MaxSounds,
  47. };
  48. SFXProfile* sound[MaxSounds];
  49. F32 restitution;
  50. F32 friction;
  51. } body;
  52. enum VehicleConsts
  53. {
  54. VC_NUM_DUST_EMITTERS = 1,
  55. VC_NUM_DAMAGE_EMITTER_AREAS = 2,
  56. VC_NUM_DAMAGE_LEVELS = 2,
  57. VC_NUM_BUBBLE_EMITTERS = 1,
  58. VC_NUM_DAMAGE_EMITTERS = VC_NUM_DAMAGE_LEVELS + VC_NUM_BUBBLE_EMITTERS,
  59. VC_NUM_SPLASH_EMITTERS = 2,
  60. VC_BUBBLE_EMITTER = VC_NUM_DAMAGE_EMITTERS - VC_NUM_BUBBLE_EMITTERS,
  61. };
  62. enum Sounds {
  63. ExitWater,
  64. ImpactSoft,
  65. ImpactMedium,
  66. ImpactHard,
  67. Wake,
  68. MaxSounds
  69. };
  70. SFXProfile* waterSound[MaxSounds];
  71. F32 exitSplashSoundVel;
  72. F32 softSplashSoundVel;
  73. F32 medSplashSoundVel;
  74. F32 hardSplashSoundVel;
  75. F32 minImpactSpeed;
  76. F32 softImpactSpeed;
  77. F32 hardImpactSpeed;
  78. F32 minRollSpeed;
  79. F32 maxSteeringAngle;
  80. F32 collDamageThresholdVel;
  81. F32 collDamageMultiplier;
  82. bool cameraRoll; ///< Roll the 3rd party camera
  83. F32 cameraLag; ///< Amount of camera lag (lag += car velocity * lag)
  84. F32 cameraDecay; ///< Rate at which camera returns to target pos.
  85. F32 cameraOffset; ///< Vertical offset
  86. F32 minDrag;
  87. F32 maxDrag;
  88. S32 integration; ///< # of physics steps per tick
  89. F32 collisionTol; ///< Collision distance tolerance
  90. F32 contactTol; ///< Contact velocity tolerance
  91. Point3F massCenter; ///< Center of mass for rigid body
  92. Point3F massBox; ///< Size of inertial box
  93. F32 jetForce;
  94. F32 jetEnergyDrain; ///< Energy drain/tick
  95. F32 minJetEnergy;
  96. F32 steeringReturn;
  97. F32 steeringReturnSpeedScale;
  98. bool powerSteering;
  99. ParticleEmitterData * dustEmitter;
  100. S32 dustID;
  101. F32 triggerDustHeight; ///< height vehicle has to be under to kick up dust
  102. F32 dustHeight; ///< dust height above ground
  103. ParticleEmitterData * damageEmitterList[ VC_NUM_DAMAGE_EMITTERS ];
  104. Point3F damageEmitterOffset[ VC_NUM_DAMAGE_EMITTER_AREAS ];
  105. S32 damageEmitterIDList[ VC_NUM_DAMAGE_EMITTERS ];
  106. F32 damageLevelTolerance[ VC_NUM_DAMAGE_LEVELS ];
  107. F32 numDmgEmitterAreas;
  108. ParticleEmitterData* splashEmitterList[VC_NUM_SPLASH_EMITTERS];
  109. S32 splashEmitterIDList[VC_NUM_SPLASH_EMITTERS];
  110. F32 splashFreqMod;
  111. F32 splashVelEpsilon;
  112. //
  113. VehicleData();
  114. bool preload(bool server, String &errorStr);
  115. static void initPersistFields();
  116. virtual void packData(BitStream* stream);
  117. virtual void unpackData(BitStream* stream);
  118. DECLARE_CONOBJECT(VehicleData);
  119. DECLARE_CALLBACK( void, onEnterLiquid, ( Vehicle* obj, F32 coverage, const char* type ) );
  120. DECLARE_CALLBACK( void, onLeaveLiquid, ( Vehicle* obj, const char* type ) );
  121. };
  122. //----------------------------------------------------------------------------
  123. class Vehicle: public ShapeBase
  124. {
  125. typedef ShapeBase Parent;
  126. protected:
  127. enum CollisionFaceFlags {
  128. BodyCollision = 0x1,
  129. WheelCollision = 0x2,
  130. };
  131. enum MaskBits {
  132. PositionMask = Parent::NextFreeMask << 0,
  133. EnergyMask = Parent::NextFreeMask << 1,
  134. NextFreeMask = Parent::NextFreeMask << 2
  135. };
  136. struct StateDelta {
  137. Move move; ///< Last move from server
  138. F32 dt; ///< Last interpolation time
  139. // Interpolation data
  140. Point3F pos;
  141. Point3F posVec;
  142. QuatF rot[2];
  143. // Warp data
  144. S32 warpTicks; ///< Number of ticks to warp
  145. S32 warpCount; ///< Current pos in warp
  146. Point3F warpOffset;
  147. QuatF warpRot[2];
  148. //
  149. Point3F cameraOffset;
  150. Point3F cameraVec;
  151. Point3F cameraRot;
  152. Point3F cameraRotVec;
  153. };
  154. StateDelta mDelta;
  155. S32 mPredictionCount; ///< Number of ticks to predict
  156. VehicleData* mDataBlock;
  157. bool inLiquid;
  158. SFXSource* mWakeSound;
  159. Point3F mCameraOffset; ///< 3rd person camera
  160. // Control
  161. Point2F mSteering;
  162. F32 mThrottle;
  163. bool mJetting;
  164. // Rigid Body
  165. bool mDisableMove;
  166. GFXStateBlockRef mSolidSB;
  167. Box3F mWorkingQueryBox;
  168. S32 mWorkingQueryBoxCountDown;
  169. CollisionList mCollisionList;
  170. CollisionList mContacts;
  171. Rigid mRigid;
  172. ShapeBaseConvex mConvex;
  173. S32 restCount;
  174. SimObjectPtr<ParticleEmitter> mDustEmitterList[VehicleData::VC_NUM_DUST_EMITTERS];
  175. SimObjectPtr<ParticleEmitter> mDamageEmitterList[VehicleData::VC_NUM_DAMAGE_EMITTERS];
  176. SimObjectPtr<ParticleEmitter> mSplashEmitterList[VehicleData::VC_NUM_SPLASH_EMITTERS];
  177. //
  178. virtual bool onNewDataBlock( GameBaseData *dptr, bool reload );
  179. void updatePos(F32 dt);
  180. bool updateCollision(F32 dt);
  181. bool resolveCollision(Rigid& ns,CollisionList& cList);
  182. bool resolveContacts(Rigid& ns,CollisionList& cList,F32 dt);
  183. bool resolveDisplacement(Rigid& ns,CollisionState *state,F32 dt);
  184. bool findContacts(Rigid& ns,CollisionList& cList);
  185. void checkTriggers();
  186. static void findCallback(SceneObject* obj,void * key);
  187. void setPosition(const Point3F& pos,const QuatF& rot);
  188. void setRenderPosition(const Point3F& pos,const QuatF& rot);
  189. void setTransform(const MatrixF& mat);
  190. // virtual bool collideBody(const MatrixF& mat,Collision* info) = 0;
  191. virtual void updateMove(const Move* move);
  192. virtual void updateForces(F32 dt);
  193. void writePacketData(GameConnection * conn, BitStream *stream);
  194. void readPacketData (GameConnection * conn, BitStream *stream);
  195. U32 packUpdate (NetConnection *conn, U32 mask, BitStream *stream);
  196. void unpackUpdate(NetConnection *conn, BitStream *stream);
  197. void updateLiftoffDust( F32 dt );
  198. void updateDamageSmoke( F32 dt );
  199. void updateWorkingCollisionSet(const U32 mask);
  200. virtual U32 getCollisionMask();
  201. void updateFroth( F32 dt );
  202. bool collidingWithWater( Point3F &waterHeight );
  203. /// ObjectRenderInst delegate hooked up in prepBatchRender
  204. /// if GameBase::gShowBoundingBox is true.
  205. void _renderMassAndContacts( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  206. /// ObjectRenderInst delegate hooked up in prepBatchRender
  207. /// if GameBase::gShowBoundingBox is true.
  208. void _renderMuzzleVector( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  209. public:
  210. // Test code...
  211. static ClippedPolyList* sPolyList;
  212. static S32 sVehicleCount;
  213. //
  214. Vehicle();
  215. static void consoleInit();
  216. static void initPersistFields();
  217. void processTick(const Move *move);
  218. bool onAdd();
  219. void onRemove();
  220. /// Interpolates between move ticks @see processTick
  221. /// @param dt Change in time between the last call and this call to the function
  222. void interpolateTick(F32 dt);
  223. void advanceTime(F32 dt);
  224. /// Disables collisions for this vehicle and all mounted objects
  225. void disableCollision();
  226. /// Enables collisions for this vehicle and all mounted objects
  227. void enableCollision();
  228. /// Returns the velocity of the vehicle
  229. Point3F getVelocity() const;
  230. void setEnergyLevel(F32 energy);
  231. void prepBatchRender( SceneRenderState *state, S32 mountedImageIndex );
  232. ///@name Rigid body methods
  233. ///@{
  234. /// This method will get the velocity of the object, taking into account
  235. /// angular velocity.
  236. /// @param r Point on the object you want the velocity of, relative to Center of Mass
  237. /// @param vel Velocity (out)
  238. void getVelocity(const Point3F& r, Point3F* vel);
  239. /// Applies an impulse force
  240. /// @param r Point on the object to apply impulse to, r is relative to Center of Mass
  241. /// @param impulse Impulse vector to apply.
  242. void applyImpulse(const Point3F &r, const Point3F &impulse);
  243. void getCameraParameters(F32 *min, F32* max, Point3F* offset, MatrixF* rot);
  244. void getCameraTransform(F32* pos, MatrixF* mat);
  245. ///@}
  246. /// @name Mounted objects
  247. /// @{
  248. virtual void mountObject( SceneObject *obj, S32 node, const MatrixF &xfm = MatrixF::Identity );
  249. /// @}
  250. DECLARE_CONOBJECT(Vehicle);
  251. };
  252. #endif