Slider constraint (spring between bodies)

machuidel
Posts: 5
Joined: Mon Dec 08, 2008 6:46 pm

Slider constraint (spring between bodies)

Post by machuidel »

How can I set multiple slider constraints to something like this (3 dynamic rigid bodies, 2 slider constraints):
Image

Is that possible or should I use another constraint?

I can only get the slider constraint to allow movement along the world's x-axis. (of course ignoring errors and softness)

Edit:
Updated the title to be more on-topic. I hope this will not count as a double post as I could have added this to my other thread. Sorry for that.
Last edited by machuidel on Thu Dec 11, 2008 6:14 pm, edited 1 time in total.
machuidel
Posts: 5
Joined: Mon Dec 08, 2008 6:46 pm

Re: Slider constraint, angle / orientation [fixed image]

Post by machuidel »

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.