Page 1 of 1

Basic help with btGeneric6DofSpringConstraint

Posted: Thu Jul 12, 2012 11:50 pm
by riotnrrd
I wish to attach a spring between two rigid objects, but I wish the attachment points to not be the center of mass of each object. Rather, I would like the attachment point to be an arbitrary (user-defined) local offset from the C.O.M. I also need to support non-zero rest lengths. How do I do this?

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.);
Any kind of illumination on what the frame offsets mean, and what coordinate system they're in, would be greatly appreciated.

I've searched the forums and looked through the code examples, but without much illumination.