constraining rotation in one direction

uklebot
Posts: 3
Joined: Tue May 24, 2011 4:41 pm

constraining rotation in one direction

Post by uklebot »

I'm using a hinge constraint to limit to rotation about the Y axis, but I'd like to limit it to clockwise only. I couldn't find any options for directional limitation, though. Is that something I'd have to write myself? How would I go about doing so?
marios
Posts: 52
Joined: Mon Jul 19, 2010 3:11 am

Re: constraining rotation in one direction

Post by marios »

first idea that comes into my mind, is to set each frame limit(for example low limit) using setLimit(...) method with actual angle of constraint that you can get by getHingeAngle() . You can try doing that, but i don't guarantee that works corectly.

edit:
instead of setting new limit each frame, you can do check if new angle of hinge is bigger/smaller (depends of which direction you wants rotation) than set new limit. This can prevent rotating backwards.
uklebot
Posts: 3
Joined: Tue May 24, 2011 4:41 pm

Re: constraining rotation in one direction

Post by uklebot »

I tried using your first idea and run this in the main loop, before running the physics engine. Seems to work fairly well and constrain rotation to clockwise only--will have to do some more testing. I'm sure there's a more efficient way though.

Code: Select all

    if(hinge) {
        float ca = hinge->getHingeAngle(); // current angle
        std::cout << ca << std::endl;
        float lim = 0.0;
        if(ca < 0)
            lim = ca + PI;
        else
            lim = ca - PI;
        // smaller first to be a limit, otherwise gets interpreted as no limit
        if(lim < ca)
            hinge->setLimit(btScalar(lim), btScalar(ca));
        else
            hinge->setLimit(btScalar(ca), btScalar(lim));
    }