### Physics RK4 001

Over here http://gafferongames.com/game-physics/integration-basics/

someone shows how to implement physics simulations using an RK4 integrator

and here’s my implementation of his c++ source as a Daz Script

1 - create a cylinder, 30cm tall 1 cm diameter
2 - select the cylinder
3 - run the script

the Z rotation angle of the selected cylinder will be driven by the simulated output ( x )

the line that reads “if( (cpt++) > 200 ) break;” is a safety device limiting the simulation to 200 frames

cpt also serves as the keyframe time

you may want to replace

zRot.setValue( cpt * tick, state[0] );

by

zRot.setValue( t * 4800, state[0] );

g_k is the Hooke’s law spring constant, g_b is the dampening factor

 `//implementation of http://gafferongames.com/game-physics/integration-basics/` `var g_k = 10; // spring constant` `var g_b = .4; // damping` `var onesixth = 1 / 6;` `function acceleration( state, t )` `{` `return( - g_k * state[0] - g_b * state[1] );` `}` `function evaluateA( initial, t )` `{` `outputdx = initial[1];` `outputdv = acceleration( initial, t );` `return( [ outputdx, outputdv ] );` `}` `function evaluateB( initial, t, dt, d )` `{` `state = new Array( 2 );` `state[0] = initial[0] + d[0] * dt;` `state[1] = initial[1] + d[1] * dt;` `output = new Array( 2 );` `output[0] = state[1];` `output[1] = acceleration( state, t + dt );` `return( output );` `}` `function integrate( state, t, dt )` `{` `a = evaluateA( state, t );` `b = evaluateB( state, t, dt * 0.5, a );` `c = evaluateB( state, t, dt * 0.5, b );` `d = evaluateB( state, t, dt, c );` `dxdt = onesixth * ( a[0] + 2.0 * ( b[0] + c[0] ) + d[0] );` `dvdt = onesixth * ( a[1] + 2.0 * ( b[1] + c[1] ) + d[1] );` `state[0] = state[0] + dxdt * dt;` `state[1] = state[1] + dvdt * dt;` `}` `function main() ` `{` `zRot = Scene.getPrimarySelection().getZRotControl();` `tick = Scene.getTimeStep();` `var state = [ 100, 0 ];` `var t = 0;` `var dt = 0.1;` `cpt = 0;` `while( Math.abs( state[0] ) > 0.001 || Math.abs( state[1] ) > 0.001 )` `{` `debug( state[0] + " " + state[1] );` `zRot.setValue( cpt * tick, state[0] ); ` `integrate( state, t, dt );` `t += dt;` `if( (cpt++) > 200 )` `break;` `}` `}` `main(); `

and here is HTML5 applet to display the curve and motion

 ` ` ` ` `RK4 001 ` ` ` ` ` ` ` `

` `` `` `` `` `                ` `

` ` ` ` `

ċ
mcjRK4001.dsa
(1k)
mCasual Jacques,
Sep 29, 2013, 6:00 AM
ċ
mCasual Jacques,
Sep 29, 2013, 12:01 PM