Finally found how to create something what looks like a spring between rigid bodies.
See my code below:
Code: Select all
void addSpring(btDynamicsWorld *dw, btRigidBody *rba, btRigidBody *rbb)
{
btTransform TransformA, TransformB;
TransformA = btTransform::getIdentity();
TransformB = btTransform::getIdentity();
btTransform rbat = rba->getCenterOfMassTransform();
btVector3 up(rbat.getBasis()[0][0], rbat.getBasis()[1][0], rbat.getBasis()[2][0]);
btVector3 direction = (rbb->getWorldTransform().getOrigin() - rba->getWorldTransform().getOrigin()).normalize();
btScalar angle = acos(up.dot(direction));
btVector3 axis = up.cross(direction);
TransformA.setRotation(btQuaternion(axis, angle));
TransformB.setRotation(btQuaternion(axis, angle));
btSliderConstraint *spring = new btSliderConstraint((*rba), (*rbb), TransformA, TransformB, true);
spring->setLowerLinLimit(0.0f);
spring->setUpperLinLimit((rbb->getWorldTransform().getOrigin() - rba->getWorldTransform().getOrigin()).length());
spring->setLowerAngLimit(0.0f);
spring->setUpperAngLimit(0.0f);
/* Total dampening prevents sliding down */
/* TODO: replace by linear motor which moves bodies apart to create more realistic spring effect */
spring->setDampingDirLin(1.0f);
/* The following allow for some more error to create more bounciness */
/* spring->setSoftnessOrthoLin(1.0f); */
/* spring->setSoftnessLimLin(1.0f); */
dw->addConstraint(spring, true);
}
This creates some realistic looking suspension.
Will update this thread when finished using the linear motor.