[ODE] Re: Springs

j ode at dynamica.org
Sat Jun 25 23:32:29 MST 2005


Hi brad,
 
 Here is an exerpt from my code for the ODE-version
of LivingConcepts at www.dynamica.org, it's not complete
but you might find it useful. Note that the project is open source,
upon request : )
 
 Jonathan
 
 //-------------------------------------------------------------------
 
  dMass m1, m2;
  dBodyGetMass( object1->GetBodyID(), &m1 );
  dBodyGetMass( object2->GetBodyID(), &m2 );
 
  // 'restlength' is the length of the spring without forces acting on it
 // and 'currentlength' is the distance between the points where
 // the spring is attached to the bodies.
  s1 = + ( restlength - currentlength ) * m1.mass * config.stiffness;
  s2 = - ( restlength - currentlength ) * m2.mass * config.stiffness;
 
  // &m[8] is a normalized vector pointing
  // in the direction of the spring
 
  V3D_SCA( f1, &m[8], s1 ); // f1 = &m[8] * s1
  V3D_SCA( f2, &m[8], s2 );
 
  // a scalar is a dReal
  scalar *lv;
  Vector3 vd1, vd2;
 
  lv = ( scalar * )dBodyGetLinearVel( object1->GetBodyID() );
  scalar d1 = V3D_DOT( &m[8], lv );
  V3D_SCA( vd1, &m[8], d1 * config.damping );
 
  lv = ( scalar * )dBodyGetLinearVel( object2->GetBodyID() );
  scalar d2 = V3D_DOT( &m[8], lv );
  V3D_SCA( vd2, &m[8], d2 * config.damping );
 
  V3D_DEC( f1, vd1 );    // f1 = f1 - vd1
  V3D_DEC( f2, vd2 );
 
  // p1_local is the point where the
  // spring attaches to body 1 in local coords
   dBodyAddForceAtRelPos( object1->GetBodyID(), f1[0], f1[1], f1[2],
 p1_local[0], p1_local[1], p1_local[2] );
   dBodyAddForceAtRelPos( object2->GetBodyID(), f2[0], f2[1], f2[2],
 p2_local[0], p2_local[1], p2_local[2] );
 
 //-------------------------------------------------------------------
 


More information about the ODE mailing list