Team Fortress 2 Source Code as on 22/4/2020
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.

213 lines
5.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef SHEETSIMULATOR_H
  8. #define SHEETSIMULATOR_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "mathlib/mathlib.h"
  13. #include "mathlib/vector.h"
  14. #include "utlvector.h"
  15. // Uncomment this for client-side simulation
  16. //#define CLIENT_SIDE_SIMULATION 1
  17. //-----------------------------------------------------------------------------
  18. // Simulates a sheet
  19. //-----------------------------------------------------------------------------
  20. class CGameTrace;
  21. typedef CGameTrace trace_t;
  22. //struct trace_t;
  23. typedef void (*TraceLineFunc_t)(const Vector &vecStart, const Vector &vecEnd,
  24. unsigned int mask, int collisionGroup, trace_t *ptr);
  25. typedef void (*TraceHullFunc_t)(const Vector &vecStart, const Vector &vecEnd,
  26. const Vector &hullMin, const Vector &hullMax,
  27. unsigned int mask, int collisionGroup, trace_t *ptr);
  28. class CSheetSimulator
  29. {
  30. public:
  31. CSheetSimulator( TraceLineFunc_t traceline, TraceHullFunc_t traceHull );
  32. ~CSheetSimulator();
  33. void Init( int w, int h, int fixedPointCount );
  34. // orientation
  35. void SetPosition( const Vector& origin, const QAngle& angles );
  36. // Makes a spring
  37. void AddSpring( int p1, int p2, float restLength );
  38. void AddFixedPointSpring( int fixedPoint, int p, float restLength );
  39. // spring constants....
  40. void SetPointSpringConstant( float constant );
  41. void SetFixedSpringConstant( float constant );
  42. // Used for both kinds of springs
  43. void SetSpringDampConstant( float damp );
  44. void SetViscousDrag( float drag );
  45. // Sets the collision group
  46. void SetCollisionGroup( int group );
  47. // Sets the bounding box used for collision vs world
  48. void SetBoundingBox( Vector& mins, Vector& maxs );
  49. // Computes the bounding box
  50. void ComputeBounds( Vector& mins, Vector& maxs );
  51. // simulation
  52. void Simulate( float dt );
  53. void Simulate( float dt, int steps );
  54. // get at the points
  55. int NumHorizontal() const;
  56. int NumVertical() const;
  57. int PointCount() const;
  58. // Fixed points
  59. Vector& GetFixedPoint( int i );
  60. // Point masses
  61. const Vector& GetPoint( int x, int y ) const;
  62. const Vector& GetPoint( int i ) const;
  63. // For iterative collision detection
  64. void DetectCollision( int i, float flOffset );
  65. void InitPosition( int i );
  66. // For offseting the control points
  67. void SetControlPointOffset( const Vector& offset );
  68. // Gravity
  69. void SetGravityConstant( float g );
  70. void AddGravityForce( int particle );
  71. protected:
  72. struct Particle_t
  73. {
  74. float m_Mass;
  75. Vector m_Position;
  76. Vector m_Velocity;
  77. Vector m_Force;
  78. int m_Collided;
  79. int m_CollisionPlane;
  80. float m_CollisionDist;
  81. };
  82. struct Spring_t
  83. {
  84. int m_Particle1;
  85. int m_Particle2;
  86. float m_RestLength;
  87. };
  88. inline int NumParticles() const
  89. {
  90. return m_HorizontalCount * m_VerticalCount;
  91. }
  92. // simulator
  93. void EulerStep( float dt );
  94. void ComputeControlPoints();
  95. void ClearForces();
  96. void ComputeForces();
  97. void TestVertAgainstPlane( int vert, int plane, bool bFarTest = true );
  98. void SatisfyCollisionConstraints();
  99. void DetermineBestCollisionPlane( bool bFarTest = true );
  100. void ClampPointsToCollisionPlanes();
  101. // How many particles horiz + vert?
  102. int m_HorizontalCount;
  103. int m_VerticalCount;
  104. // The particles
  105. Particle_t* m_Particle;
  106. // Output position after simulation
  107. Vector* m_OutputPosition;
  108. // fixed points
  109. int m_FixedPointCount;
  110. Vector* m_pFixedPoint;
  111. Vector* m_ControlPoints;
  112. CUtlVector<Spring_t> m_Springs;
  113. CUtlVector<int> m_Gravity;
  114. // raycasting methods
  115. TraceLineFunc_t m_TraceLine;
  116. TraceHullFunc_t m_TraceHull;
  117. // Spring constants
  118. float m_FixedSpringConstant;
  119. float m_PointSpringConstant;
  120. float m_DampConstant;
  121. float m_ViscousDrag;
  122. // Collision group
  123. int m_CollisionGroup;
  124. // position + orientation
  125. Vector m_Origin;
  126. QAngle m_Angles;
  127. // collision box
  128. Vector m_FrustumBoxMin;
  129. Vector m_FrustumBoxMax;
  130. // Collision planes
  131. cplane_t* m_pCollisionPlanes;
  132. bool* m_pValidCollisionPlane;
  133. // Control point offset
  134. Vector m_ControlPointOffset;
  135. // Gravity
  136. float m_GravityConstant;
  137. };
  138. //-----------------------------------------------------------------------------
  139. // Class to help dealing with the iterative computation
  140. //-----------------------------------------------------------------------------
  141. class CIterativeSheetSimulator : public CSheetSimulator
  142. {
  143. public:
  144. CIterativeSheetSimulator( TraceLineFunc_t traceline, TraceHullFunc_t traceHull );
  145. void BeginSimulation( float dt, int steps, int substeps, int collisionCount );
  146. // Returns true if it just did a simulation step
  147. bool Think( );
  148. bool IsDone() const { return m_SimulationSteps == 0; }
  149. int StepsRemaining( ) const { return m_SimulationSteps; }
  150. private:
  151. CIterativeSheetSimulator( const CIterativeSheetSimulator & ); // not defined, not accessible
  152. // Iterative collision detection
  153. void DetectCollisions( void );
  154. float m_TimeStep;
  155. float m_SubSteps;
  156. char m_TotalSteps;
  157. char m_SimulationSteps;
  158. char m_CollisionCount;
  159. char m_CurrentCollisionPt;
  160. bool m_InitialPass;
  161. };
  162. #endif // TF_SHIELD_SHARED_H