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.

72 lines
1.8 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "simple_physics.h"
  9. #include "tier0/dbg.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. CSimplePhysics::CSimplePhysics()
  13. {
  14. Init( 1.0f / 30.0f ); // default is 30 steps per second
  15. }
  16. void CSimplePhysics::Init( float flTimeStep )
  17. {
  18. m_flPredictedTime = 0;
  19. m_iCurTimeStep = 0;
  20. m_flTimeStep = flTimeStep;
  21. m_flTimeStepMul = m_flTimeStep*m_flTimeStep*0.5f;
  22. }
  23. void CSimplePhysics::Simulate(
  24. CSimplePhysics::CNode *pNodes,
  25. int nNodes,
  26. CSimplePhysics::IHelper *pHelper,
  27. float dt,
  28. float flDamp )
  29. {
  30. // Figure out how many time steps to run.
  31. m_flPredictedTime += dt;
  32. int newTimeStep = (int)ceil( m_flPredictedTime / m_flTimeStep );
  33. int nTimeSteps = newTimeStep - m_iCurTimeStep;
  34. for( int iTimeStep=0; iTimeStep < nTimeSteps; iTimeStep++ )
  35. {
  36. // Simulate everything..
  37. for( int iNode=0; iNode < nNodes; iNode++ )
  38. {
  39. CSimplePhysics::CNode *pNode = &pNodes[iNode];
  40. // Apply forces.
  41. Vector vAccel;
  42. pHelper->GetNodeForces( pNodes, iNode, &vAccel );
  43. Assert( vAccel.IsValid() );
  44. Vector vPrevPos = pNode->m_vPos;
  45. pNode->m_vPos = pNode->m_vPos + (pNode->m_vPos - pNode->m_vPrevPos) * flDamp + vAccel * m_flTimeStepMul;
  46. pNode->m_vPrevPos = vPrevPos;
  47. }
  48. // Apply constraints.
  49. pHelper->ApplyConstraints( pNodes, nNodes );
  50. }
  51. m_iCurTimeStep = newTimeStep;
  52. // Setup predicted positions.
  53. float flInterpolant = (m_flPredictedTime - (GetCurTime() - m_flTimeStep)) / m_flTimeStep;
  54. for( int iNode=0; iNode < nNodes; iNode++ )
  55. {
  56. CSimplePhysics::CNode *pNode = &pNodes[iNode];
  57. VectorLerp( pNode->m_vPrevPos, pNode->m_vPos, flInterpolant, pNode->m_vPredicted );
  58. }
  59. }