rigidShape.h 9.1 KB

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