physicsDebris.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 _PHYSICS_DEBRIS_H_
  23. #define _PHYSICS_DEBRIS_H_
  24. #ifndef __RESOURCE_H__
  25. #include "core/resource.h"
  26. #endif
  27. #ifndef _GAMEBASE_H_
  28. #include "T3D/gameBase/gameBase.h"
  29. #endif
  30. #ifndef _T3D_PHYSICSCOMMON_H_
  31. #include "T3D/physics/physicsCommon.h"
  32. #endif
  33. class TSShapeInstance;
  34. class TSShape;
  35. //**************************************************************************
  36. // Debris Data
  37. //**************************************************************************
  38. class PhysicsDebrisData : public GameBaseData
  39. {
  40. typedef GameBaseData Parent;
  41. public:
  42. F32 lifetime;
  43. F32 lifetimeVariance;
  44. ///
  45. F32 mass;
  46. ///
  47. F32 dynamicFriction;
  48. ///
  49. F32 staticFriction;
  50. ///
  51. F32 restitution;
  52. ///
  53. F32 linearDamping;
  54. ///
  55. F32 angularDamping;
  56. ///
  57. F32 linearSleepThreshold;
  58. ///
  59. F32 angularSleepThreshold;
  60. // A scale applied to the normal linear and angular damping
  61. // when the object enters a water volume.
  62. F32 waterDampingScale;
  63. // The density of this object used for water buoyancy effects.
  64. F32 buoyancyDensity;
  65. /// Is rendererd during shadow passes.
  66. bool castShadows;
  67. const char* shapeName;
  68. Resource<TSShape> shape;
  69. PhysicsDebrisData();
  70. bool onAdd();
  71. bool preload( bool server, String &errorStr );
  72. static void initPersistFields();
  73. void packData( BitStream *stream );
  74. void unpackData( BitStream *stream );
  75. DECLARE_CONOBJECT( PhysicsDebrisData );
  76. };
  77. class PhysicsBody;
  78. class PhysicsWorld;
  79. class PhysicsDebris : public GameBase
  80. {
  81. typedef GameBase Parent;
  82. public:
  83. /// Helper method which creates debris based on the
  84. /// datablock and initial state.
  85. ///
  86. /// It can return NULL if the system quality settings
  87. /// are set to disable the debris.
  88. ///
  89. static PhysicsDebris* create( PhysicsDebrisData *datablock,
  90. const MatrixF &transform,
  91. const VectorF &linVel );
  92. PhysicsDebris();
  93. virtual ~PhysicsDebris();
  94. DECLARE_CONOBJECT(PhysicsDebris);
  95. static void initPersistFields();
  96. bool onAdd();
  97. void onRemove();
  98. void applyImpulse( const Point3F &pos, const VectorF &vec );
  99. void applyRadialImpulse( const Point3F &origin, F32 radius, F32 magnitude );
  100. protected:
  101. /// The global object lifetime scalar.
  102. static F32 smLifetimeScale;
  103. void processTick( const Move *move );
  104. void advanceTime( F32 dt );
  105. void interpolateTick( F32 delta );
  106. bool onNewDataBlock( GameBaseData *dptr, bool reload );
  107. void prepRenderImage( SceneRenderState *state );
  108. void prepBatchRender( SceneRenderState *state );
  109. void _deleteFragments();
  110. void _createFragments();
  111. void _updateForces( PhysicsBody *body, const Box3F &bounds );
  112. void _findNodes( U32 objId, Vector<U32> &nodeIds );
  113. void _onPhysicsReset( PhysicsResetEvent reset );
  114. protected:
  115. F32 mLifetime;
  116. Point3F mInitialLinVel;
  117. PhysicsDebrisData *mDataBlock;
  118. TSShapeInstance *mShapeInstance;
  119. PhysicsWorld *mWorld;
  120. struct Fragment
  121. {
  122. Vector<U32> nodeIds;
  123. PhysicsBody *body;
  124. // The delta state.
  125. Point3F pos;
  126. Point3F lastPos;
  127. QuatF rot;
  128. QuatF lastRot;
  129. };
  130. typedef Vector<Fragment> FragmentVector;
  131. FragmentVector mFragments;
  132. };
  133. #endif // _PHYSICS_DEBRIS_H_