dampened spring oscillation experiment script for weekendish nerd waste of time

mCasualmCasual Posts: 4,607
edited September 2013 in Freebies

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


you can download the script here
https://sites.google.com/site/mcasualsdazscripts3/physicsrk4_001
there's also an HTML5 ( camvas + javascript ) "app" to visialize
the simulation as a curve and as an animation, right in your (modern) browser!

//implementation of <a href="http://gafferongames.com/game-physics/integration-basics/">http://gafferongames.com/game-physics/integration-basics/</a>;var g_k = 10; // spring constantvar g_b = .4;  // dampingvar 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();
moviiii.gif
600 x 337 - 282K
Post edited by mCasual on

Comments

  • mCasualmCasual Posts: 4,607
    edited September 2013

    you can download the script here

    https://sites.google.com/site/mcasualsdazscripts3/physicsrk4_001

    there's also an HTML5 ( camvas + javascript ) "app" to visualize

    the simulation as a curve and as an animation, right in your (modern) browser!

    or you can copy & paste it here .... not ,,, since the forum removes javascript tags

    boingboing.gif
    640 x 360 - 362K
    Post edited by mCasual on
  • mCasualmCasual Posts: 4,607
    edited December 1969

    i was reading the comments on 's page

    and apparently springs-based rigging may be discarded in favor of something called "soft constraints"

    https://code.google.com/p/box2d/downloads/detail?name=GDC2011_Catto_Erin_Soft_Constraints.pdf

  • patience55patience55 Posts: 7,006
    edited December 1969

    Casual said:
    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


    you can download the script here
    https://sites.google.com/site/mcasualsdazscripts3/physicsrk4_001
    there's also an HTML5 ( camvas + javascript ) "app" to visialize
    the simulation as a curve and as an animation, right in your (modern) browser!

    //implementation of <a href="http://gafferongames.com/game-physics/integration-basics/">http://gafferongames.com/game-physics/integration-basics/</a>;var g_k = 10; // spring constantvar g_b = .4;  // dampingvar 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();

    Reminds me of a car window wiper. Starts off good, and slows down as the window dries.

Sign In or Register to comment.