Here's a code snippet of where I'm creating a spring. Note that this spring has not offset, a user-defined rest length, and user-defined limits.
Code: Select all
const btVector3 RIGID_A_POS(...);
const btVector3 RIGID_B_POS(...);
btVector3 diff = RIGID_B_POS - RIGID_A_POS;
diff = diff * 0.5;
// Set the frame offsets. This appears to require global coordinates
// which is strange.
btTransform xformA = btTransform::getIdentity();
xformA.setOrigin(diff);
btTransform xformB = btTransform::getIdentity();
xformB.setOrigin(-diff);
btGeneric6DofSpringConstraint* p2p =
new btGeneric6DofSpringConstraint(*pBodyA, *pBodyB,
xformA, xformB,
true);
p2p->enableFeedback(true);
const float k = MY_SPRING_CONSTANT;
const float kd = 1.0 - MY_DAMPING_CONSTANT;
for (int dof=0; dof<3; ++dof)
{
p2p->enableSpring(dof, true);
p2p->setStiffness(dof, k);
p2p->setDamping (dof, kd);
}
for (int dof=3; dof<6; ++dof)
p2p->enableSpring(dof, false);
for (int dof=0; dof<6; ++dof)
p2p->setParam(BT_CONSTRAINT_STOP_CFM, 1.0e-5f, dof);
// constrain the max +/- extension of the spring
const float limitLength = MY_LIMIT_LENGTH;
if (limitLength >= 0)
{
p2p->setLinearLowerLimit(
btVector3(-1.*limitLength, -1.*limitLength, -1.*limitLength));
p2p->setLinearUpperLimit(
btVector3(limitLength, limitLength, limitLength));
}
else
{
p2p->setLinearLowerLimit(btVector3(-1e99, -1e99, -1e99));
p2p->setLinearUpperLimit(btVector3(1e99, 1e99, 1e99));
}
// set the rest-length
const float restLength = MY_REST_LENGTH - 1;
for (int dof=0; dof<3; ++dof)
p2p->setEquilibriumPoint(dof, diff[dof]*restLength);
for (int dof=3; dof<6; ++dof)
p2p->setEquilibriumPoint(dof, 0.);
I've searched the forums and looked through the code examples, but without much illumination.