diff options
| author | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
|---|---|---|
| committer | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
| commit | 3bf9df6b2785fa6d951086978a3e66f49427166a (patch) | |
| tree | 2c0f1f0c63c4832882bc93814ebd2c2b1c6224e5 /public/simple_physics.h | |
| download | archived-source-engine-2018-hl2-src-master.tar.xz archived-source-engine-2018-hl2-src-master.zip | |
Diffstat (limited to 'public/simple_physics.h')
| -rw-r--r-- | public/simple_physics.h | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/public/simple_physics.h b/public/simple_physics.h new file mode 100644 index 0000000..fd40b18 --- /dev/null +++ b/public/simple_physics.h @@ -0,0 +1,81 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: +// +// $NoKeywords: $ +//=============================================================================// + +#ifndef SIMPLE_PHYSICS_H +#define SIMPLE_PHYSICS_H +#ifdef _WIN32 +#pragma once +#endif + + +#include "mathlib/vector.h" + + +// CSimplePhysics is a framework for simplified physics simulation. +// It simulates at a fixed timestep and uses the Verlet integrator. +// +// To use it, create your nodes and implement your constraints and +// forces in an IHelper, then call Simulate each frame. +// CSimplePhysics will figure out how many timesteps to run and will +// provide predicted positions of things for you. +class CSimplePhysics +{ +public: + + class CNode + { + public: + + // Call this when initializing the nodes with their starting positions. + void Init( const Vector &vPos ) + { + m_vPos = m_vPrevPos = m_vPredicted = vPos; + } + + Vector m_vPos; // At time t + Vector m_vPrevPos; // At time t - m_flTimeStep + Vector m_vPredicted; // Predicted position + }; + + class IHelper + { + public: + virtual void GetNodeForces( CNode *pNodes, int iNode, Vector *pAccel ) = 0; + virtual void ApplyConstraints( CNode *pNodes, int nNodes ) = 0; + }; + + +public: + + CSimplePhysics(); + + void Init( float flTimeStep ); + + void Simulate( + CNode *pNodes, + int nNodes, + IHelper *pHelper, + float dt, + float flDamp ); + + +private: + + double GetCurTime() { return m_flTimeStep * m_iCurTimeStep; } + + +private: + + double m_flPredictedTime; // (GetCurTime()-m_flTimeStep) <= m_flPredictedTime <= GetCurTime() + int m_iCurTimeStep; + + float m_flTimeStep; + float m_flTimeStepMul; // dt*dt*0.5 +}; + + +#endif // SIMPLE_PHYSICS_H |