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