physicsDebris.h 4.7 KB

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