So i generally understand how RK4 work, how you get the derivatives from the current object state, applying forces at different points in the time step. Then you move on the simulation based on some kind of weighted average of those 4 derivatives.
My problem is understanding how external influences are applied to an object in the system, as Glenn Fiedler's example seems to be dependant on time rather on an object moving under the influence of external forces.
Let me give you an example, here is a function and description from the source code in Glenn's article titled 'Physics in 3D':
/// Evaluate derivative values for the physics state at future time t+dt
/// using the specified set of derivatives to advance dt seconds from the
/// specified physics state.
static Derivative evaluate(State state, float t, float dt, const Derivative &derivative)
{
state.position += derivative.velocity * dt;
state.momentum += derivative.force * dt;
state.orientation += derivative.spin * dt;
state.angularMomentum += derivative.torque * dt;
state.recalculate();
Derivative output;
output.velocity = state.velocity;
output.spin = state.spin;
forces(state, t+dt, output.force, output.torque);
return output;
}
/// Calculate force and torque for physics state at time t.
/// Due to the way that the RK4 integrator works we need to calculate
/// force implicitly from state rather than explictly applying forces
/// to the rigid body once per update. This is because the RK4 achieves
/// its accuracy by detecting curvature in derivative values over the
/// timestep so we need our force values to supply the curvature.
static void forces(const State &state, float t, Vector &force, Vector &torque)
{
// attract towards origin
force = -10 * state.position;
// sine force to add some randomness to the motion
force.x += 10 * Mathematics::sin(t*0.9f + 0.5f);
force.y += 11 * Mathematics::sin(t*0.5f + 0.4f);
force.z += 12 * Mathematics::sin(t*0.7f + 0.9f);
// sine torque to get some spinning action
torque.x = 1.0f * Mathematics::sin(t*0.9f + 0.5f);
torque.y = 1.1f * Mathematics::sin(t*0.5f + 0.4f);
torque.z = 1.2f * Mathematics::sin(t*0.7f + 0.9f);
// damping torque so we dont spin too fast
torque -= 0.2f * state.angularVelocity;
}
From what i can gather 't is the time since the start of the simulation. In this function he uses the time to give a cube object motion. So firstly i want to ask, is this an integral part of the RK4 implementation? Does the force calculation of a derivative need to be derived from a total time value?
What i want to know is how do you use this model in a simple example such as gravity and say a ships thrusters acting against it?
My thought is that forces acting on an object would be stored as a variable in the State class, so you would add gravity as a force acting down each update, and the thrusters added to this store acting in the opposite direction on each update the thrusters are active. The forces function would then be modified to be interested in the time step rather than the total time. Giving you something that looks like this:
static void forces(const State &state, float dt, Vector &force, Vector &torque)
{
//Force of derivative is equal to the external forces over the time step.
force = state.totalExternalForce * dt;
//...omited the rotation for clarity...
}
Am i on the right lines here? Have i misunderstood something fundamental? Thanks for reading my gibberings, any help understanding this would be much appreciated.