Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
5.4 KiB

  1. //========= Copyright � Valve Corporation, All rights reserved. ============//
  2. //
  3. // A small subset of CRnWorld
  4. // Note: mathlib is tentative place for this code. We will probably move it to a separate lib or dll or vphysics.dll
  5. //
  6. #ifndef MATHLIB_SOFTBODY_ENV_HDR
  7. #define MATHLIB_SOFTBODY_ENV_HDR
  8. #include "mathlib/vector.h"
  9. #include "rubikon/param_types.h"
  10. #include "tier1/utlincrementalvector.h"
  11. #include "mathlib/softbody.h"
  12. #include "rubikon/intersection.h"
  13. #include "mathlib/aabb.h"
  14. #include "mathlib/dynamictree.h"
  15. class CDynamicTree;
  16. class CSoftbodyCollisionSphere;
  17. class CSoftbodyCollisionCapsule;
  18. class CSoftbodyCollisionFilter
  19. {
  20. public:
  21. CSoftbodyCollisionFilter();
  22. void InitGroup( int nGroup, CollisionGroupPairFlags defaultFlags = 0 );
  23. uint16 TestSimulation( const RnCollisionAttr_t &left, const RnCollisionAttr_t &right )const;
  24. public:
  25. enum ConstEnum_t{ MAX_GROUPS = COLLISION_GROUPS_MAX_ALLOWED };
  26. CollisionGroupPairFlags m_GroupPairs[ MAX_GROUPS ][ MAX_GROUPS ];
  27. };
  28. class CSoftbodyCollisionShape
  29. {
  30. public:
  31. CSoftbodyCollisionShape( PhysicsShapeType_t type ) : m_nType( type ), m_nProxyId( -1 ) {}
  32. // CSoftbodyCollisionSphere *IsSphere();
  33. // CSoftbodyCollisionCapsule *IsCapsule();
  34. // Shape type
  35. PhysicsShapeType_t GetType( void ) const { return m_nType; }
  36. const RnCollisionAttr_t &GetCollisionAttributes( void ) const { return m_CollisionAttr; }
  37. RnCollisionAttr_t &GetCollisionAttributes( void ) { return m_CollisionAttr; }
  38. int32 GetProxyId() const { return m_nProxyId; }
  39. void SetProxyId( int32 nProxyId ){ m_nProxyId = nProxyId; }
  40. AABB_t GetBbox()const;
  41. protected:
  42. RnCollisionAttr_t m_CollisionAttr;
  43. PhysicsShapeType_t m_nType; // not really necessary..
  44. int32 m_nProxyId;
  45. };
  46. class CSoftbodyCollisionSphere: public CSoftbodyCollisionShape
  47. {
  48. public:
  49. CSoftbodyCollisionSphere() : CSoftbodyCollisionShape( SHAPE_SPHERE ){}
  50. void SetRadius( float flRadius ) { m_flRadius = flRadius; }
  51. float GetRadius() const { return m_flRadius; }
  52. void SetCenter( const Vector &vCenter ){ m_vCenter = vCenter; }
  53. const Vector &GetCenter()const { return m_vCenter; }
  54. AABB_t GetBbox()const;
  55. protected:
  56. Vector m_vCenter;
  57. float m_flRadius;
  58. };
  59. class CSoftbodyCollisionCapsule : public CSoftbodyCollisionShape
  60. {
  61. public:
  62. CSoftbodyCollisionCapsule() : CSoftbodyCollisionShape( SHAPE_CAPSULE ) {}
  63. void SetRadius( float flRadius ) { m_flRadius = flRadius; }
  64. float GetRadius() const { return m_flRadius; }
  65. void SetCenter( int nIndex, const Vector &vCenter ){ m_vCenter[ nIndex ] = vCenter; }
  66. const Vector &GetCenter( int nIndex )const { return m_vCenter[ nIndex ]; }
  67. AABB_t GetBbox()const;
  68. protected:
  69. Vector m_vCenter[2];
  70. float m_flRadius;
  71. };
  72. class CSoftbodyEnvironment
  73. {
  74. public:
  75. CSoftbodyEnvironment();
  76. uint GetSoftbodySimulationFlags()const { return 0; }
  77. uint GetSoftbodyIterations() const { return m_nIterations; }
  78. void SetSoftbodyIterations( int nIterations ) { m_nIterations = nIterations; }
  79. CDynamicTree *GetBroadphaseTree() { return &m_BroadphaseTree; }
  80. const VectorAligned &GetGravity()const { return m_vGravity; }
  81. CDebugHighlightCone& GetDebugHighlightCone() { return m_DebugHighlightCone; }
  82. void Register( CSoftbody *pSoftbody ) { m_Softbodies.AddToTail( pSoftbody ); }
  83. void Unregister( CSoftbody *pSoftbody ) { m_Softbodies.FindAndFastRemove( pSoftbody ); }
  84. void Step( float dt, float flSubstepDt = 1.0f / 60.0f, int nMaxSubsteps = 3 );
  85. int GetSoftbodyCount() const { return m_Softbodies.Count(); }
  86. CSoftbody* GetSoftbody( int i ) { return m_Softbodies[ i ]; }
  87. void Add( CSoftbodyCollisionShape * pShape );
  88. void AddOrUpdate( CSoftbodyCollisionShape * pShape );
  89. void Update( CSoftbodyCollisionShape * pShape );
  90. void Remove( CSoftbodyCollisionShape * pShape );
  91. const Vector4DAligned &GetWindDesc() const { return m_vWindDesc; }
  92. void SetWind( const Vector & vWind );
  93. void SetWindDesc( const Vector &vWindDir, float flStrength ) { m_vWindDesc.Init( vWindDir, flStrength ); }
  94. void SetNoWind() { m_vWindDesc.Init( 1, 0, 0, 0 ); }
  95. public:
  96. CSoftbodyCollisionFilter m_Filter;
  97. protected:
  98. Vector4DAligned m_vWindDesc; // normalized direction in x,y,z and strength in w
  99. VectorAligned m_vGravity;
  100. CDynamicTree m_BroadphaseTree;
  101. int m_nIterations;
  102. CDebugHighlightCone m_DebugHighlightCone;
  103. CUtlIncrementalVector< CSoftbody, CSoftbody::CWorldIndexPred > m_Softbodies;
  104. float m_flAccumulatedTimeSlack;
  105. };
  106. inline CSoftbodyEnvironment::CSoftbodyEnvironment()
  107. {
  108. SetNoWind();
  109. m_flAccumulatedTimeSlack = 0.0f;
  110. m_nIterations = 1;
  111. m_vGravity.Init( 0, 0, -360 );
  112. }
  113. inline void CSoftbodyEnvironment::Step( float dt, float flSubstepDt, int nMaxSubsteps )
  114. {
  115. m_flAccumulatedTimeSlack += dt;
  116. if ( m_flAccumulatedTimeSlack < flSubstepDt )
  117. return;
  118. float flSubsteps = m_flAccumulatedTimeSlack / flSubstepDt;
  119. int nSubsteps = int( flSubsteps );
  120. if ( nSubsteps < nMaxSubsteps )
  121. {
  122. m_flAccumulatedTimeSlack = m_flAccumulatedTimeSlack - floorf( flSubsteps * flSubstepDt );
  123. }
  124. else
  125. {
  126. nSubsteps = nMaxSubsteps;
  127. m_flAccumulatedTimeSlack = 0.0f;
  128. }
  129. for ( int i = 0; i < m_Softbodies.Count(); ++i )
  130. {
  131. CSoftbody *pSoftbody = m_Softbodies[ i ];
  132. for ( int j = 0; j < nSubsteps; ++j )
  133. {
  134. pSoftbody->Step( flSubstepDt );
  135. }
  136. }
  137. }
  138. inline AABB_t CSoftbodyCollisionShape::GetBbox()const
  139. {
  140. switch ( m_nType )
  141. {
  142. default:
  143. Assert( m_nType == SHAPE_SPHERE );
  144. return static_cast< const CSoftbodyCollisionSphere* >( this )->GetBbox();
  145. case SHAPE_CAPSULE:
  146. return static_cast< const CSoftbodyCollisionCapsule* >( this )->GetBbox();
  147. }
  148. }
  149. #endif