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.

81 lines
1.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef SIMPLE_PHYSICS_H
  8. #define SIMPLE_PHYSICS_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "mathlib/vector.h"
  13. // CSimplePhysics is a framework for simplified physics simulation.
  14. // It simulates at a fixed timestep and uses the Verlet integrator.
  15. //
  16. // To use it, create your nodes and implement your constraints and
  17. // forces in an IHelper, then call Simulate each frame.
  18. // CSimplePhysics will figure out how many timesteps to run and will
  19. // provide predicted positions of things for you.
  20. class CSimplePhysics
  21. {
  22. public:
  23. class CNode
  24. {
  25. public:
  26. // Call this when initializing the nodes with their starting positions.
  27. void Init( const Vector &vPos )
  28. {
  29. m_vPos = m_vPrevPos = m_vPredicted = vPos;
  30. }
  31. Vector m_vPos; // At time t
  32. Vector m_vPrevPos; // At time t - m_flTimeStep
  33. Vector m_vPredicted; // Predicted position
  34. };
  35. class IHelper
  36. {
  37. public:
  38. virtual void GetNodeForces( CNode *pNodes, int iNode, Vector *pAccel ) = 0;
  39. virtual void ApplyConstraints( CNode *pNodes, int nNodes ) = 0;
  40. };
  41. public:
  42. CSimplePhysics();
  43. void Init( float flTimeStep );
  44. void Simulate(
  45. CNode *pNodes,
  46. int nNodes,
  47. IHelper *pHelper,
  48. float dt,
  49. float flDamp );
  50. private:
  51. double GetCurTime() { return m_flTimeStep * m_iCurTimeStep; }
  52. private:
  53. double m_flPredictedTime; // (GetCurTime()-m_flTimeStep) <= m_flPredictedTime <= GetCurTime()
  54. int m_iCurTimeStep;
  55. float m_flTimeStep;
  56. float m_flTimeStepMul; // dt*dt*0.5
  57. };
  58. #endif // SIMPLE_PHYSICS_H