rigidShape.h 9.4 KB

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