X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FRK4.hh;h=806127ecc977c1388b2dcef37f838f2658f86f77;hp=00c0b649c9406414dd7a56593322b59bb1f8b780;hb=d5b4262bc0c6cea41c603e8a3a85ab93adfdc36b;hpb=df541170776dc4ac4f241ca480812bd70bcb6eca diff --git a/src/Moof/RK4.hh b/src/Moof/RK4.hh index 00c0b64..806127e 100644 --- a/src/Moof/RK4.hh +++ b/src/Moof/RK4.hh @@ -34,9 +34,9 @@ namespace Mf { -// Generic implementation of the RK4 integrator. To use, you need one type -// representing the state and another containing the derivatives of the primary -// state variables. The state class must implement these methods: +// Generic implementations of a few simple integrators. To use, you need one +// type representing the state and another containing the derivatives of the +// primary state variables. The state class must implement these methods: // // void getDerivative(Derivative_Type& derivative, Scalar absoluteTime); // void applyDerivative(const Derivative_Type& derivative, Scalar deltaTime); @@ -64,7 +64,24 @@ inline D evaluate(const S& state, Scalar t, Scalar dt, const D& derivative) template -inline void integrate(S& state, Scalar t, Scalar dt) +inline void euler(S& state, Scalar t, Scalar dt) +{ + D a = evaluate(state, t); + + state.applyDerivative(a, dt); +} + +template +inline void rk2(S& state, Scalar t, Scalar dt) +{ + D a = evaluate(state, t); + D b = evaluate(state, t, dt * 0.5, a); + + state.applyDerivative(b, dt); +} + +template +inline void rk4(S& state, Scalar t, Scalar dt) { D a = evaluate(state, t); D b = evaluate(state, t, dt * 0.5, a); @@ -75,15 +92,6 @@ inline void integrate(S& state, Scalar t, Scalar dt) } -//template -//inline T spring(Scalar k, Scalar b) -//{ - //current.force = -15 * (current.position - Mf::Vector2(200.0, 200.0)) - //- 15.0 * current.velocity; - //return -//} - - } // namespace Mf #endif // _MOOF_RK4_HH_