rigidShape.h 9.0 KB

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