Make a rigid body stay vertical

Post Reply
zoubi
Posts: 3
Joined: Mon Nov 05, 2012 11:20 am

Make a rigid body stay vertical

Post by zoubi »

Hello,

I'd like an object to always stay vertical. It can translate freely in all directions, but the rotation should be constrained only on the Y axis.

For now, I have this code:

Code: Select all

btBoxShape
        * box_shape;
    btVector3
        half_extent,
        local_intertia( 0, 0, 0 );

    half_extent = //...

    box_shape = new btBoxShape( half_extent );

    if ( mass != 0.0f )
    {
        box_shape->calculateLocalInertia( mass, local_intertia );
    }

    btRigidBody::btRigidBodyConstructionInfo
        construction_info( mass, &MotionState, box_shape, local_intertia );

    Object = new btRigidBody( construction_info );

    Object->setRestitution( 0.8f );
    Object->setFriction( 3.0f );
    Object->setDamping( 0.02f, 0.1f );

    btVector3
        aabb_min,
        aabb_max,
        pivot;

    Object->getAabb( aabb_min, aabb_max );

    pivot = ( aabb_min + aabb_max ) / 2;
    pivot.setY( aabb_max.y() - aabb_min.y() );

    btHingeConstraint
        * constraint = new btHingeConstraint( *Object, pivot, btVector3( 0, 1, 0 ) );

    ConstraintsTable.AddLastItem( constraint );

    Object->addConstraintRef( constraint );

    Object->setCcdMotionThreshold( motion_threshold );
    Object->setCcdSweptSphereRadius( motion_threshold / 2.0f );
But unfortunately it doesn't work. My object can still fall on its side. I know for sure the problem is not related to the creation of the box, because objects using this shape work well (as soon as I don't need them to be constrained of course :p)

Is the Hinge constraint what I need? If not, how should I do that?

Thanks in advance
Humungo
Posts: 5
Joined: Tue Oct 30, 2012 9:34 am

Re: Make a rigid body stay vertical

Post by Humungo »

Try setAngularFactor(btVector3(0,1,0)) on your rigid body, that should allow for only y-axis rotation.
zoubi
Posts: 3
Joined: Mon Nov 05, 2012 11:20 am

Re: Make a rigid body stay vertical

Post by zoubi »

Thanks!

It did the trick :)
Post Reply