vehicle.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. bool enablePhysicsRep;
  113. //
  114. VehicleData();
  115. bool preload(bool server, String &errorStr);
  116. static void initPersistFields();
  117. virtual void packData(BitStream* stream);
  118. virtual void unpackData(BitStream* stream);
  119. DECLARE_CONOBJECT(VehicleData);
  120. DECLARE_CALLBACK( void, onEnterLiquid, ( Vehicle* obj, F32 coverage, const char* type ) );
  121. DECLARE_CALLBACK( void, onLeaveLiquid, ( Vehicle* obj, const char* type ) );
  122. };
  123. //----------------------------------------------------------------------------
  124. class PhysicsBody;
  125. class Vehicle: public ShapeBase
  126. {
  127. typedef ShapeBase Parent;
  128. protected:
  129. enum CollisionFaceFlags {
  130. BodyCollision = 0x1,
  131. WheelCollision = 0x2,
  132. };
  133. enum MaskBits {
  134. PositionMask = Parent::NextFreeMask << 0,
  135. EnergyMask = Parent::NextFreeMask << 1,
  136. NextFreeMask = Parent::NextFreeMask << 2
  137. };
  138. struct StateDelta {
  139. Move move; ///< Last move from server
  140. F32 dt; ///< Last interpolation time
  141. // Interpolation data
  142. Point3F pos;
  143. Point3F posVec;
  144. QuatF rot[2];
  145. // Warp data
  146. S32 warpTicks; ///< Number of ticks to warp
  147. S32 warpCount; ///< Current pos in warp
  148. Point3F warpOffset;
  149. QuatF warpRot[2];
  150. //
  151. Point3F cameraOffset;
  152. Point3F cameraVec;
  153. Point3F cameraRot;
  154. Point3F cameraRotVec;
  155. };
  156. PhysicsBody *mPhysicsRep;
  157. StateDelta mDelta;
  158. S32 mPredictionCount; ///< Number of ticks to predict
  159. VehicleData* mDataBlock;
  160. bool inLiquid;
  161. SFXSource* mWakeSound;
  162. Point3F mCameraOffset; ///< 3rd person camera
  163. // Control
  164. Point2F mSteering;
  165. F32 mThrottle;
  166. bool mJetting;
  167. // Rigid Body
  168. bool mDisableMove;
  169. GFXStateBlockRef mSolidSB;
  170. Box3F mWorkingQueryBox;
  171. S32 mWorkingQueryBoxCountDown;
  172. CollisionList mCollisionList;
  173. CollisionList mContacts;
  174. Rigid mRigid;
  175. ShapeBaseConvex mConvex;
  176. S32 restCount;
  177. SimObjectPtr<ParticleEmitter> mDustEmitterList[VehicleData::VC_NUM_DUST_EMITTERS];
  178. SimObjectPtr<ParticleEmitter> mDamageEmitterList[VehicleData::VC_NUM_DAMAGE_EMITTERS];
  179. SimObjectPtr<ParticleEmitter> mSplashEmitterList[VehicleData::VC_NUM_SPLASH_EMITTERS];
  180. //
  181. virtual bool onNewDataBlock( GameBaseData *dptr, bool reload );
  182. void updatePos(F32 dt);
  183. bool updateCollision(F32 dt);
  184. bool resolveCollision(Rigid& ns,CollisionList& cList);
  185. bool resolveContacts(Rigid& ns,CollisionList& cList,F32 dt);
  186. bool resolveDisplacement(Rigid& ns,CollisionState *state,F32 dt);
  187. bool findContacts(Rigid& ns,CollisionList& cList);
  188. void checkTriggers();
  189. static void findCallback(SceneObject* obj,void * key);
  190. void setPosition(const Point3F& pos,const QuatF& rot);
  191. void setRenderPosition(const Point3F& pos,const QuatF& rot);
  192. void setTransform(const MatrixF& mat);
  193. // virtual bool collideBody(const MatrixF& mat,Collision* info) = 0;
  194. virtual void updateMove(const Move* move);
  195. virtual void updateForces(F32 dt);
  196. void writePacketData(GameConnection * conn, BitStream *stream);
  197. void readPacketData (GameConnection * conn, BitStream *stream);
  198. U32 packUpdate (NetConnection *conn, U32 mask, BitStream *stream);
  199. void unpackUpdate(NetConnection *conn, BitStream *stream);
  200. void updateLiftoffDust( F32 dt );
  201. void updateDamageSmoke( F32 dt );
  202. void updateWorkingCollisionSet(const U32 mask);
  203. virtual U32 getCollisionMask();
  204. void updateFroth( F32 dt );
  205. bool collidingWithWater( Point3F &waterHeight );
  206. /// ObjectRenderInst delegate hooked up in prepBatchRender
  207. /// if GameBase::gShowBoundingBox is true.
  208. void _renderMassAndContacts( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  209. /// ObjectRenderInst delegate hooked up in prepBatchRender
  210. /// if GameBase::gShowBoundingBox is true.
  211. void _renderMuzzleVector( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  212. public:
  213. // Test code...
  214. static ClippedPolyList* sPolyList;
  215. static S32 sVehicleCount;
  216. //
  217. Vehicle();
  218. static void consoleInit();
  219. static void initPersistFields();
  220. void processTick(const Move *move);
  221. bool onAdd();
  222. void onRemove();
  223. void _createPhysics();
  224. /// Interpolates between move ticks @see processTick
  225. /// @param dt Change in time between the last call and this call to the function
  226. void interpolateTick(F32 dt);
  227. void advanceTime(F32 dt);
  228. /// Disables collisions for this vehicle and all mounted objects
  229. void disableCollision();
  230. /// Enables collisions for this vehicle and all mounted objects
  231. void enableCollision();
  232. /// Returns the velocity of the vehicle
  233. Point3F getVelocity() const;
  234. void setEnergyLevel(F32 energy);
  235. void prepBatchRender( SceneRenderState *state, S32 mountedImageIndex );
  236. ///@name Rigid body methods
  237. ///@{
  238. /// This method will get the velocity of the object, taking into account
  239. /// angular velocity.
  240. /// @param r Point on the object you want the velocity of, relative to Center of Mass
  241. /// @param vel Velocity (out)
  242. void getVelocity(const Point3F& r, Point3F* vel);
  243. /// Applies an impulse force
  244. /// @param r Point on the object to apply impulse to, r is relative to Center of Mass
  245. /// @param impulse Impulse vector to apply.
  246. void applyImpulse(const Point3F &r, const Point3F &impulse);
  247. void getCameraParameters(F32 *min, F32* max, Point3F* offset, MatrixF* rot);
  248. void getCameraTransform(F32* pos, MatrixF* mat);
  249. ///@}
  250. /// @name Mounted objects
  251. /// @{
  252. virtual void mountObject( SceneObject *obj, S32 node, const MatrixF &xfm = MatrixF::Identity );
  253. /// @}
  254. DECLARE_CONOBJECT(Vehicle);
  255. };
  256. #endif