Game Development Reference
In-Depth Information
public double getX() {
return getQ(1);
}
public double getTime() {
return getS();
}
The ODE solver will be used to update the location and velocity of the spring as a function
of time. The SpringODE class declares a method called updatePositionAndVelocity that calls the
Runge-Kutta solver.
// This method updates the velocity and position
// of the spring using a 4th order Runge-Kutta
// solver to integrate the equations of motion.
public void updatePositionAndVelocity(double dt) {
ODESolver.rungeKutta4(this, dt);
}
The final method declared in the SpringODE class is an implementation of the
getRightHandSide method first declared in the ODE class. The getRightHandSide method
computes the right-hand side of the two spring motion ODEs according to the Runge-Kutta
method relations shown in Equations (4.23a) through (4.23d). Which of the four relations is
computed (
q 4 ) depends on the arguments sent to the method.
// sides of the two first-order damped spring ODEs
// q[0] = vx
// q[1] = x
// dq[0] = d(vx) = dt*(-mu*dxdt - k*x)/mass
// dq[1] = d(x) = dt*(v)
public double[] getRightHandSide(double s, double q[],
double deltaQ[], double ds, double qScale) {
Δ
q 1 ,
Δ
q 2 ,
Δ
q 3 , or
Δ
double dq[] = new double[4]; // right-hand side values
double newQ[] = new double[4]; // intermediate dependent
// variable values.
// Compute the intermediate values of the
// dependent variables.
for(int i=0; i<2; ++i) {
newQ[i] = q[i] + qScale*deltaQ[i];
}
// Compute right-hand side values.
dq[0] = -ds*(mu*newQ[0] + k*newQ[1])/mass;
dq[1] = ds*(newQ[0]);
return dq;
}
}
Search WWH ::




Custom Search