physicsDebris.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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, protected AssetPtrCallback
  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_REFACTOR(PhysicsDebrisData, Shape)
  69. PhysicsDebrisData();
  70. virtual ~PhysicsDebrisData();
  71. bool onAdd() override;
  72. bool preload( bool server, String &errorStr ) override;
  73. static void initPersistFields();
  74. void packData( BitStream *stream ) override;
  75. void unpackData( BitStream *stream ) override;
  76. DECLARE_CONOBJECT( PhysicsDebrisData );
  77. protected:
  78. void onAssetRefreshed(AssetPtrBase* pAssetPtrBase) override
  79. {
  80. reloadOnLocalClient();
  81. }
  82. };
  83. class PhysicsBody;
  84. class PhysicsWorld;
  85. class PhysicsDebris : public GameBase
  86. {
  87. typedef GameBase Parent;
  88. public:
  89. /// Helper method which creates debris based on the
  90. /// datablock and initial state.
  91. ///
  92. /// It can return NULL if the system quality settings
  93. /// are set to disable the debris.
  94. ///
  95. static PhysicsDebris* create( PhysicsDebrisData *datablock,
  96. const MatrixF &transform,
  97. const VectorF &linVel );
  98. PhysicsDebris();
  99. virtual ~PhysicsDebris();
  100. DECLARE_CONOBJECT(PhysicsDebris);
  101. DECLARE_CATEGORY("UNLISTED");
  102. static void initPersistFields();
  103. bool onAdd() override;
  104. void onRemove() override;
  105. void applyImpulse( const Point3F &pos, const VectorF &vec ) override;
  106. void applyRadialImpulse( const Point3F &origin, F32 radius, F32 magnitude ) override;
  107. protected:
  108. /// The global object lifetime scalar.
  109. static F32 smLifetimeScale;
  110. void processTick( const Move *move ) override;
  111. void advanceTime( F32 dt ) override;
  112. void interpolateTick( F32 delta ) override;
  113. bool onNewDataBlock( GameBaseData *dptr, bool reload ) override;
  114. void prepRenderImage( SceneRenderState *state ) override;
  115. void prepBatchRender( SceneRenderState *state );
  116. void _deleteFragments();
  117. void _createFragments();
  118. void _updateForces( PhysicsBody *body, const Box3F &bounds );
  119. void _findNodes( U32 objId, Vector<U32> &nodeIds );
  120. void _onPhysicsReset( PhysicsResetEvent reset );
  121. protected:
  122. F32 mLifetime;
  123. Point3F mInitialLinVel;
  124. PhysicsDebrisData *mDataBlock;
  125. TSShapeInstance *mShapeInstance;
  126. PhysicsWorld *mWorld;
  127. struct Fragment
  128. {
  129. Vector<U32> nodeIds;
  130. PhysicsBody *body;
  131. // The delta state.
  132. Point3F pos;
  133. Point3F lastPos;
  134. QuatF rot;
  135. QuatF lastRot;
  136. };
  137. typedef Vector<Fragment> FragmentVector;
  138. FragmentVector mFragments;
  139. };
  140. #endif // _PHYSICS_DEBRIS_H_