rigidShape.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 _RIGIDSHAPE_H_
  23. #define _RIGIDSHAPE_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. class RigidShapeData : public ShapeBaseData
  37. {
  38. typedef ShapeBaseData Parent;
  39. protected:
  40. bool onAdd();
  41. //-------------------------------------- Console set variables
  42. public:
  43. struct Body
  44. {
  45. enum Sounds
  46. {
  47. SoftImpactSound,
  48. HardImpactSound,
  49. MaxSounds,
  50. };
  51. SFXTrack* sound[MaxSounds];
  52. F32 restitution;
  53. F32 friction;
  54. } body;
  55. enum RigidShapeConsts
  56. {
  57. VC_NUM_DUST_EMITTERS = 1,
  58. VC_NUM_BUBBLE_EMITTERS = 1,
  59. VC_NUM_SPLASH_EMITTERS = 2,
  60. VC_BUBBLE_EMITTER = VC_NUM_BUBBLE_EMITTERS,
  61. };
  62. enum Sounds
  63. {
  64. ExitWater,
  65. ImpactSoft,
  66. ImpactMedium,
  67. ImpactHard,
  68. Wake,
  69. MaxSounds
  70. };
  71. SFXTrack* waterSound[MaxSounds];
  72. F32 exitSplashSoundVel;
  73. F32 softSplashSoundVel;
  74. F32 medSplashSoundVel;
  75. F32 hardSplashSoundVel;
  76. F32 minImpactSpeed;
  77. F32 softImpactSpeed;
  78. F32 hardImpactSpeed;
  79. F32 minRollSpeed;
  80. bool cameraRoll; ///< Roll the 3rd party camera
  81. F32 cameraLag; ///< Amount of camera lag (lag += car velocity * lag)
  82. F32 cameraDecay; ///< Rate at which camera returns to target pos.
  83. F32 cameraOffset; ///< Vertical offset
  84. F32 minDrag;
  85. F32 maxDrag;
  86. S32 integration; ///< # of physics steps per tick
  87. F32 collisionTol; ///< Collision distance tolerance
  88. F32 contactTol; ///< Contact velocity tolerance
  89. Point3F massCenter; ///< Center of mass for rigid body
  90. Point3F massBox; ///< Size of inertial box
  91. ParticleEmitterData * dustEmitter;
  92. S32 dustID;
  93. F32 triggerDustHeight; ///< height shape has to be under to kick up dust
  94. F32 dustHeight; ///< dust height above ground
  95. ParticleEmitterData* splashEmitterList[VC_NUM_SPLASH_EMITTERS];
  96. S32 splashEmitterIDList[VC_NUM_SPLASH_EMITTERS];
  97. F32 splashFreqMod;
  98. F32 splashVelEpsilon;
  99. F32 dragForce;
  100. F32 vertFactor;
  101. ParticleEmitterData * dustTrailEmitter;
  102. S32 dustTrailID;
  103. //-------------------------------------- load set variables
  104. public:
  105. RigidShapeData();
  106. ~RigidShapeData();
  107. static void initPersistFields();
  108. void packData(BitStream*);
  109. void unpackData(BitStream*);
  110. bool preload(bool server, String &errorStr);
  111. DECLARE_CONOBJECT(RigidShapeData);
  112. };
  113. //----------------------------------------------------------------------------
  114. class RigidShape: public ShapeBase
  115. {
  116. typedef ShapeBase Parent;
  117. private:
  118. RigidShapeData* mDataBlock;
  119. SimObjectPtr<ParticleEmitter> mDustTrailEmitter;
  120. protected:
  121. enum CollisionFaceFlags
  122. {
  123. BodyCollision = BIT(0),
  124. WheelCollision = BIT(1),
  125. };
  126. enum MaskBits {
  127. PositionMask = Parent::NextFreeMask << 0,
  128. EnergyMask = Parent::NextFreeMask << 1,
  129. FreezeMask = Parent::NextFreeMask << 2,
  130. ForceMoveMask = Parent::NextFreeMask << 3,
  131. NextFreeMask = Parent::NextFreeMask << 4
  132. };
  133. void updateDustTrail( F32 dt );
  134. struct StateDelta
  135. {
  136. Move move; ///< Last move from server
  137. F32 dt; ///< Last interpolation time
  138. // Interpolation data
  139. Point3F pos;
  140. Point3F posVec;
  141. QuatF rot[2];
  142. // Warp data
  143. S32 warpTicks; ///< Number of ticks to warp
  144. S32 warpCount; ///< Current pos in warp
  145. Point3F warpOffset;
  146. QuatF warpRot[2];
  147. //
  148. Point3F cameraOffset;
  149. Point3F cameraVec;
  150. Point3F cameraRot;
  151. Point3F cameraRotVec;
  152. };
  153. StateDelta mDelta;
  154. S32 mPredictionCount; ///< Number of ticks to predict
  155. bool inLiquid;
  156. Point3F mCameraOffset; ///< 3rd person camera
  157. // Rigid Body
  158. bool mDisableMove;
  159. CollisionList mCollisionList;
  160. CollisionList mContacts;
  161. Rigid mRigid;
  162. ShapeBaseConvex mConvex;
  163. S32 restCount;
  164. SimObjectPtr<ParticleEmitter> mDustEmitterList[RigidShapeData::VC_NUM_DUST_EMITTERS];
  165. SimObjectPtr<ParticleEmitter> mSplashEmitterList[RigidShapeData::VC_NUM_SPLASH_EMITTERS];
  166. GFXStateBlockRef mSolidSB;
  167. //
  168. bool onNewDataBlock( GameBaseData *dptr, bool reload );
  169. void updatePos(F32 dt);
  170. bool updateCollision(F32 dt);
  171. bool resolveCollision(Rigid& ns,CollisionList& cList);
  172. bool resolveContacts(Rigid& ns,CollisionList& cList,F32 dt);
  173. bool resolveDisplacement(Rigid& ns,CollisionState *state,F32 dt);
  174. bool findContacts(Rigid& ns,CollisionList& cList);
  175. void checkTriggers();
  176. static void findCallback(SceneObject* obj,void * key);
  177. void setPosition(const Point3F& pos,const QuatF& rot);
  178. void setRenderPosition(const Point3F& pos,const QuatF& rot);
  179. void setTransform(const MatrixF& mat);
  180. // virtual bool collideBody(const MatrixF& mat,Collision* info) = 0;
  181. void updateMove(const Move* move);
  182. void writePacketData(GameConnection * conn, BitStream *stream);
  183. void readPacketData (GameConnection * conn, BitStream *stream);
  184. void updateLiftoffDust( F32 dt );
  185. void updateWorkingCollisionSet(const U32 mask);
  186. U32 getCollisionMask();
  187. void updateFroth( F32 dt );
  188. bool collidingWithWater( Point3F &waterHeight );
  189. void _renderMassAndContacts( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  190. void updateForces(F32);
  191. public:
  192. // Test code...
  193. static ClippedPolyList* sPolyList;
  194. //
  195. RigidShape();
  196. ~RigidShape();
  197. static void initPersistFields();
  198. void processTick(const Move *move);
  199. bool onAdd();
  200. void onRemove();
  201. /// Interpolates between move ticks @see processTick
  202. /// @param dt Change in time between the last call and this call to the function
  203. void interpolateTick(F32 dt);
  204. void advanceTime(F32 dt);
  205. /// Disables collisions for this shape
  206. void disableCollision();
  207. /// Enables collisions for this shape
  208. void enableCollision();
  209. /// Returns the velocity of the shape
  210. Point3F getVelocity() const;
  211. void setEnergyLevel(F32 energy);
  212. void prepBatchRender( SceneRenderState *state, S32 mountedImageIndex );
  213. // xgalaxy cool hacks
  214. void reset();
  215. void freezeSim(bool frozen);
  216. ///@name Rigid body methods
  217. ///@{
  218. /// This method will get the velocity of the object, taking into account
  219. /// angular velocity.
  220. /// @param r Point on the object you want the velocity of, relative to Center of Mass
  221. /// @param vel Velocity (out)
  222. void getVelocity(const Point3F& r, Point3F* vel);
  223. /// Applies an impulse force
  224. /// @param r Point on the object to apply impulse to, r is relative to Center of Mass
  225. /// @param impulse Impulse vector to apply.
  226. void applyImpulse(const Point3F &r, const Point3F &impulse);
  227. /// Forces the client to jump to the RigidShape's transform rather
  228. /// then warp to it.
  229. void forceClientTransform();
  230. void getCameraParameters(F32 *min, F32* max, Point3F* offset, MatrixF* rot);
  231. void getCameraTransform(F32* pos, MatrixF* mat);
  232. ///@}
  233. U32 packUpdate (NetConnection *conn, U32 mask, BitStream *stream);
  234. void unpackUpdate(NetConnection *conn, BitStream *stream);
  235. DECLARE_CONOBJECT(RigidShape);
  236. DECLARE_CALLBACK( void, onEnterLiquid, ( const char* objId, F32 waterCoverage, const char* liquidType ));
  237. DECLARE_CALLBACK( void, onLeaveLiquid, ( const char* objId, const char* liquidType ));
  238. };
  239. #endif