So i started this thread a while ago, and the replies dried up. I don't think i made it clear as to my problem so i wanted to re word it so that i might get some more responses.
Firstly let me point out that i know RK4 is over kill, but it's mainly just so that i can learn and practise my coding techniques, so while i understand there are good intentions to pointing out other systems lets try and keep this about RK4. So i have implemented a system similar to that detailed on http://gafferongames.com but i am still confused as to where external forces fit into the system.
From my understanding the way RK4 works is that it needs to calculate the force of a derivative implicitly from the current state, rather than explicitly applying forces once per update. So if that's the case how do i say apply some thrust to a ship for example?
Let me break it down furthur i have the position and momentum of the RigidBody prior to any calculation, i also have the velocity of the current derivative step. What i want to be able to do at this point is apply things like collision input and 'push' forces, and the end result should be to calculate the forces for this current derivative step. I also have the current time step also.
Here is some code for you, hopefully its clear enough, please just ask if anything is confusing.
* Integrate a state forward by a delta time.
* This function uses the RK4 integration method to update the state by a given
* @param state Reference to the state that needs integrating.
* @param deltaTime Time to move the state on by.
void CPhysController::Integrate(CRidgidBody::CRBState &state, float deltaTime)
//RK4 uses 4 derivatives of the current state at different points within the delttime.
//Each evaluation uses the previous derivative to calculate next.
CRidgidBody::CRBDerivative a = Evaluate(state, deltaTime, CRidgidBody::CRBDerivative() );
CRidgidBody::CRBDerivative b = Evaluate(state, deltaTime*0.5f, a);
CRidgidBody::CRBDerivative c = Evaluate(state, deltaTime*0.5f, b);
CRidgidBody::CRBDerivative d = Evaluate(state, deltaTime, c);
//Using a wighted sum that comes from the Taylor Series expansion, the best overall derivative
//is calculated. This can then increment the state.
state.UpdatePosition( 1.0f/6.0f * deltaTime * (a.mVelocity + 2.0f*(b.mVelocity + c.mVelocity) + d.mVelocity) );
state.UpdateMomentum( 1.0f/6.0f * deltaTime * (a.mForce + 2.0f*(b.mForce + c.mForce) + d.mForce) );
CRidgidBody::CRBDerivative CPhysController::Evaluate(CRidgidBody::CRBState &state, float deltaTime,
const CRidgidBody::CRBDerivative &prevDerivative)
//First move the state on base on this time step value, using euler integration.
state.UpdatePosition( prevDerivative.mVelocity * deltaTime );
state.UpdateMomentum( prevDerivative.mForce * deltaTime );
//Prep our derivative
CRidgidBody::CRBDerivative derivative = CRidgidBody::CRBDerivative();
derivative.mVelocity = state.GetVelocity();
void CPhysController::Resolve(CRidgidBody::CRBState ¤tState, CRidgidBody::CRBDerivative &derivative)
Any help understanding this would be great. I did think that perhaps i should be coming at the problem from a different angle, perhaps someone here could enlighten me.