Spring constrained to a single axis

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

Spring constrained to a single axis

Post by machuidel »

How can I create a single axis spring constraint between 2 rigid bodies? I want to simulate suspension between 2 bodies but could not find any typed constraint to use.

I was thinking about using the SliderConstraint but cannot figure out how to simulate a spring with it (using motors etc.).

Any ideas?

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

Re: Spring constrained to a single axis

Post by machuidel »

Okay. I got it to work but don't really know why. Still need to figure out the exact meaning of some function calls (even though I think I know what they mean).

Code: Select all

        btTransform TransformA, TransformB;
        TransformA = (rba->getWorldTransform().inverse() * rbb->getWorldTransform());
        TransformB = btTransform::getIdentity();

        btSliderConstraint *spring = new btSliderConstraint((*rba), (*rbb), TransformA, TransformB, true);

        spring->setLowerLinLimit(0.0f);
        spring->setUpperLinLimit(0.5f);
        spring->setLowerAngLimit(0.0f);
        spring->setUpperAngLimit(0.0f);

        // TODO: Figure out exact meaning of the following calls
        spring->setDampingDirLin(0.0f);
        spring->setRestitutionLimLin(1.1f);
        spring->setDampingLimLin(0.1f);
        spring->setSoftnessOrthoLin(0.1);
        spring->setSoftnessLimLin(0.1);

        mDynamicsWorld->addConstraint(spring, true);
The use of softness seems to absorb some of the energy keeping the simulation from exploding (by violating the slider constraint).

EDIT:
Still not the thing I was looking for :(. It seems the slider constraint (at least by default) initially aligns to the worlds x-axis. Think I have to find another constraint I can use to act as a spring. Crazy, as even in my own simple physics engine I added a spring constraint. I assume Bullet should have something like that as well (I actually found what I need inside the soft body code [ cfm and erp ], but I need it as a constraint between rigid bodies).