diff options
| author | Joe Ludwig <[email protected]> | 2013-06-26 15:22:04 -0700 |
|---|---|---|
| committer | Joe Ludwig <[email protected]> | 2013-06-26 15:22:04 -0700 |
| commit | 39ed87570bdb2f86969d4be821c94b722dc71179 (patch) | |
| tree | abc53757f75f40c80278e87650ea92808274aa59 /sp/src/public/simple_physics.h | |
| download | source-sdk-2013-39ed87570bdb2f86969d4be821c94b722dc71179.tar.xz source-sdk-2013-39ed87570bdb2f86969d4be821c94b722dc71179.zip | |
First version of the SOurce SDK 2013
Diffstat (limited to 'sp/src/public/simple_physics.h')
| -rw-r--r-- | sp/src/public/simple_physics.h | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/sp/src/public/simple_physics.h b/sp/src/public/simple_physics.h new file mode 100644 index 00000000..d122d822 --- /dev/null +++ b/sp/src/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
|